Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call Rake tasks that are defined in the standard Rakefile from an other Ruby script?
    text
    copied!<p>Is it possible to call a task which is defined in a <code>Rakefile</code> - not in <code>somefile.rake</code> - from an other Ruby script?</p> <p>I was hoping that creating a new <code>Rake::Application</code> would automatically load the <code>Rakefile</code> from the same directory, but it seems that this is not the case. Here is what I came up with so far:</p> <pre><code>$LOAD_PATH.unshift File.dirname(__FILE__) require 'rake' require 'pp' rake = Rake::Application.new rake[:hello].invoke </code></pre> <p>Executing this code results in the following:</p> <pre><code>/opt/ruby/1.9.2-p180/lib/ruby/1.9.1/rake.rb:1720:in `[]': Don't know how to build task 'hello' (RuntimeError) from script.rb:7:in `&lt;main&gt;' </code></pre> <p><code>pp rake</code> yields the following:</p> <pre><code>#&lt;Rake::Application:0x00000101118da0 @default_loader=#&lt;Rake::DefaultLoader:0x00000101118b20&gt;, @imported=[], @last_description=nil, @loaders= {".rb"=&gt;#&lt;Rake::DefaultLoader:0x00000101118a80&gt;, ".rf"=&gt;#&lt;Rake::DefaultLoader:0x000001011189b8&gt;, ".rake"=&gt;#&lt;Rake::DefaultLoader:0x00000101118800&gt;}, @name="rake", @original_dir= "/Users/t6d/Projects/Sandbox/Ruby/rake-from-ruby-script", @pending_imports=[], @rakefile=nil, @rakefiles=["rakefile", "Rakefile", "rakefile.rb", "Rakefile.rb"], @rules=[], @scope=[], @tasks={}, @top_level_tasks=[], @tty_output=false&gt; </code></pre> <p>It is somehow irritating that <code>@rakefile</code> is <code>nil</code>.</p> <p><em>Update May 20th, 4:40pm CET</em></p> <p>After reading the rake source code for a little while, I figured out that you need to call Rake::Application#init in order to initialize your newly created rake application:</p> <pre><code>rake = Rake::Application.new rake.init rake.load_rakefile </code></pre> <p>However, I still cannot invoke any tasks defined in my Rakefile:</p> <pre><code>rake.top_level_tasks # =&gt; ["default"] </code></pre> <p>I'd gladly appreciate any help on that matter.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload