Managing .NET Deployment Configuration With Rake

Posted by Liam McLennan on Geeks with Blogs See other posts from Geeks with Blogs or by Liam McLennan
Published on Thu, 10 Jun 2010 21:16:35 GMT Indexed on 2010/06/11 3:33 UTC
Read the original article Hit count: 329

Filed under:

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'

© Geeks with Blogs or respective owner