[英]Running terminal commands from Cocoa App in Swift failing: “command not found” but works using Command Line Tool in swift
[英]Run multiple terminal commands using Swift
我想在 macOS 应用程序中一个接一个地执行两个命令。 例如cd ~/Desktop/MyiOSProject
和 'fastlane releaseBeta'
我已经在下面的方法中实现了命令行执行。 当我调用波纹管方法command("cd ~/Desktop/MyiOSProject && fastlane releaseBeta")
它会引发我的错误
env: cd ~/Desktop/MyiOSProject && fastlane releaseBeta: 没有那个文件或目录
func command(args: String..., completion: @escaping (Bool,String, Process)->Void) {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
let outPipe = Pipe()
task.standardOutput = outPipe
task.launch()
task.terminationHandler = {process in
let outdata = outPipe.fileHandleForReading.readDataToEndOfFile()
if var string = String(data: outdata, encoding: .utf8) {
string = string.trimmingCharacters(in: .newlines)
if process.terminationStatus == 0 {
completion(true, string, process)
}else {
completion(false, string, process)
}
return
}
completion(false, "Failed to run command", process)
}
}
在Process
实例中,您没有与终端内相同的环境。 IE。 搜索路径不同——尽管您使用/usr/bin/env
。 我怀疑找不到fastlane
命令。 如果是这样,解决方案很简单:指定它的完整路径,即/usr/local/bin/fastlane
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.