Xcode 8扩展执行NSTask

Guy*_*gus 9 xcode nstask clang-format xcode8 xcode-extension

我的目标是创建一个执行clang格式的扩展.我的代码看起来像这样:

- (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError * _Nullable nilOrError))completionHandler
{
    NSError *error = nil;

    NSURL *executableURL = [[self class] executableURL];

    if (!executableURL)
    {
          NSString *errorDescription = [NSString stringWithFormat:@"Failed to find clang-format. Ensure it is installed at any of these locations\n%@", [[self class] clangFormatUrls]];
              completionHandler([NSError errorWithDomain:SourceEditorCommandErrorDomain
              code:1
              userInfo:@{NSLocalizedDescriptionKey: errorDescription}]);
          return;
    }

    NSMutableArray *args = [NSMutableArray array];
    [args addObject:@"-style=LLVM"];
    [args addObject:@"someFile.m"];
    NSPipe *outputPipe = [NSPipe pipe];
    NSPipe *errorPipe = [NSPipe pipe];

    NSTask *task = [[NSTask alloc] init];
    task.launchPath = executableURL.path;
    task.arguments = args;

    task.standardOutput = outputPipe;
    task.standardError = errorPipe;

    @try
    {
          [task launch];
    }
    @catch (NSException *exception)
    {
          completionHandler([NSError errorWithDomain:SourceEditorCommandErrorDomain
              code:2
              userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to run clang-format: %@", exception.reason]}]);
          return;
    }

    [task waitUntilExit];

    NSString *output = [[NSString alloc] initWithData:[[outputPipe fileHandleForReading] readDataToEndOfFile]
          encoding:NSUTF8StringEncoding];
    NSString *errorOutput = [[NSString alloc] initWithData:[[errorPipe fileHandleForReading] readDataToEndOfFile]
          encoding:NSUTF8StringEncoding];
    [[outputPipe fileHandleForReading] closeFile];
    [[errorPipe fileHandleForReading] closeFile];

    int status = [task terminationStatus];
    if (status == 0)
    {
          NSLog(@"Success: %@", output);
    }
    else
    {
          error = [NSError errorWithDomain:SourceEditorCommandErrorDomain
              code:3
              userInfo:@{NSLocalizedDescriptionKey: errorOutput}];
    }

    completionHandler(error);
}
Run Code Online (Sandbox Code Playgroud)

我需要try-catch块的原因是因为当我尝试运行此代码时抛出异常.例外原因是:

错误:无法访问启动路径

我的clang格式的路径是/ usr/local/bin/clang-format.我发现的是,它不喜欢我试图访问在/ usr/local/bin中的应用程序,但/斌是确定的(例如,如果我尝试执行/斌/ LS没有问题).

我尝试的另一个解决方案是通过设置启动路径和这样的参数来运行/ bin/bash:

task.launchPath = [[[NSProcessInfo processInfo] environment] objectForKey:@"SHELL"];
task.arguments = @[@"-l", @"-c", @"/usr/local/bin/clang-format -style=LLVM someFile.m"];
Run Code Online (Sandbox Code Playgroud)

这成功启动了任务,但失败并出现以下错误输出:

/ bin/bash:/ etc/profile:不允许操作/ bin/bash:/ usr/local/bin/clang-format:不允许操作

第一条错误消息是由于尝试在bash中调用-l参数,该参数尝试以用户身份登录.

知道如何启用对其他文件夹的访问吗?是否需要启用某种沙箱环境设置?

Joc*_*hen 2

我想由于沙箱的原因,这是不可能的。您可以捆绑 clang 格式的可执行文件并从那里使用它。