May 31, 2009

Replace macro fields with Ruby

OK, now let's do it in Ruby. This requires a values.rb folder (like our values.ps1 folder in powershell):


require 'erb'
require 'rio'

module MacroConfig

def self.generate
mkdir_if_not_exist 'configs'

Dir.glob('templates/*').each do |template_file|
template = ''
rio(template_file) > template
config = ERB.new(template).result(get_values)

config_file = 'configs/' + File.basename(template_file)
rio(config_file) < script =" ''">> script
script << "\n"
end

eval script
binding
end

def self.get_values
read_values(['../values.rb', 'values.rb'])
end

def self.mkdir_if_not_exist(dir_name)
Dir.mkdir dir_name unless File.exist? dir_name
end

end

No comments:

Post a Comment