{"id":1853,"date":"2010-11-18T04:16:39","date_gmt":"2010-11-18T03:16:39","guid":{"rendered":"http:\/\/www.devco.net\/?p=1853"},"modified":"2010-11-18T04:16:39","modified_gmt":"2010-11-18T03:16:39","slug":"a_few_rake_tips","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2010\/11\/18\/a_few_rake_tips.php","title":{"rendered":"A few rake tips"},"content":{"rendered":"

Rake<\/a> is the Ruby make system, it lets you write tasks using the Ruby language and is used in most Ruby projects.<\/p>\n

I wanted to automate some development tasks and had to figure out a few patterns, thought I’d blog them for my own future reference and hopefully to be useful to someone else.<\/p>\n

In general people pass variables on the command line to rake, something like:<\/p>\n

<\/p>\n

\r\n$ ENVIRONMENT=production rake db::migrate\r\n<\/pre>\n

<\/code><\/p>\n

This is ok if you know all the variables you can pass but sometimes it’s nice to support asking if something isn’t given:<\/p>\n

<\/p>\n

\r\ndef ask(prompt, env, default=\"\")\r\n    return ENV[env] if ENV.include?(env)\r\n\r\n    print \"#{prompt} (#{default}): \"\r\n    resp = STDIN.gets.chomp\r\n\r\n    resp.empty? ? default : resp\r\nend\r\n\r\ntask :do_something do\r\n    what = ask(\"What should I do\", \"WHAT\", \"something\")\r\n    puts \"Doing #{what}\"\r\nend\r\n<\/pre>\n

<\/code><\/p>\n

This will support running like:<\/p>\n

<\/p>\n

\r\n$ WHAT=\"foo\" rake do_something\r\n<\/pre>\n

<\/code><\/p>\n

It will also though prompt for the information with a default if the environment variable isn’t set. The only real trick here is that you have to use STDIN.gets<\/em> and not just gets<\/em>. For added niceness you could use Highline<\/a> to build the prompts but that’s an extra dependency.<\/p>\n

Here is what it does if you don’t specify a environment var:<\/p>\n

<\/p>\n

\r\n$ rake do_something\r\n(in \/home\/rip\/temp)\r\nWhat should I do (something): test\r\nDoing test\r\n<\/pre>\n

<\/code><\/p>\n

The second tip is about rendering templates, I am using the above ask method to ask a bunch of information from the user and then building a few templates based on that. So I am calling ERB a few times and wanted to write a helper. <\/p>\n

<\/p>\n

\r\ndef render_template(template, output, scope)\r\n    tmpl = File.read(template)\r\n    erb = ERB.new(tmpl, 0, \"<>\")\r\n    File.open(output, \"w\") do |f|\r\n        f.puts erb.result(scope)\r\n    end\r\nend\r\n\r\ntask :do_something do\r\n   what = ask(\"What do you want to do\", \"WHAT\", \"something\")\r\n  \r\n   render_template(\"simple.erb\", \"\/tmp\/output.txt\", binding)\r\nend\r\n<\/pre>\n

<\/code><\/p>\n

The template would just have:<\/p>\n

<\/p>\n

\r\nDoing <%= what %>\r\n<\/pre>\n

<\/code><\/p>\n

This is pretty standard stuff the only odd thing is the binding<\/a><\/em>, it basically takes the current scope of the do_something<\/em> task and passes it into the render_template<\/em> method that then pass it along to the erb. This way your local variable – what<\/em> in this case – is available as a local variable in the template.<\/p>\n","protected":false},"excerpt":{"rendered":"

Rake is the Ruby make system, it lets you write tasks using the Ruby language and is used in most Ruby projects. I wanted to automate some development tasks and had to figure out a few patterns, thought I’d blog them for my own future reference and hopefully to be useful to someone else. In […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[1],"tags":[121,13],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1853"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=1853"}],"version-history":[{"count":9,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1853\/revisions"}],"predecessor-version":[{"id":1862,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1853\/revisions\/1862"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=1853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=1853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=1853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}