LoginSignup
3

More than 5 years have passed since last update.

hackscodeでXcodeをちょっと便利にする

Last updated at Posted at 2018-07-09

hackscode

最近ちょっとしたツールを書きました。
https://github.com/toshi0383/hackscode

今後色々便利コマンドを追加していきたいと思っていますが、今のところ下記の1つだけです。

remove-build-file

以下のようにすると、 MyViewModelSpec.swift 以外の *Spec.swift をビルド対象から除くことができます。

$ hackscode remove-build-files --from-target Tests --matching Spec.swift --excluding MyViewModelSpec.swift

Build Phase(PBXBuildFile)から除いているだけなので、Xcode上でファイルは編集できるというのがポイントです。
このようにターゲットのチェックが外れた状態になります。
Screen Shot 2018-07-09 at 19.13.30.png

Swiftのコンパイルはとても時間がかかるので、大きなコードベースでテストケース1つだけを直すような時に使うと便利です。

しかしこれをコマンドラインで毎回指定するのも、ちょっと長くて面倒ですよね?そこで、AppleScriptの出番です。

#!/usr/bin/osascript

use AppleScript version "2.4" # Yosemite or later
use scripting additions
use framework "Foundation"

on run
    tell application "Xcode"
        set projectPath to path of active workspace document
        set projectFolder to characters 1 thru -((offset of "/" in (reverse of items of projectPath as string)) + 1) of projectPath as string
        set sourceName1 to (get name of window 1)

        try
            do shell script "hackscode remove-build-files --from-target AbemaTVTests --matching Spec.swift --excluding " & sourceName1 & " --project-root " & projectFolder
            display notification "👌 Ignored all specs except " & sourceName1 with title "Hackscode"
        on error errStr number errorNumber
            display notification "🛑 " & errStr & " (" & errorNumber & ")"
        end try
    end tell
end run

このコードを適当なファイル名で保存して、以下のようにXcodeの設定 => Behaviorsに追加します。

Screen Shot 2018-07-09 at 18.46.59.png

こうすると、ショートカット1発で、現在開いているファイルを --excluding に指定して実行することができます。今日もこれ何度か使ったのですが結構便利です。

pbxprojの編集には、XcodeGenでも使われているxcodeprojを使っています。

トラブルシュート

コマンドラインと違って、XcodeのBehavior経由だと PATH が通っていなかったり、カレントディレクトリがプロジェクトルートになっていない場合があります。
そんな時は、コマンドラインからXcode.appを開き直すとうまくいくようになるかもしれません。

$ cd project_root/ # App.xcodeprojのあるディレクトリに移動
$ open -a /Applications/Xcode.app App.xcodeproj

なぜXcode Editor Extensionではないのか?

Xcode Editor Extensionも試したのですが、以下の理由でやめました。
- 開いているファイル名が取得できない(致命的。。)
- インストールが面倒かつ期待通り変更が反映されなかった
- AppCodeやvimユーザが使えない

Editorのメニューに出てくるのはとても便利なんですけどね。そこだけ使わせて欲しい😅

Xcode Editor Extensionについてはこちらのスライドが参考になると思います。
https://speakerdeck.com/takasek/20170916-number-iosdc

まとめ

以上久しぶりにちょっと記事を書いてみました。
Xcodeは長く使っている割に使いこなせていない部分も結構ある気がしているので、他にもこういうコマンドあったら便利そうとかあれば教えて欲しいです🙏

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3