我想用swift替换我的CI bash脚本。我不知道如何调用普通的终端命令,例如ls或xcodebuild
ls
xcodebuild
#!/usr/bin/env xcrun swift import Foundation // Works println("Test") // Works ls // Fails xcodebuild -workspace myApp.xcworkspace // Fails
$ ./script.swift ./script.swift:5:1: error: use of unresolved identifier 'ls' ls // Fails ^ ... etc ....
如果您不使用Swift代码中的命令输出,则只需执行以下操作:
#!/usr/bin/env swift import Foundation @discardableResult func shell(_ args: String...) -> Int32 { let task = Process() task.launchPath = "/usr/bin/env" task.arguments = args task.launch() task.waitUntilExit() return task.terminationStatus } shell("ls") shell("xcodebuild", "-workspace", "myApp.xcworkspace")
更新:对于Swift3 / Xcode8