5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rakeを使って作業を自動化

Last updated at Posted at 2020-12-29

!macOS-11.1 !ruby-2.7.2p137

Preface (はじめに)

makeやantのRuby版としてrakeがあります. これはRakefileに記述に沿って自動で処理してくれます.

今回はGitの "add-commit-pull-push"までの処理を自動化してくれるRakefileを作って作業を楽にしたいと思います.

rake

Rakefileの基本

典型的なRakefileは,

Rakefile
task :default do
  system 'rake -T'
  exit
end

desc 'hello NAME'
task :hello do
  name = ARGV[1]
  puts "Hello #{name}!"
  exit
end

このRakefileで実行すると

> rake
-> rake hello # hello NAME


> rake hello Wor
Hello Wor!

見事に作業の自動化ができました.

Gitの自動化

今度はGitの"add ~ push"までの作業を自動化する.

まずは, Gitのpullを自動化します. systemコマンドを起動する関数systemを用いて,

Rakefile
desc 'git pull'
task :pull do
  p comm = "git pull origin main"
  system comm
  exit
end

以下のコマンドで実行できます.

> rake pull

次に, 今回の主目的であるGitの"add ~ push"までの作業を自動化するRakefile

Rakefile
desc 'git push'
task :push do
  p comm = "git add -A"
  system comm
  p comm = "git commit -m \'hoge\'"
  system comm
  p comm = "git pull origin main"
  system comm
  p comm = "git push origin main"
  system comm
  exit
end

ただ, このままでは大量の"hoge"というコミットメッセージのコミットが生まれてしまうので,

自分でコミットメッセージを変えれるようにします.

Rakefile
require 'colorize'
desc 'git push'
task :push do
  msg = ARGV[1]
  p comm = "git add -A"
  system comm
  comm = "git commit -m \'" + msg + "\'"
  puts comm.green
  system comm
  p comm = "git pull origin main"
  system comm
  p comm = "git push origin main"
  system comm
  exit
end

以下のコマンドで実行できます.

> rake push

参考資料

チャート式ruby-appendix-IV(rake)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?