0

I am building an installer for an OS X application in Swift 2.0. The installer does little more than take user input, sends it to a server, and builds a desktop shortcut determined by server response. This shortcut (the 'application') opens up another application (FileMaker). As part of the installation process I am installing FileMaker silently using NSTask which runs a shell script executing installer.app on the FileMaker .pkg. This works fine.

I need to determine whether this installation is successful or not in order to progress to the next step in the installer. I can easily get the string response from the terminal, ie "installer: The install was successful," but I dont feel a hard coded string condition is robust enough. Any other possibilities?

n.b. I'm a beginner Swift developer (1 week!) and have only a year of Web Development behind that. ie. I'm blindingly green.

P.S. Ideally I'd be displaying a progress indicator for the FileMaker installation rather than a message, but that'd be overextending myself (further) if it's even possible.

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")
}

Runs shell command

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))
            }
        }
    }

}
richardjc
  • 89
  • 7
  • 1
    If the `FileMaker` installer returns an exit code that reports the success or failure of the install, then you can make use of that. `NSTask` allows you to obtain the exit code of the process you launched. – Cristik Jan 12 '16 at 23:46
  • Thanks Cristik. How would I determine if an exit code is returned? If I run the content of the script direct into the terminal I receive only a success message, no (more reliable) codes. a -verbose installation isn't any more detailed. Is this the only place such a code would be returned? – richardjc Jan 13 '16 at 23:38

0 Answers0