1
0

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 1 year has passed since last update.

【Rails】rakeコマンドで定型作業をコマンド化する

Posted at

Rubyのrakeコマンドの基礎についてまとめてみました。

rakeコマンドとは

rakeコマンドはタスクを実行するためのタスクランナーで、実行する処理をRakeタスクと呼ぶ。主にリリース作業などの定型処理をRakeタスクとして定義する。
DBの管理を行ったり、独自のタスクを作成することができる。

タスクの作成方法

'Hello, Rake!'が出力されるhelloタスクを作成する

Rakefile
desc 'Hello, Rake Task'
task :hello do
    puts 'Hello, Rake!'
end
用途
desc '...' タスクの一覧を表示する際に、どのようなタスクか説明するための記述
task :task_name do ... end タスクの名称と実行する内容
タスク一覧の表示
% rake -T
rake hello # Hello,Rake Task

rakeコマンドはカレントディレクトリ内のRakefileというファイルを参照する。
もし別のファイル名で記述されたRakeタスクを読み込む場合は-fオプションで明示する必要がある。

タスクの実行

% rake hello
Hello,Rake!

想定通りに文字列が出力される。

Railsで標準で用意されたRakeタスク

Railsでは標準で様々なRakeタスクが用意されており、データベースの作成からテストの実行などの処理を自動化できるようになっている。

> rails -T
rails about                              # List versions of all...
rails action_mailbox:ingress:exim        # Relay an inbound ema...
rails action_mailbox:ingress:postfix     # Relay an inbound ema...
rails action_mailbox:ingress:qmail       # Relay an inbound ema...
rails action_mailbox:install             # Installs Action Mail...
rails action_mailbox:install:migrations  # Copy migrations from...
rails action_text:install                # Copy over the migrat...
rails action_text:install:migrations     # Copy migrations from...
rails active_storage:install             # Copy over the migrat...
rails app:template                       # Applies the template...
rails app:update                         # Update configs and s...
rails assets:clean[keep]                 # Remove old compiled ...
rails assets:clobber                     # Remove compiled assets
rails assets:environment                 # Load asset compile e...
rails assets:precompile                  # Compile all the asse...
rails cache_digests:dependencies         # Lookup first-level d...
rails cache_digests:nested_dependencies  # Lookup nested depend...
rails db:create                          # Creates the database...
rails db:drop                            # Drops the database f...
rails db:encryption:init                 # Generate a set of ke...
rails db:environment:set                 # Set the environment ...
rails db:fixtures:load                   # Loads fixtures into ...
rails db:migrate
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?