noor noor - 5 months ago 66
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 https://github.com/fastlane/fastlane/tree/master/scan

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"
task.launch()
task.waitUntilExit()
return task.terminationStatus
}

}


I call this function from another swift file like bwlow:

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


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.

Thanks

Answer

At last i found a way to generate html report from .plist file.As i used scan there's an open error in scan https://github.com/fastlane-old/gym/issues/235#event-580661928

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 }

    task.launch()
    task.waitUntilExit()
    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",
                   "-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")
Comments