Class: Dev::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/dev/Environment.rb

Class Method Summary collapse

Class Method Details

.dev_rootObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dev/Environment.rb', line 27

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

.expand_command(command) ⇒ Object

return “unknown”



92
# File 'lib/dev/Environment.rb', line 92

def self.expand_command(command); Dev::Environment.expand_project_variables(command); end

.expand_project_variables(str) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dev/Environment.rb', line 93

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. '<name>' to 'widget

        key = pvar[0].gsub("<","").gsub(">","")          #                                '<paths,nunit>' to 'C:/wherever/NUnit.exe'

  value=DEV.get_value(key)
        result = result.gsub(pvar[0],value)
      }
    end
    return result
  end
  
  return str
end

.expand_string_variables(str) ⇒ Object



59
60
61
# File 'lib/dev/Environment.rb', line 59

def self.expand_string_variables(str)
  eval("\"#{str.gsub('"','\"')}\"")
end

.machineObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/dev/Environment.rb', line 82

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

.replace_text_in_file(filename, search, replace) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dev/Environment.rb', line 10

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

.replace_text_in_glob(glob, search, replace) ⇒ Object



6
7
8
# File 'lib/dev/Environment.rb', line 6

def self.replace_text_in_glob(glob,search,replace)
  Dir.glob(glob).each{ |f| replace_text_in_file(f,search,replace) }
end

.s_to_hash(str) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dev/Environment.rb', line 63

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

.userObject



76
77
78
79
80
# File 'lib/dev/Environment.rb', line 76

def self.user
  return ENV['USER'] if !ENV['USER'].nil?  #on Unix

  return ENV['USERNAME'] if !ENV['USERNAME'].nil? #on Windows

   return "unknown"
end

.user_profileObject



20
21
22
23
24
25
# File 'lib/dev/Environment.rb', line 20

def self.
  ["USERPROFILE","HOME"].each {|v|
return ENV[v].gsub('\\','/') unless ENV[v].nil?
   }
  dir="~"
end