Class: Environment

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

Constant Summary collapse

@@default =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#execute, #to_html

Constructor Details

#initialize(env = nil) ⇒ Environment

Returns a new instance of Environment.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/base/environment.rb', line 13

def initialize env=nil
  @output=''

  @env=Hash.new
  @env_aliases={'HOME' => ['USERPROFILE'],
                'DEV_ROOT' => ['DEV_HOME','HOME','USERPROFILE'],
                'USERNAME' => ['USER','USR']
  }
  env.each{|k,v| @env[k.to_s]=v} if !env.nil?
  @@default=self if @@default.nil?

  @publish_dir="#{root_dir}/publish"
  FileUtils.mkdir_p @publish_dir if !File.exists? @publish_dir
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



6
7
8
# File 'lib/base/environment.rb', line 6

def output
  @output
end

#publish_dirObject

Returns the value of attribute publish_dir.



6
7
8
# File 'lib/base/environment.rb', line 6

def publish_dir
  @publish_dir
end

Class Method Details

.checkObject

def self.remove directory

if(File.exists?(directory))
  begin
    FileUtils.rm_rf directory
    FileUtils.rm_r directory
  rescue
  end
end

end



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/base/environment.rb', line 206

def self.check
  puts 'checking commands...'
  missing_command=false
  ['ruby --version','svn --version --quiet','git --version','msbuild /version','nunit-console','nuget','candle','light','gem --version'].each{|cmd|
    command=Command.new(cmd)
    command[:quiet]=true
    command[:ignore_failure]=true
    command.execute
    if(command[:exit_code] == 0)
      puts "#{cmd.split(' ')[0]} #{get_version(command[:output])}"
    else
      puts "#{cmd.split(' ')[0]} not found."
        missing_command=true
    end
    
  }
  puts "missing commands may be resolved by making sure that are installed and in PATH environment variable." if missing_command
end

.defaultObject



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

def self.default
  @@default=Environment.new if @@default.nil?
  @@default
end

.dev_rootObject

Begin LEGACY support



29
30
31
# File 'lib/base/environment.rb', line 29

def self.dev_root
  default.root_dir
end

.get_version(text) ⇒ Object



225
226
227
# File 'lib/base/environment.rb', line 225

def self.get_version text
  text.match(/(\d+\.\d+\.[\d\w]+)/)
end

.linux?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/base/environment.rb', line 183

def self.linux?
  unix? and not mac?
end

.mac?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/base/environment.rb', line 175

def self.mac?
 (/darwin/ =~ RUBY_PLATFORM) != nil
end

.OSObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/base/environment.rb', line 155

def self.OS
  if windows?
    return "windows"
  else
    if mac?
      return "mac"
    else
      if linux?
        return "linux"
      else
        return "unix"
      end
    end
  end
end

.unix?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/base/environment.rb', line 179

def self.unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/base/environment.rb', line 171

def self.windows?
  Gem.win_platform?
end

Instance Method Details

#colorize?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/base/environment.rb', line 122

def colorize?
  colorize=true
  if Environment.windows?
    if(`gem list win32console`.include?('win32console'))
      require 'ansi/code'
    else
      colorize=false
    end
  end
  if Environment.mac?
    colorize=false
  end
  colorize
end

#debug?Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/base/environment.rb', line 117

def debug?
  return true if get_env('DEBUG')=='true'
  false
end

#dropbox_dirObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/base/environment.rb', line 48

def dropbox_dir
  dropbox_info = "#{ENV['LOCALAPPDATA']}/Dropbox/info.json"
  if(File.exists?(dropbox_info))
    info = JSON.parse(IO.read(dropbox_info))
    if(info.has_key?('personal'))
      if(info['personal'].has_key?('path'))
        return info['personal']['path']
      end
    end
  end
  ""
end

#get_env(key) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/base/environment.rb', line 98

def get_env key
  if(!@env.nil? && @env.has_key?(key))
    return @env[key] 
    end
  value = ENV[key]
  if(value.nil?)
    if(@env_aliases.has_key?(key))
      @env_aliases[key].each{|akey|
        value=get_env(akey) if value.nil?
      }
    end
  end
  value
end

#has_work?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/base/environment.rb', line 142

def has_work?
  true
end

#home_dirObject



38
39
40
# File 'lib/base/environment.rb', line 38

def home_dir
  get_env('HOME').gsub('\\','/')
end

#infoObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/base/environment.rb', line 229

def info 
  puts "Environment"
  puts "  ruby version: #{`ruby --version`}"
  puts " ruby platform: #{RUBY_PLATFORM}"
  puts "      dev_root: #{self.root_dir}"
  puts "       machine: #{self.machine}"
  puts "          user: #{self.user}"
  #puts " configuration: #{self.configuration}"
  puts "         debug: #{self.debug?}"
  puts "git user.email: #{Git.user_email}" 
  puts " "
  puts "Path Commands"
  ['svn --version --quiet','git --version','msbuild /version','nuget','candle','light','gem --version'].each{|cmd|
    command=Command.new(cmd)
    command[:quiet]=true
    command[:ignore_failure]=true
    command.execute
    if(command[:exit_code] == 0)
      puts "#{cmd.split(' ')[0].fix(14)} #{Environment.get_version(command[:output])}"
    else
      puts "#{cmd.split(' ')[0].fix(14)} not found."
        missing_command=true
    end
  }
end

#log_dirObject



42
43
44
45
46
# File 'lib/base/environment.rb', line 42

def log_dir
  dir="#{root_dir}/log/#{user}@#{machine}"
  FileUtils.mkdir_p dir if !File.exists? dir
  dir
end

#machineObject



85
86
87
88
89
90
# File 'lib/base/environment.rb', line 85

def machine
  return ENV['COMPUTERNAME'] if !ENV['COMPUTERNAME'].nil? 
  machine = `hostname`
  machine = machine.split('.')[0] if machine.include?('.')
  return machine.strip
end

#make_dirObject



67
68
69
70
71
# File 'lib/base/environment.rb', line 67

def make_dir
  dir="#{root_dir}/make"
  FileUtils.mkdir_p dir if !File.exists? dir
  dir
end

#out(message) ⇒ Object



146
147
148
149
# File 'lib/base/environment.rb', line 146

def out message
    puts message if !get_env('SUPPRESS_CONSOLE_OUTPUT')
    @output=@output+message+'\n'
end

#root_dirObject

End LEGACY support



34
35
36
# File 'lib/base/environment.rb', line 34

def root_dir
  get_env('DEV_ROOT').gsub('\\','/')
end

#set_env(key, value) ⇒ Object



113
114
115
# File 'lib/base/environment.rb', line 113

def set_env key,value
  @env[key]=value
end

#show_success?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/base/environment.rb', line 151

def show_success?
  true
end

#tmp_dirObject



61
62
63
64
65
# File 'lib/base/environment.rb', line 61

def tmp_dir
  dir="#{root_dir}/tmp"
  FileUtils.mkdir_p dir if !File.exists? dir
  dir
end

#userObject



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

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

#working?Boolean

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/base/environment.rb', line 137

def working?
  return true if Rake.application.original_dir.include? wrk_dir
  false
end

#wrk_dirObject

def publish_dir

dir="#{root_dir}/publish"
FileUtils.mkdir_p dir if !File.exists? dir
dir

end



79
80
81
82
83
# File 'lib/base/environment.rb', line 79

def wrk_dir
  dir="#{root_dir}/wrk"
  FileUtils.mkdir_p dir if !File.exists? dir
  dir
end