puts_debug "read " + __FILE__.foreground(:green) require 'semver' UNITTESTS=Array.new module Dev class Project < Hash def initialize(hash,init_defaults=true) puts_debug "initialize, copying hash values" hash.each { |name,value| self[name]=value } update_default_values if init_defaults end def array_method(name) string_name=name.to_s puts_debug "method_missing name=" + string_name puts " no directives defined for #{string_name}" if self.get_value(string_name).nil? && string_name=="pull" return if(self.get_value(string_name).nil?) puts " no directives defined for #{string_name}" if self.get_value(string_name).length < 1 return if self.get_value(string_name).length < 1 self.get_value(string_name).each{ |c| execute_cmd(c); sleep(0.5) } end def execute_cmd(c) # expand the command here.... command=expand_command(c) puts_debug "command: " + command.to_s if c.include?('<%') && c.include?('%>') #puts "Command: " + c eval(c.gsub("<%","").gsub("%>","")) else # can the command be converted to a hash? hash=Dev::Environment.s_to_hash(command) call=nil if(hash.nil?) call=Dev::SystemCall.new(command) else call=Dev::SystemCall.new(hash) end call.puts_summary end end def method_missing( name, *args ); array_method(name); end def expand_command(command); expand_project_variables(command); end def expand_project_variables(str) if str.kind_of?(Hash) expandedHash = Hash.new str.each do |name,value| expandedHash[name]=expand_project_variables(value) end return expandedHash end if str.kind_of?(Array) expandedArray= Array.new str.each{|e|expandedArray << expand_project_variable(e) } return expandedArray end if str.kind_of?(String) result=Dev::Environment.expand_string_variables(str) # expands ruby variables, e.g. '#{name}' to 'widget' if result.include?('<') && result.include?('>') result.scan(/(<[\w,]+>)/).each { |pvar| # expand project variables, e.g. '' to 'widget key = pvar[0].gsub("<","").gsub(">","") # '' to 'C:/wherever/NUnit.exe' result = result.gsub(pvar[0],get_value(key)) } end return result end return str end def loc_cmd cmd="countloc --recurse ." cmd="countloc --recurse --mode ruby ." if self[:type]=="ruby" || self[:type]=="gem" cmd="countloc --recurse --mode csharp ." if self[:type]=="C#" cmd="countloc --recurse --mode cpp ." if self[:type]=="c++" return cmd end def loc system(loc_cmd) end def add unless self[:src_glob].nil? scm = Dev::Scm.new return if scm.scm_type == "none" if self[:src_glob].kind_of?(Hash) self[:src_glob].each do |name,value| puts " adding files " + value.to_s scm.add_file_glob(value) end else puts " adding files " + self[:src_glob].to_s scm.add_file_glob(self[:src_glob]) end end end def loc_total # parse the output for TOTAL LOC call=Dev::SystemCall.new(loc_cmd) words=call.output.split if(words.length>6) return words[words.length-6] end "?" end def context Dev::Environment.user + "." + Dev::Environment.machine end def check puts " default.taskstamp." + context + " exists." if File.exists?("default.taskstamp."+context) puts " default.taskstamp." + context + " does not exist" unless File.exists?("default.taskstamp."+context) begin hasdiff = has_diff rescue puts "has_diff threw an exception." end puts " no differences detected." unless hasdiff puts " detected differences." if hasdiff if File.exists?("default.taskstamp."+context) && !hasdiff #puts " nothing to do" update exit end #if !has_diff and File.exists?("default.taskstamp."+context) # puts " no differences detected." # puts " default.taskstamp." + context + " exists." # exit #end #puts " detected differences" if has_diff #puts " default.taskstamp." + context + " does not exist" unless File.exists?("default.taskstamp." + context) end def stamp_task(name) File.open("#{name}.taskstamp." + context,"w") { |f| f.write(Time.now.to_s) } end def replace #puts " " #puts "replace".foreground(:yellow).bright hash=self[:replace] if hash.nil? puts " no replace directives" end unless hash.nil? hash.each do |k,v| unless v.nil? file=v[:file] search=v[:search] replace=v[:replace] glob=v[:glob] unless search.nil? || replace.nil? unless file.nil? Dev::Environment.replace_text_in_file(file,search,replace) end unless glob.nil? Dev::Environment.replace_text_in_glob(glob,search,replace) end end end end end end def features if self[:type]=="C#" && File.exists?("bin/x86/Release/#{self[:name]}.Features.dll") call=Dev::SystemCall.new(expand_project_variables(" /nologo bin/x86/Release/#{self[:name]}.Features.dll")) puts call.command puts call.output raise "exit_code=#{call.status.to_s}" + call.error unless call.status==0 end array_method("features") end def has_build_products end def has_diff call=nil if File.exists?(".git") call=Dev::SystemCall.new('git status') return true if call.output.include?("new file:") return true if call.output.include?("deleted:") return true if call.output.include?("modified:") end call=Dev::SystemCall.new('git diff --name-only') if File.exists?(".git") call=Dev::SystemCall.new('svn diff') if File.exists?(".svn") unless call.nil? || call.output.length==0 puts_debug call.output return true # differences detected else return false # no differences end end def update array_method("update") end def commit scm = Dev::Scm.new return if scm.scm_type == "none" puts " no differences detected" unless has_diff array_method("commit") if has_diff if File.exists?(".svn") call=Dev::SystemCall.new('svn info') url = call.output.match(/URL: ([\d\w\.\:\/-]+)/)[1] rev = call.output.match(/Last Changed Rev: ([\d]+)/)[1] puts " #{url}@#{rev}" end end def rake_working_deps unless dep_hash.nil? dep_hash.each do |key,value| if value.kind_of?(Hash) # potential hash keys: uri, dir, rake # uri is required unless value.get_value("uri").nil? uri=expand_project_variables(value.get_value("uri")) uri_words=uri.split('/') while(uri_words.length > 3) do uri_words.shift end dir=self[:dev_root] + "/wrk/" + uri_words.join('/') unless value.get_value("rake").nil? if(File.exist?(dir)) rake_task=value.get_value("rake") cmd_hash = {:cmd=> "rake #{rake_task}", :dir=> "#{dir}"} end end end end # unless value.get_value("uri").nil? end end # unless dep_hash.nil? end def pull update unless has_diff array_method("pull") filename=Dir.pwd + "/rakefile.rb" puts " updating revision variables for " + filename Dev::Svn::update_revision_variables(filename) end def info [ "file_count","loc" ].each do |k| puts_debug "set_default_value(#{k})" set_default_value(k) end puts " " Hash.print_hash("",self) puts " " end def set_default_value(key) set_value(key,get_default_value(key)) if get_value(key).nil? && !get_default_value(key).nil? end def get_default_value(key) system_call = Dev::SystemCall.new("semver init") if(!File.exist?(".semver")) version = SemVer.find value="#{Dev::Environment.dev_root}" if key=="dev_root" value="C#" if key=="type" value="**/*.{rb,feature,semver}" if key=="src_glob" value="**/*.{cs,csproj,sln,xaml,xml,rb,resx,settings,feature,semver,snk,txt}" if key=="src_glob" && self[:type]=="C#" value="**/*.{c,cpp,h,hpp,semver,vcproj,sln,txt}" if key=="src_glob" && (self[:type]=="C++" || self[:type]=="c++" || self[:type]=="cpp") value="**/*.{rb,feature,semver}" if key=="src_glob" && self[:type]=="ruby" value="**/*.{rb,feature,gemspec,semver}" if key=="src_glob" && self[:type]=="gem" value=loc_total if key=="loc" value="svn" if key=="scm_type" && File.exists?(".svn") value="git" if key=="scm_type" && File.exists?(".git") value="#{version.major}.#{version.minor}.#{version.patch}" if key=="version" if key=="file_count" unless self[:src_glob].nil? if self[:src_glob].kind_of?(Hash) length=0 self[:src_glob].each do |name,value| length+=Dir.glob(self[:src_glob][name]).length end value=length.to_s else value=Dir.glob(self[:src_glob]).length.to_s end end end hash=Hash.new if(key=="paths") #if(self[:type]=="C#") hash.set_value("msbuild_vs9","C:/WINDOWS/Microsoft.NET/Framework/v3.5/MSBuild.exe") hash.set_value("msbuild","C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe") nunit_exe="#{Dev::Environment.dev_root}/dep/google/third-party/NUnit/2.6.0.12051/bin/nunit-console-x86.exe" ["2.5.10.11092","2.6.0.12051"].each{ |v| tmp="#{Dev::Environment.dev_root}/dep/github-third-party/NUnit/#{v}/bin/nunit-console-x86.exe" nunit_exe=tmp if File.exist?(tmp) && !File.exist?(nunit_exe) tmp="#{Dev::Environment.dev_root}/dep/google/third-party/NUnit/#{v}/bin/nunit-console-x86.exe" nunit_exe=tmp if File.exist?(tmp) && !File.exist?(nunit_exe) tmp="#{Dev::Environment.dev_root}/dep/ThirdParty/NUnit/2.5.10.11092/bin/net-2.0/nunit-console-x86.exe" nunit_exe=tmp if File.exist?(tmp) && !File.exist?(nunit_exe) } hash.set_value("nunit",nunit_exe) hash.set_value("regasm","C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/RegAsm.exe") if File.exists?("C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/RegAsm.exe") hash.set_value("regasm","C:/Windows/Microsoft.NET/Framework/v4.0.30319/RegAsm.exe") if File.exists?("C:/Windows/Microsoft.NET/Framework/v4.0.30319/RegAsm.exe") #end end value=hash if hash.length > 0 array=Array.new if(key=="setup") #array << "bundle install" if File.exists?("Gemfile") array << "{ :cmd=> 'bundle install', :capture_output=> false}" if File.exists?("Gemfile") && "#{RUBY_VERSION}">="1.8.7" # dep,svn directives dep_hash=self.get_value("dep") unless dep_hash.nil? dep_hash.each do |key,value| if value.kind_of?(Hash) dep=Dev::Dep.new(value) dep.setup_commands.each{|cmd| array << cmd} end end end if(!Dir.glob("*.csproj").nil? && Dir.glob("*.csproj").length > 0) array << "<%Dir.mkdir 'bin' unless File.exist?('bin')%>" array << "<%Dir.mkdir 'bin/Debug' unless File.exist?('bin/Debug')%>" array << "<%Dir.mkdir 'bin/Release' unless File.exist?('bin/Release')%>" end end if(key=="pull") # dep,svn directives dep_hash=self.get_value("dep") unless dep_hash.nil? dep_hash.each do |key,value| if value.kind_of?(Hash) dep=Dev::Dep.new(value) dep.pull_commands.each{|cmd| array << cmd} puts_debug "pull array: " + array.to_s end end end end if(key=="compile") Dir.glob("*.gemspec").each { |gs| #array << "gem build #{gs}" array << "{ :cmd=> 'gem build #{gs}', :capture_output=> false}" } if self[:type]=="gem" Dir.glob("*.csproj").each{|cs| platforms = Dev::Project::extract_platforms(cs) platforms = ["AnyCPU"] if platforms.nil? puts_debug "for project " + cs + " platforms = " + platforms.to_s platforms.each{ |platform| if(platform=="AnyCPU") if(!has_key?(:any_release_compile_flags)) self[:any_release_compile_flags]="/property:Configuration=Release /property:Platform=\"Any CPU\" /p:OutputPath=./bin/Release" end array<<" #{cs} " if RUBY_PLATFORM.include?("w32") array<<"xbuild #{cs} " if !RUBY_PLATFORM.include?("w32") end if(platform=="x86") if(!has_key?(:x86_release_compile_flags)) self[:x86_release_compile_flags]="/property:Configuration=Release /property:Platform=\"x86\" /p:OutputPath=./bin/x86/Release" end array<<" #{cs} " if RUBY_PLATFORM.include?("w32") array<<"xbuild #{cs} " if !RUBY_PLATFORM.include?("w32") end if(platform=="x64") if(!has_key?(:x64_release_compile_flags)) self[:x64_release_compile_flags]="/property:Configuration=Release /property:Platform=\"x64\" /p:OutputPath=./bin/x64/Release" end array<<" #{cs} " if RUBY_PLATFORM.include?("w32") #array<<"xbuild #{cs} " if !RUBY_PLATFORM.include?("w32") end } } if self[:type]=="c++" if(!has_key?(:win32_release_compile_flags)) self[:win32_release_compile_flags]="/property:Configuration=Release /property:Platform=\"win32\"" end Dir.glob("*.sln").each{|sln| slntext=File.read(sln) if(slntext.include?("Format Version 10.00")) array<<" #{sln} " else array<<" #{sln} " end } end end if(key=="test") Dir.glob("*.gemspec").each { |gs| #array << "{cmd: 'gem install ./#{gs.gsub('.gemspec','')}-#{self[:version]}.gem', capture_output: false}" array << "{:cmd=> 'gem install #{gs.gsub('.gemspec','')}-#{self[:version]}.gem', :capture_output=> false}" #array << "gem install ./#{gs.gsub('.gemspec','')}-#{self[:version]}.gem" } if self[:type]=="gem" if self[:type]=="C#" Dir.glob("*.{Test.csproj,Features.csproj}").each { |cs| dll_name="bin/x86/Release/#{File.basename(cs,'.csproj')}.dll" array << "\"\" /nologo #{dll_name} /xml:#{dll_name}.nunit-results.xml" if RUBY_PLATFORM.include?("w32") array << "mono \"\" #{dll_name}" if !RUBY_PLATFORM.include?("w32") } end end if(key=="features") array << "cucumber features" if File.exists?("features") && Dir.glob("features/**/*.rb").length > 0 end if(key=="has_diff") array << 'git diff' if File.exists?(".git") array << "{ :cmd=> 'svn diff', :capture_output=> false}" if File.exists?(".svn") end if(key=="commit") array << 'git commit -a -m "rake commit all"' if File.exists?(".git") array << 'svn commit -m "rake commit all"' if File.exists?(".svn") end if(key=="update") array << 'svn update' if File.exists?(".svn") end value=array if array.length > 0 return value end def update_default_values puts_debug "update_default_values" self[:type]="C#" if self[:type].nil? [ "version","dev_root","src_glob","scm_type", "paths","add","pull","setup","compile","test","features","commit","update" ].each do |k| puts_debug "set_default_value(#{k})" set_default_value(k) end generate_tasks(self) end def self.extract_platforms(filename) results = Array::new text=File.read(filename) text.scan(/(\w+)<\/Platform/).each{ | var_match | results << var_match[0].to_s if !results.include?(var_match[0].to_s)} results end end end # module Dev