Managing .NET Deployment Configuration With Rake
- by Liam McLennan
Rake is a ruby internal DSL for build scripting. With (or without) the help of albacore rake makes an excellent build scripting tool for .NET projects. The albacore documentation does a good job of explaining how to build solutions with rake but there is nothing to assist with another common build task – updating configuration files. The following ruby script provides some helper methods for performing common configuration changes that are required as part of a build process.
class ConfigTasks
def self.set_app_setting(config_file, key, value)
ovsd_element = config_file.root.elements['appSettings'].get_elements("add[@key='#{key}']")[0]
ovsd_element.attributes['value'] = value
end
def self.set_connection_string(config_file, name, connection_string)
conn_string_element = config_file.root.elements['connectionStrings'].get_elements("add[@name='#{name}']")[0]
conn_string_element.attributes['connectionString'] = connection_string
end
def self.set_debug_compilation(config_file, debug_compilation)
compilation_element = config_file.root.elements['system.web'].get_elements("compilation")[0]
compilation_element.attributes['debug'] = false
end
private
def self.write_xml_to_file(xml_document, file)
File.open(file, 'w') do |config_file|
formatter = REXML::Formatters::Default.new
formatter.write(xml_document, config_file)
end
end
end
To use, require the file and call the class methods, passing the configuration file name and any other parameters.
require 'config_tasks'
ConfigTasks.set_app_setting 'web.config', 'enableCache', 'false'