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

时间:2016-06-27 14:28:11

标签: 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

1 个答案:

答案 0 :(得分:0)

NSTask不是shell,它不会为您解释shell脚本。

您需要手动设置NSPipe以将xcodebuild NSTask的标准输出与xcpretty NSTask连接起来。< / p>

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")