noor noor - 1 year ago 216
Swift Question

generate html report from .plist using xcodebuild and xcpretty command

First of all, my ultimate goal is to generate an html report from .plist file using xcodebuild command.

But, i am facing a problem in my osx project. My environment is

osx 10.11 el captain, xcode 7.3 and swift 2.2

I have install scan to solve this. The way to install scan

gem install scan

scan link is

Now in terminal, type scan and press enter. The scan command will do the everything.

But i want to run this terminal command through my swift code. For this, i use the below code:

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"
return task.terminationStatus


I call this function from another swift file like bwlow:

let main = Command()

The link is here How do I run an terminal command in a swift script? (e.g. xcodebuild)

But if i run this, it shows below error:

env: scan: No such file or directory

Than i use

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

i add this to my code launch path

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

than, if i run my swift code, it shows below error

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

what is my error and why terminal command is not run in swift code as it runs well in through terminal.

please, any help or any suggestion will be highly appreciated.

One more weakness, i am not pretty expert on osx.


Answer Source

At last i found a way to generate html report from .plist file.As i used scan there's an open error in scan

So, i tried in another way.....using xcpretty. Code is given below:

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 }

    return (task.terminationStatus)

now call the 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",
                   "-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",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download