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



101
102
103
104
105
106
107
108
# File 'lib/environment.rb', line 101

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



92
93
94
95
# File 'lib/environment.rb', line 92

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



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/environment.rb', line 110

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
69
70
71
# File 'lib/environment.rb', line 61

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

.execute(cmd) ⇒ Object



148
149
150
151
152
153
# File 'lib/environment.rb', line 148

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

.machineObject



73
74
75
76
77
78
79
80
81
# File 'lib/environment.rb', line 73

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



124
125
126
# File 'lib/environment.rb', line 124

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

.relative_directoryObject



97
98
99
# File 'lib/environment.rb', line 97

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



83
84
85
86
# File 'lib/environment.rb', line 83

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

.working_directoryObject



88
89
90
# File 'lib/environment.rb', line 88

def self.working_directory
  Rake.application.original_dir
end