The Rake’s Progress – Part II

Some things I have loved so far about working with rake/ruby:

  • File enumeration
  • Blending regular expressions where it makes things more readable
  • Testability

After checking out lots of other rake files, it is easy to see how much simpler ruby can make things. Along the way I am picking up some idiom’s that (seem) more acceptable and concise.

File Selects

For example, I can do this:

Dir.glob(Path.join(“/path/to/my/files/**”,”*.dll”) + Dir.glob(Path.join(“/path/to/my/files/**”,”*.pdb”)

or better I can use rake’s FileList and a more concise File.join:“path”,”to”,”my”,”files”,”*.{dll,pdb,xml}”))


Directory Ops

I wanted to copy a file structure from one directory to another, but preserving the structure from the original. I had a hard time figuring out the ruby way of doing this, but this seems to do the trick:,"**","*.{spark,asax,aspx,ascx,ashx}"),
File.join("#{BUILD_CONFIG_DIR}/**", "*")
).each do |file|
path = File.join(DEPLOY_WEB_DIR,file.gsub(WEB_DIR,""))
target = File.dirname(path)
cp, in ruby for to_str invocation workaround

Configuration & Templating

I like the config.yaml RoR convention, so here’s what I came up with. First, two simple classes:

require 'yaml'
class Environment
attr_accessor :development,:test,:production

def [](name)
if name.downcase=="development" then development
elsif name.downcase=="test" then test
elsif name.downcase=="production" then production

class Configuration
attr_accessor :service_path, :database_name, :connection_string, :log_level,:static_files_url ,:dev
# Support templating of member data.
def get_binding
Plz…Ignore the crappy [] hack in Environment . These two classes will be read in by ERB like so:
desc "Expand templates"
task :expand => [:clean] do
config = YAML::load(,"config.yaml")))
b = config[ENVIRONMENT].get_binding,"**","*.{template}"),File.join(INSTALLER_DIR,"**","*.{template}")).each do |file|
path =,b)

Here’s my config.yaml. Note how we map to the objects I defined above :

--- !ruby/object:Environment 
development: !ruby/object:Configuration
database_name: MaterialsTesting_Dev
connection_string: my_connection_string
log_level: DEBUG
test: !ruby/object:Configuration
database_name: MaterialsTesting_Test
connection_string: my_connection_string
log_level: DEBUG
production: !ruby/object:Configuration
database_name: MaterialsTesting
connection_string: my_connection_string
log_level: INFO


Now we can use the ExpandTemplate call:

class ExpandTemplate		
def write(file,b)
template =
erb =,0,"%<>")
output_file_name = File.join(BUILD_DIR,file.gsub(".template",""))
output =, 'w') do |file|
file.puts erb.result(b)


So far this has been fun ripping  out nant scripts and replacing them with rake.

Posted 09-13-2009 1:56 AM by Michael Nichols
Filed under: ,



mendicant wrote re: The Rake’s Progress – Part II
on 09-14-2009 12:05 AM

I love seeing more people using rake. I'm very close to starting to push  rake on the guys at my work. I doubt it'll go over well until I can get some AppCmd scripts wrapped in ruby... and when I get a better implementation of expanding templates :D

Looking forward to seeing more.

Ruprict wrote re: The Rake’s Progress – Part II
on 09-14-2009 9:51 AM

I struggled with the copy this dir to that dir and keep this dir's structure in that dir, too. Ended up using pathmap to replace

part of the source path with the target path.  Dunno if you've used pathmap, but it's really nice.

Just another way to do it...

Bruno Lopes wrote re: The Rake’s Progress – Part II
on 09-17-2009 6:34 PM

I've also been trying to get away from the xml mafia in build scripts, but after spending some time getting to know msbuild tasks rather well, I started some work to get those tasks running under rake in ironruby. I'd say you could do the same with nant tasks.

So far I've been able to get several tasks going, and will most likely be able to replace most of my scripts with rake.

You can look at the ongoing effort on the github repo at .

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)