LoginSignup
4
0

More than 3 years have passed since last update.

第8回(rake)

Last updated at Posted at 2020-12-07

Mac OS X-10.15.7 ruby-2.7.1p83

rakeとは

rakeはmakeやantのruby版. 典型的な例は,

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

Rakefileが用意しているtask(仕事)が表示される.これはdefaultでrake -Tというtaskの一覧を表示するコマンドを実行するようにしているから.

Rakefileの中身を詳しく見ていくと
- descの後にはrake -Tとした時の説明文(description)
- :defaultはrakeが引数なしで呼ばれた時の動作,
- それ以外はoptionで呼ばれるtaskの名前
- taskの中には標準のruby言語で,動作を記述

となっている.':'で始まる変数はsymbolと呼ばれるクラスで,名前の代わりをしてくれるらしい.

system call

rakeを使ってgit pushの一連の流れをやってみる

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

これで一連の流れ全てをgit pushだけで行ってくれるようになる.ただcommitのコメントが全部hogeになるので実際に使うならもう少し書き直す必要がある.


  • source ~/grad_members_20f/members/yoshida/c04_rake.org
4
0
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
4
0