0

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

Community
  • 1
  • 1
noor
  • 2,954
  • 2
  • 16
  • 29
  • Try supplying the full path to `xcpretty`... to find it do `which xcpretty` in terminal. It should come back as something like `/path/to/xcpretty`. – l'L'l Jun 24 '16 at 11:48
  • @I'L'l thanks for your reply but it is not clear to me what u are indicating about escape sequence in my command .... again thanks – noor Jun 24 '16 at 11:50
  • Ignore the comment about the color codes... it was in regards to the weirdness formatting (`4[36m`, etc. that is showing up in your question); the new comment about using the full path is what is important.* – l'L'l Jun 24 '16 at 11:52
  • i have already tried ur comment but its not working because command to use xcpretty and scan are not same .....i think scan use xcpretty command inside them ... some how error comes from there ...... thanks – noor Jun 24 '16 at 11:53
  • In terminal do: `which xcpretty`, what is the result? – l'L'l Jun 24 '16 at 11:54
  • /usr/local/bin/xcpretty – noor Jun 24 '16 at 11:54
  • Okay, so at the end of your command where `| xcpretty` is, try making it `| /usr/local/bin/xcpretty` – l'L'l Jun 24 '16 at 11:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115518/discussion-between-noor-and-lll). – noor Jun 24 '16 at 11:56

1 Answers1

0

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")
noor
  • 2,954
  • 2
  • 16
  • 29