class Environment < Hash def initialize end def self.scm return "git" if(Dir.exists?(".git")) return "svn" if(Dir.exists?(".svn")) "none" end def self.copy src,dest FileUtils.mkdir_p File.dirname(dest) if !File.exists?(File.dirname(dest)) FileUtils.cp(src,dest) end def self.copy_files files,dest Console.debug("Environment.copy_files([#{files.to_s}],'#{dest}'')") files.each{|f| if(File.directory?(dest)) dest_file = "#{dest}/#{f}" Console.debug "copying #{f} to #{dest_file}" FileUtis.mkdir_p(File.dirname(dest_file)) if(!File.exists?(File.dirname(dest_file))) FileUtils.cp(f,dest_file) end } end def self.svn_add_all dir Dir.chdir(dir) do Dir.glob('**/*').each{|f| puts `svn add #{f}` } end end def self.scm_origin if(Dir.exists?(".git")) `git remote show origin`.scan(/Fetch URL: ([\w:\/.-]+)/).each{|m| return m.first.to_s } end if(Dir.exists?(".svn")) `svn info`.scan(/URL: ([\w:\/.-]+)/).each{|m| return m.first.to_s } end "" end def self.svn_latest_revision if(Dir.exists?(".svn")) `svn info`.scan(/Last Changed Rev: ([\d]+)/).each{|m| return m.first.to_s } end "0" end def self.dev_root ["DEV_HOME","DEV_ROOT","USERPROFILE","HOME"].each {|v| return ENV[v].gsub('\\','/') unless ENV[v].nil? } dir="~" dir=ENV["HOME"] unless ENV["HOME"].nil? dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil? dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil? return dir end def self.machine if !ENV['COMPUTERNAME'].nil? return ENV['COMPUTERNAME'] end machine = `hostname` machine = machine.split('.')[0] if machine.include?('.') return machine.strip end def self.working_directory Rake.application.original_dir end def self.context dir =Environment.working_directory.gsub(Environment.dev_root + '/','') return dir.split('/').first end def self.relative_directory Environment.working_directory.gsub(Environment.dev_root + '/' + Environment.context + '/','') end def self.branch if(Environment.scm=='git') branches=`git branch`.split(/\n/) branches.each{|b| return b.gsub('*','').strip if b.include?('*') } end end def self.copy_if_newer(source,dest) if(File.exists?(source)) if(File.exists?(dest)) if(File.mtime(source) > File.mtime(dest)) FileUtils.cp(source,dest) puts "copied " + Color.green + source + Color.clear + " to " + Color.green + dest + Color.clear end else FileUtils.cp(source,dest) puts "copied " + Color.green + source + Color.clear + " to " + Color.green + dest + Color.clear end end end def self.rake(directory) Environment.rake(directory,'default') end def self.rake(directory,task) Environment.rake(directory,task,false) end def self.rake(directory,task,log) Dir.chdir(directory) do timer=Timer.new out=`rake #{task}` if $? != 0 puts "rake #{task} in " + directory if log puts out if log raise "rake #{task} in " + directory + " failed" else puts " " + timer.elapsed_str + " " + directory + " rake " + task if log #puts "rake default (" + directory + ") " + timer.elapsed_str end end end def self.execute(cmd) timer=Timer.new puts "executing " + cmd puts `#{cmd}` puts timer.elapsed_str end end