使用NSTask的Shell脚本不起作用

时间:2015-07-14 12:43:28

标签: xcode macos xcodebuild nstask

我有一个命令(xcodebuild),如果我在终端尝试它的效果非常好。当我试着把它放在我的代码上时:

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

它不起作用并返回我:

** 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

我的script.sh是:

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

有什么想法吗? :(

2 个答案:

答案 0 :(得分:0)

经过多次尝试后,我发现如果我尝试使用Xcode运行应用程序的代码,它就无法运行。如果我构建mac应用程序......一切正常!

答案 1 :(得分:0)

你想要完成什么? 永远不会使用archiveCommand变量,而是使用外部sh文件初始化第二个NSTask。

无论如何,我认为你试图在操作结束前读取管道;我认为在启动命令之前的waitUntilExit方法应该有帮助(或者至少使用终止处理程序)。 这是一个包含问题相关部分的测试代码;它应该可以正常工作(我已经在ObjC中写了它,但翻译非常简单。)

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];