Class: Environment

Inherits:
Hash
  • Object
show all
Defined in:
lib/environment.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



4
5
# File 'lib/environment.rb', line 4

def initialize
end

Class Method Details

.branchObject



108
109
110
111
112
113
114
115
# File 'lib/environment.rb', line 108

def self.branch
  if(Environment.scm=='git')
    branches=`git branch`.split(/\n/)
 branches.each{|b|
   return b.gsub('*','').strip if b.include?('*')
 }
  end
end

.contextObject



99
100
101
102
# File 'lib/environment.rb', line 99

def self.context
  dir =Environment.working_directory.gsub(Environment.dev_root + '/','')
  return dir.split('/').first
end

.copy(src, dest) ⇒ Object



13
14
15
16
# File 'lib/environment.rb', line 13

def self.copy src,dest
  FileUtils.mkdir_p File.dirname(dest) if !File.exists?(File.dirname(dest))
  FileUtils.cp(src,dest)
end

.copy_files(files, dest) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/environment.rb', line 18

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}"
FileUtils.mkdir_p(File.dirname(dest_file)) if(!File.exists?(File.dirname(dest_file)))
   FileUtils.cp(f,dest_file)
 end
  }
end

.copy_if_newer(source, dest) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/environment.rb', line 117

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

.dev_rootObject



61
62
63
64
65
66
67
68
# File 'lib/environment.rb', line 61

def self.dev_root
  ["DEV_HOME","DEV_ROOT"].each {|v|
   return ENV[v].gsub('\\','/') unless ENV[v].nil?
 }
  dir=self.home
  dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil?
return dir
end

.execute(cmd) ⇒ Object



155
156
157
158
159
160
# File 'lib/environment.rb', line 155

def self.execute(cmd)
  timer=Timer.new
  puts "executing " + cmd
  puts `#{cmd}`
  puts timer.elapsed_str
end

.homeObject



70
71
72
73
74
75
76
77
78
# File 'lib/environment.rb', line 70

def self.home 
  ["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?
  return dir
end

.machineObject



80
81
82
83
84
85
86
87
88
# File 'lib/environment.rb', line 80

def self.machine
   if !ENV['COMPUTERNAME'].nil? 
  return ENV['COMPUTERNAME']
end

   machine = `hostname`
   machine = machine.split('.')[0] if machine.include?('.')
return machine.strip
end

.rake(directory, task, log) ⇒ Object



131
132
133
# File 'lib/environment.rb', line 131

def self.rake(directory)
  Environment.rake(directory,'default')
end

.relative_directoryObject



104
105
106
# File 'lib/environment.rb', line 104

def self.relative_directory
  Environment.working_directory.gsub(Environment.dev_root + '/' + Environment.context + '/','')
end

.scmObject



7
8
9
10
11
# File 'lib/environment.rb', line 7

def self.scm
  return "git" if(Dir.exists?(".git"))
  return "svn" if(Dir.exists?(".svn"))
  "none"
end

.scm_originObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/environment.rb', line 38

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

.svn_add_all(dir) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/environment.rb', line 30

def self.svn_add_all dir
  Dir.chdir(dir) do
 Dir.glob('**/*').each{|f|
   puts `svn add #{f} --parents` if(!File.directory?(f))
 }
  end
end

.svn_latest_revisionObject



52
53
54
55
56
57
58
59
# File 'lib/environment.rb', line 52

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

.userObject



90
91
92
93
# File 'lib/environment.rb', line 90

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

  ENV['USERNAME']
end

.working_directoryObject



95
96
97
# File 'lib/environment.rb', line 95

def self.working_directory
  Rake.application.original_dir
end