Created
November 5, 2009 05:52
-
-
Save dvhthomas/226805 to your computer and use it in GitHub Desktop.
Sample rake tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rexml/document' | |
@json_files | |
@xml_files | |
# This set of tasks assumes that some settings have been already | |
# been loaded in to globals called APP_SETTINGS and USER_SETTINGS. | |
# Pretty much everything will fail until that has been done, usually | |
# in a calling rakefile | |
desc "Default build task" | |
task :default => ['user_config','db:update_connection_strings'] do | |
end | |
task :clean => ['creator:clean'] | |
task :clobber => ['creator:clean'] | |
desc 'Update configuration files specific to a user' | |
task :user_config => ['creator:create_user_files'] do | |
puts "Updating JSON configuration files" | |
search_terms = USER_SETTINGS['servers'] | |
APP_SETTINGS['json_templates'].each do |name| | |
target = name.ext('') # strip file extension | |
cp name, target, :verbose => false | |
text = File.read(target) | |
search_terms.each do |s,r| | |
text = text.gsub(s.upcase,r) | |
end | |
f = File.open(target, 'w') | |
f.write(text) | |
end | |
end | |
def load_file_lists | |
@json_files = Array.new | |
@json_files.concat(APP_SETTINGS['json_templates']) | |
@xml_files = Array.new | |
@xml_files.concat(APP_SETTINGS['xml_templates']) | |
end | |
namespace :creator do | |
# This is kind of wacky - Rake can create a task-per-file. In this | |
# case we first build a list of all of the files we want and THEN | |
# build a task that collects all of the file copy activities | |
# together. Sort of backwards to my way of thinking but it works. | |
# What's really cool is that Rake keeps track of which files it's | |
# already created, so you can call this multiple times and it'll | |
# only copy files that don't yet exist. Of course, if you've changed | |
# a file then you need to run the creator:clean task first to get | |
# back to square one | |
load_file_lists() | |
FileList[@json_files, @xml_files].each do | source | | |
target = source.ext('') | |
file target => source do | |
cp source, target, :verbose => false | |
end | |
desc 'Creates all of the user specific files from templates' | |
task :create_user_files => target | |
end | |
desc 'Remove user specific config files' | |
task :clean do | |
load_file_lists() | |
delete_these = @json_files.map {|x| x.ext('') } | |
delete_these.concat(@xml_files.map {|x| x.ext('') }) | |
puts "Deleting existing user configurations" | |
rm_f FileList[delete_these], :verbose => false | |
end | |
end | |
# Database specific stuff | |
namespace :db do | |
desc 'Updates *.config files with valid database connection strings' | |
task :update_connection_strings => ['creator:create_user_files'] do | |
puts "Updating connection strings" | |
@xml_files.each do | xml | | |
doc = REXML::Document::new(File.open(xml.ext(''))) | |
doc.elements.each('connectionStrings/add') do | element | | |
element.attributes["connectionString"] = USER_SETTINGS['database'][element.attributes['name'].to_s.downcase] | |
end | |
f = File.open(xml.ext(''), 'w') | |
f.write(doc) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment