Using rake import (calling other rakefiles)

随声附和 提交于 2020-01-23 06:26:49

问题


Here's my primary rake file

subrake = 'subrake'

task :init => [subrake] do
  #call subrake.build
end

import subrake

I see documentation on how the above steps work, but I can't figure out how to call tasks in the other subrake file. BTW, those tasks may have the same name as mine, is this an issue?


回答1:


I guess I'm late with my answer, but I had the same question just few moments ago. So the solution might be useful for someone.

Rakefile.rb

subrake = 'subrake'

task :default => :init

task :init => ["#{subrake}:init"] do
  Rake::Task["#{subrake}:build"].invoke
end

require "#{Dir.pwd}/#{subrake}"

subrake.rb

namespace :subrake do

  desc "Init"
  task :init do
    puts 'Init called'
  end

  desc "Build"
  task :build do
    puts 'Build called'
  end

end

I guess the code describes itself just good, but I want to stop on one moment. When you are calling require, you should provide for a subrake file a full path (like in my sample) or '.\subrake' (if it is in a working directory)




回答2:


If you have more than one sub rake files you could do something like

Dir.glob('**/*.rake').each { |r| import r}

task :init => ["subrake:init"] 

and you can have a sub.rake files that looks like so

namespace :subrake do
  desc "Init"
  task :init do
    puts 'Init called'
  end
end

and another foo.rake file

namespace :foorake do
  desc "Init"
  task :init do
    puts 'Init called'
  end
end

and you can call foorake:init from the shell like so

#rake foorake:init

or add it to you main do task like so

task :init => ["subrake:init", "fforake:init"]


来源:https://stackoverflow.com/questions/4505996/using-rake-import-calling-other-rakefiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!