LoginSignup
3
2

More than 5 years have passed since last update.

【作業効率化】Laravelのartisanをカスタムしてgitコマンドを簡単にすると便利!

Posted at

はじめに

gitを使ってる方はCLIでcommiしたり、pushしたりしていると思いますが、毎度コマンドに打つの面倒ですよね。
独自にシェルスクリプトを書いても十分簡易化できますが、今回はLaravelのartisanの自作コマンドで1コマンドで完結する便利コマンドを作ってみましょう。

完成したコマンドでできること

  • git add ~ commit ~ pushが1コマンドで完了できる
  • pushした後、GitHubのcompare画面を開くことができる (オプション)

▼完成コマンド
$php artisan git:push 不要なクラスを削除 -c renewal-develop -s

▼実行されること
1. git add --all
2. git commit -m {コメント}
3. git push -u HEAD
4. compare renewal-develop..作業ブランチ画面を起動する

*オプションなどはこの後説明します!

早速作ろう!

1.コマンドクラスを作る

Laravelのプロジェクトフォルダにて以下のコマンドを実行
php artisan make:command GitCommand

2.コマンドを書く

▼before

GitCommnad.php
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

▼after

GitCommnad.php
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'git:push {comment} {--c|compare} {target=master} {--s|nostatus} {--i|issue=}';

引数1 : {comment}
→ git commit -m {コメント}の部分を引数とする

引数2 : {target=master}
→ compareのbase(マージ先)を指定する。指定しない場合はデフォルト値がmasterとなる。

OPTION(1) : {--c|compare}
→ compare画面を開く場合に指定する。 
(-cまたは--compareと入力できる)

OPTION(2) : {--s|nostatus}
→ はじめにgit statusを実行するか。オプションをつけるとgit statusと実行の確認をしないようになる。 
(-sまたは--nostatusと入力できる)

OPTION(3) : {--i|issue}
→ コメントにissue番号をつけたい時につける。 
(-iまたは--issueと入力できる)
*コメントに直接書けばいいのでなくてもいいオプション笑

3.引数を受け取れるようにする

getArguments() Methodを以下のようにオーバーライドします。

GitCommand.php
    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['comment', InputArgument::REQUIRED, 'Your git comment'],
            ['target', InputArgument::NOT_REQUIRED, 'Git Base Branch Name (merge target)'],
        ];
    }

4.オプションを受け取れるようにする

getOptions() Methodを以下のようにオーバーライドします。

GitCommand.php
    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
          ['nostatus', '-nostatus', InputOption::VALUE_OPTIONAL, 'Run without git status command'],
          ['issue', '-issue', InputOption::VALUE_OPTIONAL, 'GitHub issue no'],
          ['compare', '-compare', InputOption::VALUE_OPTIONAL, 'If you execute github compare, add this option'],
        ];
    }

5.実行処理を書く

以下の流れで実行するように実装します。

git status
 ↓
git add --all
 ↓
git commit-m {コメント}
 ↓
git push -u origin HEAD
 ↓
hub compare {target}..{HEAD:作業ブランチ}
 ↓
Finished!!

GitCommand.php
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
      if (!$this->option('nostatus')) {
        // git status
        $this->comment("Run: git status");
        $this->info(shell_exec("git status"));
        $confirmed = $this->confirm('Do you commit to all files?');
        if (! $confirmed) {
            $this->comment('Please remove untraced file and re-run this command.');
            return;
        }
      }

      // git add
      $this->comment("Run: git add --all");
      $this->info(shell_exec("git add --all"));

      // git commit
      $comment = trim($this->argument('comment'));
      if ($this->option('issue')) {
        $comment = "#".$this->option('issue')." ".$comment;
      }
      $this->comment("Run: git commit -m {your input comment}");
      $this->info(shell_exec('git commit -m "'.$comment.'"'));

      // git push
      $this->comment("Run: git push -u origin HEAD");
      $this->info(shell_exec('git push -u origin HEAD'));

      if (!$this->option('compare')) {
        $this->comment("Finished!!");
        return;
      }
      // Show github compare page.
      $current = shell_exec('git rev-parse --abbrev-ref HEAD');
      $base = $this->argument('target');
      $this->info(shell_exec('hub compare '.$base.'..'.$current));
      $this->comment("Finished!!");
    }

Note:
$this->option('hoge') → オプションの値を取ります。

$this->argument('hoge') → 引数の値を取ります。

$this->info(shell_exec('hoge')) → 任意のシェルコマンドを実行して、実行内容をコンソールに出力します。

細かいところは以下の記事を参考にしてください。
Laravelでコマンドラインアプリケーションを作成する
シェルスクリプトで現在のブランチ名を取得する(git rev-parse)

6. hubをインストールする

hub compareを動かすために、hubをインストールします。

完成!!

あとはもう好きにコマンドを実行して動かしてみましょう。

【ケース1】オプションを何も指定しないシンプルなコマンド
$php artisan git:push コメント
スクリーンショット 2019-03-03 17.58.12.png
git statusがされて、全部コミットOK?と確認をします。
不要なファイルがある場合はnoを入力、問題なければyesを入力してください。
スクリーンショット 2019-03-03 17.58.44.png

ちゃんと実行されました!!

【ケース2】git statusの確認は不要で即コミット&プッシュしたいパターン
$php artisan git:push コメント -s (または--nostatus)
スクリーンショット 2019-03-03 18.04.36.png

いい感じですね!!

【ケース3】プッシュした後、compare画面を開きたい
$php artisan git:push コメント -c -s(-cまたは--compare)
Mar-03-2019 18-15-03.gif

ブラウザが起動してGitのCompare画面が開きました!!
(尺が足りず表示できてませんが問題ないです)

【ケース4】プッシュした後、compare画面を開きたい(マージ先を指定)
$php artisan git:push コメント -c renewal-develop -s(-sは任意)

(キャプチャは割愛します)

終わりに

hubではプルリクエストの作成までできたりするので、工夫して作業をドンドン効率化して快適なプログラミングワークをしましょう!

記事作成者

NARI: twitter
フリーランスのエンジニアをしています。

この記事が少しでも参考になったらいいね、シェアされると嬉しいです!

3
2
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
3
2