获得成功结果是在NSTask Swift中执行安装程序pkg的shell脚本

时间:2016-01-12 23:37:56

标签: swift macos shell installer nstask

我正在为Swift 2.0中的OS X应用程序构建一个安装程序。安装程序只需接收用户输入,将其发送到服务器,并构建由服务器响应确定的桌面快捷方式。此快捷方式('应用程序')打开另一个应用程序(FileMaker)。作为安装过程的一部分,我使用NSTask静默安装FileMaker,它运行在FileMaker .pkg上执行installer.app的shell脚本。这很好。

我需要确定此安装是否成功才能进入安装程序的下一步。我可以很容易地从终端获得字符串响应,即"安装程序:安装成功,"但我觉得硬编码的字符串条件足够强大。还有其他可能性吗?

n.b。我是初学Swift开发人员(1周!),只有一年的Web开发。即。我是一个令人眼花缭乱的绿色。

P.S。理想情况下,我会显示FileMaker安装的进度指示器而不是消息,但是如果它甚至可能的话,那就是过度扩展自己(进一步)。

func installFileMaker() {

let fileMakerFileName = "FileMaker Pro 14"
let fileMakerDirectory = "Resources/FileMaker14_MacClient"

// get resource path
let bundle = NSBundle.mainBundle()
let resourcePathResult = bundle.pathForResource(fileMakerFileName, ofType: "pkg", inDirectory: fileMakerDirectory)

if let resourcePath = resourcePathResult {

    displayWebViewMessage("Installing Briefcase Now...")

    let command = "installer -pkg \"" + resourcePath + "\" -target /"
    Utilities.runAsCommandInBackground(command, callback: installUpdateWebviewCallback)

} else {
    // error
    displayWebViewMessage("Installation error")
}

print("rrr")
}

运行shell命令

static func runAsCommandInBackground(command: String, callback: (((success:Bool, message:String)) -> Void)?) {

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT

    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        let pipe = NSPipe()
        let task = NSTask()
        task.launchPath = "/bin/sh"
        task.arguments = ["-c", String(format:"%@", command)]
        task.standardOutput = pipe
        let file = pipe.fileHandleForReading
        task.launch()

        var result = ""
        if let tmpResult = NSString(data: file.readDataToEndOfFile(), encoding: NSUTF8StringEncoding) {
            result = tmpResult as String
        } else {
            // error
        }
        dispatch_async(dispatch_get_main_queue()) {
            print(result)
            // Give me a more robust result!!
            if let unwrappedCallback = callback {
                unwrappedCallback((true, result as String))
            }
        }
    }

}

0 个答案:

没有答案