0

I have a command (xcodebuild) that if I try in terminal it works very well. When I try to put it on my code:

let xcodeProjectPath = "/Users/xxx/Desktop/Code/xxx.xcworkspace"
let xcodeArchivePath = "/Users/xxx/Desktop/xxx.xcarchive"
let schemeName = "XXX"

let pid = NSProcessInfo.processInfo().processIdentifier
let pipe: NSPipe = NSPipe()

let file: NSFileHandle = pipe.fileHandleForReading

let archiveCommand = "xcodebuild -scheme \(schemeName) -workspace \(xcodeProjectPath) -configuration Release -archivePath \(xcodeArchivePath) archive"

let task = NSTask()
task.launchPath = "/bin/sh"
task.arguments = NSArray(objects: "-l", "/Users/xxx/Desktop/test.sh") as [AnyObject]

task.standardInput = NSPipe()
task.standardOutput = pipe
task.launch()

let data = file.readDataToEndOfFile()
file.closeFile()

let grepOutput = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Returned: \(grepOutput!)")

It doesn't work and return me:

** ARCHIVE FAILED **
The following build commands failed:
    DataModelVersionCompile /Users/xxx/Library/Developer/Xcode/DerivedData/XXX-fbrisxgdcevajabbkhkejvwjrxyt/Build/Intermediates/ArchiveIntermediates/XXX/InstallationBuildProductsLocation/Applications/XXX.app/Database.momd Application/Resources/Database/Database.xcdatamodeld

My script.sh is:

#!/bin/sh
xcodebuild -scheme XXX -workspace   /Users/xxx/Desktop/Codice/xxx.xcworkspace -configuration Release -archivePath /Users/xxx/Desktop/provaBuild.xcarchive archive

Any idea? :(

2 Answers2

0

After many attempts I discover that if I try my code running app with Xcode it doesn't works. If i build the mac app... everything works!

0

What you are trying to accomplish? You archiveCommand variable is never used and the second NSTask is initialized with an external sh file.

Anyway I think you are trying to read the pipe before the operation ends; I think waitUntilExit method just before launch command should help (or at least use the termination handler). This is a test code with the relevant part of your question; it should works fine (I've written it in ObjC but the translation is very straightforward).

NSArray *args = @[ @"-scheme",schemePath,@"-workspace",prjPath,@"-configuration",@"Release",@"-archivePath",archPath,@"archive"];

[task setArguments:args];
NSPipe *outputPipe = [NSPipe pipe];
[task setStandardOutput:outputPipe];

[task setTerminationHandler:^(NSTask *task) {
    NSFileHandle * read = [outputPipe fileHandleForReading];
    NSData * dataRead = [read readDataToEndOfFile];
    NSString * stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
    NSLog(@"output: %@", stringRead);
}];
[task launch];
danielemm
  • 1,636
  • 1
  • 14
  • 24