使用Swift运行多个终端命令

问题描述 投票:1回答:1

我想在macOS Application中一个接一个地执行两个命令。例如cd ~/Desktop/MyiOSProject和'fastlane releaseBeta'

我在下面的方法中实现了命令行执行。当我调用波纹管方法command("cd ~/Desktop/MyiOSProject && fastlane releaseBeta")时它会抛出我的错误

环境: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)
        }
    }
swift macos terminal fastlane
1个回答
1
投票

Process实例中,您没有与终端内部相同的环境。 I. e。搜索路径是不同的 - 尽管你使用/usr/bin/env。我怀疑无法找到fastlane命令。如果是这样,解决方案很简单:指定它的完整路径,即。即/usr/local/bin/fastlane

© www.soinside.com 2019 - 2024. All rights reserved.