2016-09-09 7 views
9

私の目標は、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パラメーターを呼び出そうとしたためです。

他のフォルダへのアクセスをどのように有効にできますか?私が有効にする必要のあるサンドボックス環境設定がありますか?

答えて

1

私はサンドボックス化のためこれは不可能だと思います。 clang形式の実行ファイルをバンドルし、そこから使用することができます。

0

個人的には、あなたはそれがすべて間違っていると思います。拡張機能はすばやく用意されています(Xcode拡張機能でビデオを見ると、何度も繰り返して取り込み、取り出します)。そして、彼らは厳しく制限されています。

しかし、コンテナアプリケーションでは、すべてのハッキングなしでこの処理を拡張機能に対して実行できる場合があります。欠点は、バッファを拡張モジュールとの間でやりとりする必要があることです。

これは簡単ではありませんが、実行できます。あなたのコンテナを実行するために簡単なピースの方法。まず、コンテナアプリケーションのInfo.plist(Info.plist拡張子ではない)を変更して、URLタイプを持つようにします。あなたの拡張機能で

Info.plist

は、次の操作を実行して、コンテナアプリを「ウェイクアップ」することができます

let customurl = NSURL.init(string: “yoururlschemehere://") 
NSWorkspace.shared().open(customurl as! URL) 

2間の通信用として、Appleは方法の茄多を持っています。私は昔の学校なので、今はDistributedNotificationCenterを使っています。

私はそれを試していませんが、なぜコンテナアプリがclangとチャットしているのかわかりません(私は設定のためにコンテナアプリを使用しています)。

+0

恐ろしい、ありがとう!私は試してみましょう:) –

+0

これがうまくいくなら、それを受け入れられた答えとしてマークしてください。 –

関連する問題