main
  1include FileTest
  2
  3class SqlRunner
  4  def initialize(settings = Hash.new("Not found"))
  5    @sql_tool = settings.fetch(:sql_tool,File.join(ENV['SystemDrive'],'program files','microsoft sql server','100','tools','binn','osql.exe'))
  6    @command_line_args = settings.fetch(:command_line_args, '-b -i')
  7    @connection_string = settings.fetch(:connection_string,"-E")
  8  end
  9
 10  def process_sql_files(files)
 11      files.each do|file|
 12        begin
 13            sh "#{@sql_tool} #{@connection_string} #{@command_line_args} #{file}"
 14        rescue 
 15          puts("Error processing sql file:#{file}")
 16          raise
 17        end
 18      end
 19  end
 20end
 21
 22class LocalSettings
 23  attr_reader :settings
 24
 25  def [](setting)
 26    @settings[setting]
 27  end
 28
 29  def each_pair
 30    @settings.each_key do|key,value|
 31      yield key,@settings[key]
 32    end
 33  end
 34end
 35
 36class TemplateFile
 37  attr_reader :template_file_name
 38  attr_reader :output_file_name
 39
 40  def initialize(template_file_name)
 41    @template_file_name = template_file_name
 42    @output_file_name = template_file_name.gsub('.template','')
 43  end
 44
 45  def generate(settings_dictionary)
 46    generate_to(@output_file_name,settings_dictionary)
 47  end
 48
 49  def generate_to_directory(output_directory,settings_dictionary)
 50    generate_to(File.join(output_directory,File.basename(@output_file_name)),settings_dictionary)
 51  end
 52
 53  def generate_to_directories(output_directories,settings_dictionary)
 54    output_directories.each do |directory|
 55      generate_to_directory(directory,settings_dictionary)
 56    end
 57  end
 58
 59  def generate_to(output_file,settings_dictionary)
 60     File.delete?(output_file) 
 61
 62     File.open_for_write(output_file) do|generated_file|
 63       File.open_for_read(@template_file_name) do|template_line|
 64         settings_dictionary.each_key do|key|
 65           template_line = template_line.gsub("@#{key}@","#{settings_dictionary[key]}")
 66         end
 67         generated_file.puts(template_line)
 68       end
 69     end
 70  end
 71
 72  def to_s()
 73    "Template File- Template:#{@template_file_name} : Output:#{@output_file_name}"
 74  end
 75
 76end
 77
 78class TemplateFileList
 79  @template_files
 80  def initialize(files_pattern)
 81    @template_files  = []
 82    FileList.new(files_pattern).each do|file| 
 83      @template_files.push(TemplateFile.new(file))
 84    end
 85  end
 86
 87  def each()
 88    @template_files.each do |file|
 89      yield file
 90    end
 91  end
 92
 93  def generate_all_output_files(settings)
 94    @template_files.each do |file|
 95      file.generate(settings)
 96    end
 97  end
 98end
 99
100class MbUnitRunner
101	def initialize(items)
102		@source_dir = items.fetch(:source_dir, 'product')
103		@mbunit_dir = items.fetch(:mbunit_dir, 'thirdparty/mbunit')
104		@test_results_dir = items.fetch(:test_results_dir, 'artifacts')
105		@compile_target = items.fetch(:compile_target, 'debug')
106		@category_to_exclude = items.fetch(:category_to_exclude, 'missing')
107		@show_report = items.fetch(:show_report, false)
108		@report_type = items.fetch(:report_type,'XML')
109	end
110	
111	def execute_tests(assemblies)
112		Dir.mkdir @test_results_dir unless exists?(@test_results_dir)
113		
114		assemblies.each do |assem|
115		  sh build_command_line_for(assem)
116		end
117	end
118
119  def build_command_line_for(assembly)
120	  file = File.expand_path("#{@source_dir}/#{assembly}/bin/#{@compile_target}/#{assembly}.dll")
121      "#{@mbunit_dir}/mbunit.cons.exe #{file} /rt:#{@report_type} /rnf:#{assembly}.dll-results /rf:#{@test_results_dir} #{'/sr' if @show_report} /ec:#{@category_to_exclude}"
122  end
123end
124
125class MSBuildRunner
126	def self.compile(attributes)
127		version = attributes.fetch(:clrversion, 'v3.5')
128		compile_target = attributes.fetch(:compile_target, 'debug')
129	    solution_file = attributes[:solution_file]
130		
131		framework_dir = File.join(ENV['windir'].dup, 'Microsoft.NET', 'Framework', 'v3.5')
132		msbuild_file = File.join(framework_dir, 'msbuild.exe')
133		
134		sh "#{msbuild_file} #{solution_file} /property:Configuration=#{compile_target} /t:Rebuild"
135	end
136end
137
138class File
139  def self.open_for_read(file)
140     File.open(file,'r').each do|line|
141       yield line
142     end
143  end
144
145  def self.read_all_text(file)
146    contents = ''
147    File.open_for_read(file) do |line|
148      contents += line
149    end
150  end
151
152  def self.delete?(file)
153	  puts "deleting.." +file
154    File.delete(file) if File.exists?(file)
155  end
156
157  def self.open_for_write(file)
158     File.open(file,'w') do|new_file|
159       yield new_file
160     end
161  end
162
163  def self.base_name_without_extensions(file)
164    File.basename(file,'.*')
165  end
166
167 end
168
169class BDDDocRunner
170  def initialize(settings = Hash.new('missing'))
171    @output_folder = settings.fetch(:output_folder,'artifacts')
172    @observation_attribute = settings.fetch(:observation_attribute,'ObservationAttribute')
173    @bdddoc_folder = settings.fetch(:bdddoc_folder,'thirdparty\developwithpassion.bdddoc')
174    @mbunit_test_output_folder = settings.fetch(:mbunit_test_output_folder,'artifacts')
175    @developwithpassion_bdddoc_exe = settings.fetch(:bdddoc_exe,'developwithpassion.bdddoc.exe')
176    @logo_jpg = settings.fetch(:logo_jpg,File.join(@bdddoc_folder,'developwithpassion.bdddoc-logo.jpg'))
177    @css = settings.fetch(:css,File.join(@bdddoc_folder,'developwithpassion.bdddoc.css'))
178  end
179
180  def run(test_library)
181    test_file = File.basename(test_library)
182    output_file = "#{File.join(@output_folder,test_file)}.developwithpassion.bdddoc.html"
183    mbunit_test_output_file = "#{File.join(@mbunit_test_output_folder,test_file)}-results.xml"
184    sh "#{File.join(@bdddoc_folder,@developwithpassion_bdddoc_exe)} #{test_library} #{@observation_attribute} #{output_file} #{mbunit_test_output_file}"
185   FileUtils.cp @logo_jpg,@output_folder
186   FileUtils.cp @css,@output_folder
187  end
188end