Prompt instructions are only supported in the latest macOS Monterey Beta
1. Process is a class that can execute terminal commands
extension Process { struct Output { var pipe:Pipe var readData:String init(pipe:Pipe) { self.pipe = pipe self.readData = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8) ?? // launchPath: // launchPath: command path /// arguments: command parameter /// / -currentDirectorypath: Command execution directory /// -environment: environment variable /// -returns: Returns the results static func the executable (launchPath: String, the arguments: [String], currentDirectoryPath: String? = nil, environment:[String:String]? = nil)->Output{ let process = Process() process.launchPath = launchPath process.arguments = arguments if let environment = environment { process.environment = environment } if let currentDirectoryPath = currentDirectoryPath { process.currentDirectoryPath = currentDirectoryPath } let pipe = Pipe() process.standardOutput = pipe process.launch() return Output(pipe: pipe) } }Copy the code
2, in the preparation of a pod command to execute the code
var environment = [String:String]() environment["PATH"] = "/opt/MonkeyDev/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framewo rk/Versions/Current/Commands" environment["LANG"] = "en_US.UTF-8" environment["CP_HOME_DIR"] = NSHomeDirectory().appending("/.cocoapods") let output = Process.executable(launchPath: "/usr/local/bin/pod", arguments: [""], currentDirectoryPath: "", environment: environment) print(output.readData)Copy the code