Friday, January 06, 2006

I prefer Rake to NAnt (#2)

I prefer Ruby's libraries to NAnt's tasks.
Recently, I was asked to add a step to the build file that would delete all build outputs that were older than 2 weeks old. The build output is stored in a folder in the distrobution folder. I'm not even going to attempt this task in NAnt, but using Ruby's find, delete, and fileutils the code was quite easy to write. (I admit, this could probably be done even better, but this was the first thing that came to mind.)
require 'find'
require 'date'
require 'fileutils'

root_dir = "C:/Dist"
two_weeks_ago = Date.today - 14

puts "deleting files last modified before #{two_weeks_ago}"
Find.find(root_dir) do |path|
if File.dirname(path) == root_dir
file_modify_date = Date.parse(File.mtime(path).strftime("%m/%d/%Y"))
puts "#{path} last modified on #{file_modify_date}"
if file_modify_date < two_weeks_ago
puts "#{path} removed"
FileUtils.rm_rf(path)
end
end
end
Post a Comment