【问题标题】:Shell script with NSTask doesn't work带有 NSTask 的 Shell 脚本不起作用
【发布时间】:2015-07-14 12:43:27
【问题描述】:

我有一个命令 (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

有什么想法吗? :(

【问题讨论】:

    标签: xcode macos xcodebuild nstask


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你想要完成什么? 您的archiveCommand 变量从未使用过,第二个 NSTask 使用外部 sh 文件初始化。

      无论如何,我认为您正在尝试在操作结束之前读取管道;我认为在启动命令之前的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];
      

      【讨论】:

        猜你喜欢
        • 2020-12-24
        • 2018-05-31
        • 1970-01-01
        • 2013-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 1970-01-01
        相关资源
        最近更新 更多