私の目標は、clang形式を実行する拡張機能を作成することです。Ncodeを実行するXcode 8の拡張
- (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);
}
私はこのコードを実行しようとすると、例外がスローされますので、try-catchブロックであることを必要とする理由:私のコードは次のようになります。例外の理由は次のとおりです。
Error: launch path not accessible
私のclang形式のパスは/ usr/local/bin/clang-formatです。私が発見したのは、/ usr/local/binにあるアプリケーションにアクセスしようとは思わないが、/ binは問題ありません(例えば、/ bin/lsを実行しようとすると問題はない)。
私が試した別の解決策は、次のように打ち上げパスと引数を設定することにより、/ binに/ bashのを実行することでした:
これは、タスクを正常に起動するが、それは、次のエラー出力で失敗します。
/bin/bash: /etc/profile: Operation not permitted /bin/bash: /usr/local/bin/clang-format: Operation not permitted
最初のエラーメッセージは、ユーザーとしてログインしようとしているbashで-lパラメーターを呼び出そうとしたためです。
他のフォルダへのアクセスをどのように有効にできますか?私が有効にする必要のあるサンドボックス環境設定がありますか?
恐ろしい、ありがとう!私は試してみましょう:) –
これがうまくいくなら、それを受け入れられた答えとしてマークしてください。 –