LoginSignup
5
1

More than 5 years have passed since last update.

.gitignoreを自動生成するShell

Last updated at Posted at 2017-12-24

.gitignoreを自動生成するShell

はじめに

本文は記事HerokuにPuppeteerの実行環境を構築するの番外編です。
gitを使うにあたり、.gitignoreの作成がほぼ必須となっています。Railsやvue-cliなどScaffoldを作成する際は、割と意識しなくていいですが、自分が一から何か小さいプロジェクトを作る時はやっぱり書くことになってしまいます。

.gitignoreの自動生成

実際簡単です。https://www.gitignore.io/ にアクセスして、Createをクリックすれば、コピペで終わります。
それでも手間かかると思っていたので、より自動化したいと思いました。

やったこと

下記スクリプトを書きました。

gi.sh
#!/bin/bash

params=`echo $@ | tr ' ' ','`
# add most used os
params="windows,macos,linux,"$params

curl -L -s "https://www.gitignore.io/api/"$params > $PWD"/.gitignore"
echo ".gitignore File Created At:"
echo $PWD"/.gitignore"

使い方:
例えばnode.jsとrails両方対応の.gitignoreを作成したい場合、下記のコマンドでできます。

$ bash gi.sh node rails

簡単に説明してみると、gitignore.ioさんは、カンマ区切りのパラメータを認識してくれて、bash gi.sh node railsで叩くと、https://www.gitignore.io/api/node,rails,linux,macos,windowsにアクセスし、.gitignoreとしてコマンド実行時のフォルダーに書き込みます。

さらに自動化

それでも毎回gi.shをどっかから持ってくるのは面倒なので、Gistに投稿し、Rawgitで一般公開しました。

MacOSの環境だけテストしましたが、下記コマンドでこのShellをインストールすれば、次回からはgi node railsとかで一発作れます。

$ curl -L -s https://cdn.rawgit.com/jerrywdlee/7d70e765c56cd151c0c13fb11d1bdb3a/raw/b8df7028d7b317aaae113111d940d4b11d0e5e81/gi.sh > $HOME/.gi.sh
$ echo "alias gi=\"bash \$HOME/.gi.sh\"" >> ~/.bash_profile && source ~/.bash_profile
5
1
0

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
5
1