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:

FileList.new(Path.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:

FileList.new(File.join(WEB_DIR,"**","*.{spark,asax,aspx,ascx,ashx}"),
File.join(WEB_DIR,"bin","*.{dll,pdb}"),
File.join("#{BUILD_CONFIG_DIR}/**", "*")
).each do |file|
path = File.join(DEPLOY_WEB_DIR,file.gsub(WEB_DIR,""))
target = File.dirname(path)
File.makedirs(target)
cp Pathname.new(file), Pathname.new(target)#bug in ruby for to_str invocation workaround
end

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
end
end
end

class Configuration
attr_accessor :service_path, :database_name, :connection_string, :log_level,:static_files_url ,:dev
# Support templating of member data.
def get_binding
binding
end
end
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(File.open(File.join(CONFIG_DIR,"config.yaml")))
b = config[ENVIRONMENT].get_binding
FileList.new(File.join(CONFIG_DIR,"**","*.{template}"),File.join(INSTALLER_DIR,"**","*.{template}")).each do |file|
path = ExpandTemplate.new().write(file,b)
end
end

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
static_files_url: http://static.local.com/MaterialsTesting
test: !ruby/object:Configuration
database_name: MaterialsTesting_Test
connection_string: my_connection_string
log_level: DEBUG
static_files_url: http://static.local.com/MaterialsTesting
production: !ruby/object:Configuration
database_name: MaterialsTesting
connection_string: my_connection_string
log_level: INFO
static_files_url: http://static.local.com/MaterialsTesting

 

Now we can use the ExpandTemplate call:

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

end

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: ,

[Advertisement]

Comments

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 github.com/.../ironbuildrake .

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)