puts_debug "read " + __FILE__ module Dev class Environment def self.replace_text_in_glob(glob,search,replace) Dir.glob(glob).each{ |f| replace_text_in_file(f,search,replace) } end def copy_from_template(source_directory,target_directory,glob_pattern,search,replace) Dir.chdir(source_directory) do Dir.glob("**/*") {|f| if(!File.directory?(f)) target_file = target_directory + "/" + f.gsub(search,replace) if(!File.exist?(target_file)) FileUtils.mkdir_p(File.dirname(target_file)) if(!Dir.exist?(File.dirname(target_file))) FileUtils.cp(f,target_file) end end } end Dir.glob(glob_pattern) { |f| Dev::Environment::replace_text_in_file(f,search,replace) } end def self.replace_text_in_file(filename,search,replace) puts_debug "replace_text_in_file(#{filename},#{search.to_s},#{replace.to_s})" text1 = File.read(filename) text2 = text1.gsub(search) { |str| str=replace } unless text1==text2 File.open(filename,"w") { |f| f.puts text2 } puts " replaced text in #{filename}" end end def self.user_profile ["USERPROFILE","HOME"].each {|v| return ENV[v].gsub('\\','/') unless ENV[v].nil? } dir="~" 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 if(RUBY_VERSION == "1.8.7") # reg_exp = /(\/(wrk|dep|exp)\/[.\w\d-]+\/[.\w\d-]+\/[.\w\d-@]+)/ if(RUBY_VERSION != "1.8.6") # unless(Rake.original_dir().match(reg_exp).nil?) # wrk_rel = Rake.original_dir().match(reg_exp)[1] if Rake.original_dir().match(reg_exp).length > 0 # if(!wrk_rel.nil? && wrk_rel.length > 0) # dir = Rake.original_dir().gsub(wrk_rel,""); # return dir # end # reg_exp = /(\/(Dropbox)\/[.\w\d-]+\/[.\w\d-]+\/[.\w\d-@]+)/ # unless(Rake.original_dir().match(reg_exp).nil?) # wrk_rel = Rake.original_dir().match(reg_exp)[1] if Rake.original_dir().match(reg_exp).length > 0 # if(!wrk_rel.nil? && wrk_rel.length > 0) # dir = Rake.original_dir().gsub(wrk_rel,""); # return dir # end # end #end return dir end def self.expand_string_variables(str) eval("\"#{str.gsub('"','\"')}\"") end def self.s_to_hash(str) # check for hash hash=nil if(str.strip.index('{')==0 && str.strip.index('}')==str.strip.length-1) begin eval("hash=#{str.strip}") rescue hash=nil end end hash end def self.user return ENV['USER'] if !ENV['USER'].nil? #on Unix return ENV['USERNAME'] if !ENV['USERNAME'].nil? #on Windows return "unknown" end def self.machine if !ENV['COMPUTERNAME'].nil? return ENV['COMPUTERNAME'] end machine = `hostname` machine = machine.split('.')[0] if machine.include?('.') return machine.strip #return "unknown" end def self.expand_command(command); Dev::Environment.expand_project_variables(command); end def self.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' value=DEV.get_value(key) result = result.gsub(pvar[0],value) } end return result end return str end end end # module Dev