xcode - 在 swift 中使用 xcpretty 命令运行 xcodebuild 时出错

标签 xcode swift macos xcodebuild xcrun

要在 xcodebuild 中运行 xcpretty 命令,我使用以下代码:

import Foundation

class Command{

func command(args: String...) -> Int32 {
    let task = NSTask()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    task.currentDirectoryPath = "/Users/Desktop/XCode/Test/"
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}


let xcodebuildCommand = Command()
xcodebuildCommand.command("xcodebuild","test","-project","proj.xcodeproj","-scheme","projScheme","-destination","platform=iOS Simulator,name=iPad Air","  | /usr/local/bin/xcpretty --report html --output /Desktop/test_output/report.html")

错误是

xcodebuild: error: Unknown build action ' | /usr/local/bin/xcpretty --report html --output /Desktop/test_output/report.html'.

以下命令从终端正确运行:

xcodebuild test -project proj.xcodeproj.xcodeproj -scheme projScheme -destination 'platform=iOS Simulator,name=iPad Air' | xcpretty --repor html --output /pathToReportfolder/report.html

最佳答案

NSTask 不是一个 shell,它不会为你解释你的 shell 脚本。

您需要手动设置一个 NSPipe 以将您的 xcodebuild NSTask 的标准输出连接到一个 xcpretty NSTask.

import Foundation

func runCommand(workingDirectory: String? = nil,
                           stdin: NSPipe? = nil,
                          stdout: NSPipe? = nil,
                          stderr: NSPipe? = nil,
                            args: String...) -> Int32 {
    let task = NSTask()

    task.launchPath = "/usr/bin/env"
    task.arguments = args

    if let workingDirectory = workingDirectory {
        task.currentDirectoryPath = workingDirectory
    }

    if let stdin  = stdin  { task.standardInput  = stdin  }
    if let stdout = stdout { task.standardOutput = stdout }
    if let stderr = stderr { task.standardError  = stderr }

    task.launch()
    task.waitUntilExit()
    return (task.terminationStatus)
}

let pipe = NSPipe()

//omit "workingDirectory:" in Swift 2
runCommand(workingDirectory: "/Users/Desktop/XCode/Test/",
                     stdout: pipe,
                       args: "xcodebuild","test",
                       "-project","proj.xcodeproj",
                       "-scheme","projScheme",
                       "-destination","platform=iOS Simulator,name=iPad Air")

//omit "workingDirectory:" in Swift 2
runCommand(workingDirectory: "/Users/Desktop/XCode/Test/",
                      stdin: pipe,
                       args: "/usr/local/bin/xcpretty",
                       "--report html",
                       "--output",
                       "/Desktop/test_output/report.html")

关于xcode - 在 swift 中使用 xcpretty 命令运行 xcodebuild 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38056579/

相关文章:

iphone - "This device isn' t 符合所请求的构建。”

ios - 如何制作具有动态类型支持的应用程序?

swift - 在 Xcode 中创建自定义自动编译错误/警告消息

c++ - 如何在 XCode 中创建多个控制台窗口

macos - 在 .app 运行时触发脚本(AppleScript 或 JXA)?

swift - swift 收到有关剪贴板更改的通知

ios - 通知中心 - 显示和隐藏启动栏

ios - 添加图片到json?

c++ - Mac 上的 clang 不支持统一初始化吗?

macos - 将变量或字符串从 OS X Cocoa 应用程序传递到 Applescript