<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nealenssle.com &#187; rake</title>
	<atom:link href="http://nealenssle.com/blog/tag/rake/feed/" rel="self" type="application/rss+xml" />
	<link>http://nealenssle.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 27 Sep 2011 11:39:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to detect if a Rake task is being run with --trace</title>
		<link>http://nealenssle.com/blog/2010/11/12/how-to-detect-if-a-rake-task-is-being-run-with-trace/</link>
		<comments>http://nealenssle.com/blog/2010/11/12/how-to-detect-if-a-rake-task-is-being-run-with-trace/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 22:16:07 +0000</pubDate>
		<dc:creator>Neal Enssle</dc:creator>
				<category><![CDATA[untagged]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://nealenssle.com/blog/?p=360</guid>
		<description><![CDATA[So occasionally I want to output a debugging message for a Rake task. That&#8217;s easy enough. But what if I only want to output that message if the Rake task is being run with the --trace option? It turns out the answer is to check the Rake.application.options.trace flag in your task to determine if the [...]]]></description>
			<content:encoded><![CDATA[<p>So occasionally I want to output a debugging message for a Rake task. That&#8217;s easy enough. But what if I <strong>only</strong> want to output that message if the Rake task is being run with the <code>--trace</code> option? It turns out the answer is to check the <strong>Rake.application.options.trace</strong> flag in your task to determine if the <code>--trace</code> argument has been passed in, like so:</p>
<pre>
desc "The answer to life, the universe, and everything"
task :answer do |t|
  puts "42"
  if Rake.application.options.trace
    puts "Task '#{t.name}' is in --trace mode"
  end
end
</pre>
<p>Note that you can also set <strong>Rake.application.options.trace = true</strong> in your Rake task if you always want to see the stack trace information.</p>
]]></content:encoded>
			<wfw:commentRss>http://nealenssle.com/blog/2010/11/12/how-to-detect-if-a-rake-task-is-being-run-with-trace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to find the name of the current Rake task</title>
		<link>http://nealenssle.com/blog/2009/11/14/how-to-find-the-name-of-the-current-rake-task/</link>
		<comments>http://nealenssle.com/blog/2009/11/14/how-to-find-the-name-of-the-current-rake-task/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 03:15:59 +0000</pubDate>
		<dc:creator>Neal Enssle</dc:creator>
				<category><![CDATA[untagged]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://nealenssle.com/blog/?p=161</guid>
		<description><![CDATA[A few weeks ago I needed a Rake task to answer a simple question for me:
&#8220;What&#8217;s my name?&#8221;
I honestly can&#8217;t quite remember why exactly the task needed to know it&#8217;s own name, but at the time it seemed important and I spent a couple hours digging about before I found out that Rake tasks are [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I needed a Rake task to answer a simple question for me:</p>
<p>&#8220;What&#8217;s my name?&#8221;</p>
<p>I honestly can&#8217;t quite remember <em>why exactly</em> the task needed to know it&#8217;s own name, but at the time it seemed important and I spent a couple hours digging about before I found out that Rake tasks are shy by nature. Or at least when implemented the way almost every tutorial tells you to implement them:</p>
<pre>
namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do
    puts "The answer to life, the universe, and everything is 42."
  end

end
</pre>
<p>But how do I know what the name of the current task is?  Well, after making a couple of random attempts (like &#8220;puts task.name&#8221;) and digging around in the <em>very sparse</em> documentation on the <a href="http://rake.rubyforge.org/">Rake homepage</a>, I stumbled  across a bit of documentation that talked about &#8220;Tasks with Actions&#8221;:</p>
<blockquote><p>
Actions are defined by passing a block to the task method. Any Ruby code can be placed in the block. The block may reference the task object via the block parameter.</p>
<pre>
task :name => [:prereq1, :prereq2] do |t|
  # actions (may reference t)
end
</pre>
</blockquote>
<p>Ah ha! &#8220;The block may reference the task object via the block parameter.&#8221;  That&#8217;s exactly what I was looking for.  Adding the block parameter allows us to reference the task object itself. And the task object itself has a good bit of useful meta information:</p>
<pre>
namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do |t|
    puts t.name
    puts t.comment
    puts t.scope
  end

end
</pre>
<p>The task now returns the following:</p>
<pre>
life:answer
The answer to life, the universe, and everything
life
</pre>
<p>The first line of output is nifty, and answers the &#8220;What&#8217;s my name?&#8221; question with the fully scoped name of the task. Given that much it&#8217;s probably not hard to return the task name in even the most deeply namespaced Rakefile. The second line is the &#8220;comment&#8221; &#8212; actually the description of the task.  And the third line gets us the scope (namespaces). There are more methods available, but check teh googles for &#8220;Rake::Task api&#8221;.</p>
<p>So with this meta information we can do something useful like:</p>
<pre>
namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do |t|
    puts "#{t.comment} is 42."
  end

end
</pre>
<p>Voila. And that&#8217;s probably enough meta for one day.</p>
]]></content:encoded>
			<wfw:commentRss>http://nealenssle.com/blog/2009/11/14/how-to-find-the-name-of-the-current-rake-task/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

