使用xcodebuild和xcpretty命令从.plist生成html报告

时间:2016-06-24 11:00:32

标签: xcode swift macos shell unix

首先,我的最终目标是使用xcodebuild命令从.plist文件生成html报告。

但是,我在我的osx项目中遇到了问题。我的环境是

osx 10.11 el captain, xcode 7.3 and swift 2.2

我已安装扫描来解决此问题。安装扫描的方法

gem install scan

扫描链接为https://github.com/fastlane/fastlane/tree/master/scan

现在在终端中,键入scan并按Enter键。 scan命令将执行所有操作。

但是我想通过我的swift代码运行这个终端命令。为此,我使用以下代码:

import Foundation

class Command{

    func terminalCommand(args: String...) -> Int32 {
        let task = NSTask()
        task.launchPath = "/usr/bin/env"
        task.arguments = args
        task.currentDirectoryPath = "path to my osx project"
        task.launch()
        task.waitUntilExit()
        return task.terminationStatus
       }

}

我从另一个sww文件中调用此函数,如bwlow:

let main = Command()
main.terminalCommand("scan")

链接在这里How do I run an terminal command in a swift script? (e.g. xcodebuild)

但如果我运行它,它会显示以下错误:

env: scan: No such file or directory

比我使用

which scan and it returns /usr/local/bin/scan

我将其添加到我的代码启动路径

 task.launchPath = "/usr/local/bin/scan"

,如果我运行我的swift代码,它会显示以下错误

    16:50:29]: [33mxcrun xcodebuild -list -project ./ios-ui-automation-demo.xcodeproj[0m

+-----------------------+------------------------------------+
|                   [32mSummary for scan 0.8.0[0m                   |
+-----------------------+------------------------------------+
| project               | ./ios-ui-automation-demo.xcodeproj |
| scheme                | ios-ui-automation-demo             |
| clean                 | false                              |
| code_coverage         | false                              |
| address_sanitizer     | false                              |
| skip_build            | false                              |
| output_directory      | ./fastlane/test_output             |
| output_types          | html,junit                         |
| buildlog_path         | ~/Library/Logs/scan                |
| open_report           | false                              |
| skip_slack            | false                              |
| slack_only_on_failure | false                              |
| use_clang_report_name | false                              |
+-----------------------+------------------------------------+

[16:50:34]: [4m[36m$ set -o pipefail && env NSUnbufferedIO=YES xcodebuild -scheme ios-ui-automation-demo -project ./ios-ui-automation-demo.xcodeproj -destination 'platform=iOS Simulator,id=841044C8-5637-4652-BDC9-3BAB05248F15' build test | tee '/Users/me/Library/Logs/scan/ios-ui-automation-demo-ios-ui-automation-demo.log' | xcpretty [0m[0m
[16:50:34]: ▸ [35mLoading...[0m
[16:50:34]: ▸ [35msh: xcpretty: command not found[0m

我的错误是什么以及为什么终端命令没有在swift代码中运行,因为它在终端中运行良好。

请,任何帮助或任何建议将受到高度赞赏。

还有一个弱点,我不是osx的专家。

由于

1 个答案:

答案 0 :(得分:0)

最后我找到了一种从.plist文件生成html报告的方法。正如我使用扫描时扫描中出现了一个打开的错误https://github.com/fastlane-old/gym/issues/235#event-580661928

所以,我尝试了另一种方式.....使用xcpretty。代码如下:

import Foundation

class command{

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

现在打电话给funcion:

let pipe = NSPipe()
let generateHtmlReport = command()

//omit "workingDirectory:" in Swift 2.2, only use the value
generateHtmlReport.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.2, only use the value
generateHtmlReport.runCommand(workingDirectory: "/Users/Desktop/XCode/Test/",
                  stdin: pipe,
                   args: "/usr/local/bin/xcpretty",
                   "--report","html",
                   "--output",
                   "/Desktop/test_output/report.html")