{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/how-to-code-swiftlint-run-script","result":{"data":{"markdownRemark":{"id":"90fd8cd1-81ec-5cb6-9bd6-6825b0c3f44a","html":"<p>SwiftLint をビルド時に Run Script で実行する設定は、様々な記事で紹介されています。</p>\n<p>しかし、微妙に間違っていたり、シェルスクリプトをよく書く自分からすると、もっとこう書いた方がいいと思ったりする点があったりします。</p>\n<p>この記事では、あまりシェルスクリプトに慣れていない iOS エンジニアの方に向けて、SwiftLint の Run Script を通してシェルスクリプトのコツを解説します。</p>\n<h2 id=\"結論\" style=\"position:relative;\"><a href=\"#%E7%B5%90%E8%AB%96\" aria-label=\"結論 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>結論</h2>\n<p>最初に SwiftLint の Run Script の書き方の結論を載せます。</p>\n<h3 id=\"swiftlint-を-cocoapods-でインストールしている場合\" style=\"position:relative;\"><a href=\"#swiftlint-%E3%82%92-cocoapods-%E3%81%A7%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%97%E3%81%A6%E3%81%84%E3%82%8B%E5%A0%B4%E5%90%88\" aria-label=\"swiftlint を cocoapods でインストールしている場合 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SwiftLint を CocoaPods でインストールしている場合</h3>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">SWIFT_LINT=&quot;${PODS_ROOT}/SwiftLint/swiftlint&quot;\n\nif which &quot;${SWIFT_LINT}&quot; &gt;/dev/null; then\n  &quot;${SWIFT_LINT}&quot; autocorrect --format\n  &quot;${SWIFT_LINT}&quot;\nelse\n  echo &quot;warning: SwiftLint not installed.&quot;\nfi</code></pre></div>\n<h3 id=\"swiftlint-を-homebrew-などで-mac-にインストールしている場合-非推奨\" style=\"position:relative;\"><a href=\"#swiftlint-%E3%82%92-homebrew-%E3%81%AA%E3%81%A9%E3%81%A7-mac-%E3%81%AB%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%97%E3%81%A6%E3%81%84%E3%82%8B%E5%A0%B4%E5%90%88-%E9%9D%9E%E6%8E%A8%E5%A5%A8\" aria-label=\"swiftlint を homebrew などで mac にインストールしている場合 非推奨 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SwiftLint を Homebrew などで Mac にインストールしている場合 (非推奨)</h3>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">SWIFT_LINT=&quot;swiftlint&quot;\n\nif which &quot;${SWIFT_LINT}&quot; &gt;/dev/null; then\n  &quot;${SWIFT_LINT}&quot; autocorrect --format\n  &quot;${SWIFT_LINT}&quot;\nelse\n  echo &quot;warning: SwiftLint not installed.&quot;\nfi</code></pre></div>\n<h2 id=\"解説\" style=\"position:relative;\"><a href=\"#%E8%A7%A3%E8%AA%AC\" aria-label=\"解説 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>解説</h2>\n<p>シェルスクリプトはなかなか癖のある文法をしているので、このコードの意味・意図を解説していきます。</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">SWIFT_LINT=&quot;${PODS_ROOT}/SwiftLint/swiftlint&quot;\n\nif which &quot;${SWIFT_LINT}&quot; &gt;/dev/null; then\n  &quot;${SWIFT_LINT}&quot; autocorrect --format\n  &quot;${SWIFT_LINT}&quot;\nelse\n  echo &quot;warning: SwiftLint not installed.&quot;\nfi</code></pre></div>\n<h3 id=\"swiftlint-の有無による分岐\" style=\"position:relative;\"><a href=\"#swiftlint-%E3%81%AE%E6%9C%89%E7%84%A1%E3%81%AB%E3%82%88%E3%82%8B%E5%88%86%E5%B2%90\" aria-label=\"swiftlint の有無による分岐 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SwiftLint の有無による分岐</h3>\n<p><code class=\"language-text\">if which &quot;${SWIFT_LINT}&quot; &gt;/dev/null; then</code></p>\n<p>シェルスクリプトに慣れていない方にとって、一番分かりにくいのはこの部分だと思います。\n雰囲気的に SwiftLint があるかどうかで分岐していることは分かると思いますが、ここをちゃんと解説してみます。</p>\n<p>まず、この部分は <code class=\"language-text\">if</code> と <code class=\"language-text\">which &quot;${SWIFT_LINT}&quot; &gt;/dev/null</code> と <code class=\"language-text\">;then</code> に分けて読む必要があります。\n単純な話ですが、 <code class=\"language-text\">which &quot;${SWIFT_LINT}&quot; &gt;/dev/null</code> の部分が <code class=\"language-text\">if</code> で条件分岐するための判定です。</p>\n<p><code class=\"language-text\">which &quot;${SWIFT_LINT}&quot; &gt;/dev/null</code> は何をしているかと言うと、<code class=\"language-text\">which &quot;${SWIFT_LINT}&quot;</code> コマンドを実行し、その際に画面 (標準出力) に表示される内容を捨てています。\n<code class=\"language-text\">which &quot;${SWIFT_LINT}&quot;</code> コマンドは、SwiftLint の有無を調べるコマンドですが、なぜその結果を捨てているのでしょうか ?</p>\n<p>その理由は、シェルスクリプトの if が表示内容ではなく、終了コードを見ていることにあります。</p>\n<p>which コマンドは指定したコマンドが見つかったか場合は終了コード 0 を、見つからなかった場合は終了コード 1 を返します。\nif はその値を見て分岐しているのです。</p>\n<h3 id=\"swift_lint-変数の定義\" style=\"position:relative;\"><a href=\"#swift_lint-%E5%A4%89%E6%95%B0%E3%81%AE%E5%AE%9A%E7%BE%A9\" aria-label=\"swift_lint 変数の定義 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SWIFT_LINT 変数の定義</h3>\n<p>ざっと調べたところ全く見かけなかったのですが、<code class=\"language-text\">${PODS_ROOT}/SwiftLint/swiftlint</code> という部分は上記のように変数に SWIFT_LINT のような変数にまとめた方が良いです。\n理由はもちろん、使用する swiftlint コマンドのパスが変わった際に、1 箇所の修正で済むようにするためです。</p>\n<p>これはシェルスクリプトではよく見かけるテクニックです。</p>\n<h3 id=\"-の挙動\" style=\"position:relative;\"><a href=\"#-%E3%81%AE%E6%8C%99%E5%8B%95\" aria-label=\" の挙動 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">&quot;&quot;</code> の挙動</h3>\n<p>SWIFT_LINT という変数を使用する際、それを <code class=\"language-text\">&quot;&quot;</code> で括っています。\n<code class=\"language-text\">&quot;&quot;</code> で括った部分は、シェルスクリプトでは 1 つの文字列として扱われます。</p>\n<p>なので、もし <code class=\"language-text\">&quot;${SWIFT_LINT}&quot; autocorrect --format</code> の部分を <code class=\"language-text\">&quot;${SWIFT_LINT} autocorrect --format&quot;</code> のように書いてしまうと、<code class=\"language-text\">${SWIFT_LINT} autocorrect --format</code> という空白を含んだコマンドを実行しようとしてしまい、正しい結果が得られません。\n<code class=\"language-text\">&quot;&quot;</code> でどこまで括るべきかは注意しましょう。</p>\n<p>ちなみに <code class=\"language-text\">${}</code> で変数を呼び出す箇所を <code class=\"language-text\">&quot;&quot;</code> で括ることは、Google の <a href=\"https://google.github.io/styleguide/shell.xml\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Shell Style Guide</a> で推奨されています。</p>\n<h3 id=\"swiftlint-を-homebrew-でインストールすべきでない理由\" style=\"position:relative;\"><a href=\"#swiftlint-%E3%82%92-homebrew-%E3%81%A7%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%81%B9%E3%81%8D%E3%81%A7%E3%81%AA%E3%81%84%E7%90%86%E7%94%B1\" aria-label=\"swiftlint を homebrew でインストールすべきでない理由 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SwiftLint を Homebrew でインストールすべきでない理由</h3>\n<p>最後に、SwiftLint を Homebrew でインストールするケースを非推奨としている理由ですが、これは SwiftLint のバージョンが開発者間で統一されなくなってしまうためです。</p>\n<p>インフラ系の仕事をする機会が多い自分にとって環境間のバージョン違いをなくすことは自然なことなのですが、こちらも実施していない記事が多いです。</p>\n<p>このことについては「<a href=\"https://shinkufencer.hateblo.jp/entry/2018/08/09/230406\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iOSアプリを開発する人はもっとRubyとgemの管理に慎重になって、かつ有効に使って欲しいという気持ちの記事を書いた。</a>」というブログ記事とそのリンク先で、なぜそうすべきか、具体的にどう設定すればいいのか、が解説されています。</p>\n<h2 id=\"おわりに\" style=\"position:relative;\"><a href=\"#%E3%81%8A%E3%82%8F%E3%82%8A%E3%81%AB\" aria-label=\"おわりに permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>おわりに</h2>\n<p>先日参加した Swift の勉強会で色々初心者的な質問をさせていただいたので、この記事の内容のように、自分が比較的得意な分野 (シェルスクリプト) で Swift のコミュニティに貢献できればと思います。</p>\n<h2 id=\"シェルスクリプトのすすめ\" style=\"position:relative;\"><a href=\"#%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E3%81%99%E3%81%99%E3%82%81\" aria-label=\"シェルスクリプトのすすめ permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>シェルスクリプトのすすめ</h2>\n<p>シェルスクリプトは非常に便利ですし、学ぶ価値は非常に高いです。\n興味を持った方が勉強する最初の一冊にはこちらの本がオススメです。</p>\n<ul>\n<li><a href=\"https://amzn.to/3o0FvE3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">新しいLinuxの教科書</a></li>\n</ul>\n<p><a href=\"https://www.amazon.co.jp/dp/4797380942/ref=as_li_ss_il?ie=UTF8&linkCode=li1&tag=oshimayuki0d-22&linkId=983333c04fab081603df17da6c816935\" target=\"_blank\"><img border=\"0\" src=\"//ws-fe.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=4797380942&Format=_SL110_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=oshimayuki0d-22\" ></a><img src=\"https://ir-jp.amazon-adsystem.com/e/ir?t=oshimayuki0d-22&amp;l=li1&amp;o=9&amp;a=4797380942\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\"></p>","fields":{"slug":"/posts/how-to-code-swiftlint-run-script","tagSlugs":["/tag/ios/","/tag/swift/","/tag/bash/"],"autoRecommendPosts":["udemy-crafting-shell","what-is-executing-a-command-on-bash","udemy-linux-reintroduction","macos-virtualization"]},"frontmatter":{"date":"2020-02-02T14:13:00.000Z","description":"SwiftLint をビルド時に Run Script で実行する設定は、様々な記事で紹介されています。\nしかし、微妙に間違っていたり、シェルスクリプトをよく書く自分からすると、もっとこう書いた方がいいと思ったりする点があったりします。\nこの記事では、あまりシェルスクリプトに慣れていない iOS エンジニアの方に向けて、SwiftLint の Run Script を通してシェルスクリプトのコツを解説します。","tags":["ios","swift","bash"],"title":"SwiftLint の Run Script は結局どう書くのが適切なのか","socialImage":null,"recommendPosts":["https://www.kanzennirikaisita.com/posts/software-developer-books-index"]}}},"pageContext":{"slug":"/posts/how-to-code-swiftlint-run-script"}},"staticQueryHashes":["251939775","3942705351","401334301"]}