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

Rake::Task#enhance利用時、タスク間の依存関係を知りたい

Last updated at Posted at 2019-11-21

はじめに

 Rakeタスクをフックする際、よくRake::Task#enhanceが利用されます。この際、タスク間の依存関係、フックしたタスクのパラメータ、起動タスク名を知りたかったのですが、軽くググった限りでは出てこなかったので備忘も兼ねて残します。

環境

バージョン
OS macOS 10.14.6
Ruby 2.6.3
Rake 13.0.1

サンプル

Rakefile
namespace :sample do
  desc '孫娘タスク前に実行されるタスク'
  task :before_daughter do |_, args|
    hooked_args = args.instance_variable_get('@parent')
    puts "(before_daughter) args[:name]:'#{args[:name]}', 2nd_param:'#{hooked_args.to_a[1]}'"
  end

  desc '孫娘 タスク(孫息子タスクから呼ばれる)'
  task :grand_daughter, :name do |task, args|
    puts "(grand_daughter)  args[:name]:'#{args[:name]}', 2nd_param:'#{args.to_a[1]}'"
    puts "(grand_daughter)  before:#{task.prerequisites}, root:#{Rake.application.top_level_tasks}"
  end

  desc '孫息子 タスク(起動タスク)'
  task :grand_son do |task|
    Rake::Task['sample:grand_daughter'].invoke("invoked by #{task.name}", 'hogefuga')
    puts "(grand_son)       before:#{task.prerequisites}, root:#{Rake.application.top_level_tasks}"
  end

  desc '子タスク'
  task :child do |task|
    puts "(child)           before:#{task.prerequisites}, root:#{Rake.application.top_level_tasks}"
  end

  desc '父タスク'
  task :father do |task|
    puts "(father)          before:#{task.prerequisites}, root:#{Rake.application.top_level_tasks}"
  end

  desc '母タスク'
  task :mother do |task|
    puts "(mother)          before:#{task.prerequisites}, root:#{Rake.application.top_level_tasks}"
  end

  # before_daughter -> grand_daughter
  Rake::Task['sample:grand_daughter'].enhance(['sample:before_daughter'])
  # child -> grand_son
  Rake::Task['sample:grand_son'].enhance(['sample:child'])
  # father, mother -> child
  Rake::Task['sample:child'].enhance(%w[sample:father sample:mother])
end

結果

$ rake sample:grand_son
=> Executing Rake sample:grand_son ...
(father)          before:[], root:["sample:grand_son"]
(mother)          before:[], root:["sample:grand_son"]
(child)           before:["sample:father", "sample:mother"], root:["sample:grand_son"]
(before_daughter) args[:name]:'invoked by sample:grand_son', 2nd_param:'hogefuga'
(grand_daughter)  args[:name]:'invoked by sample:grand_son', 2nd_param:'hogefuga'
(grand_daughter)  before:["sample:before_daughter"], root:["sample:grand_son"]
(grand_son)       before:["sample:child"], root:["sample:grand_son"]

結論

  • Rake::Task#enhanceで設定したタスク名はRake::Task#prerequisitesに保存される
  • 起動タスク名は、どこからでもRake.application.top_level_tasksで参照できる
  • Rake::Task#invokeで渡したパラメータは、フックしたタスクでも参照できる
    • 名前付きパラメータは、特に意識せずに取得できる
    • 名前なしパラメータは、args.instance_variable_get('@parent')で直接参照するしかない
      • Rake::RakeArgument#parentに、フックしたタスクのパラメータが格納されている
2
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
2
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?