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.



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

def initialize(env = nil)
  @output = ""

  @env = {}
  @env_aliases = { "HOME" => ["USERPROFILE"],
                   "DEV_ROOT" => %w[DEV_HOME HOME USERPROFILE],
                   "USERNAME" => %w[USER USR] }
  env&.each { |k, v| @env[k.to_s] = v }
  @@default = self if @@default.nil?

  @publish_dir = "#{root_dir}/publish"
  FileUtils.mkdir_p @publish_dir unless File.exist? @publish_dir
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#publish_dirObject

Returns the value of attribute publish_dir.



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

def publish_dir
  @publish_dir
end

Class Method Details

.checkObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/base/environment.rb', line 177

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 do |cmd|
    command = Command.new(cmd)
    command[:quiet] = true
    command[:ignore_failure] = true
    command.execute
    if (command[:exit_code]).zero?
      puts "#{cmd.split(" ")[0]} #{get_version(command[:output])}"
    else
      puts "#{cmd.split(" ")[0]} not found."
      missing_command = true
    end
  end
  if missing_command
    puts "missing commands may be resolved by making sure that are installed and in PATH environment variable."
  end
end

.defaultObject



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

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

.dev_rootObject

####Begin LEGACY support



32
33
34
# File 'lib/base/environment.rb', line 32

def self.dev_root
  default.root_dir
end

.get_version(text) ⇒ Object



198
199
200
# File 'lib/base/environment.rb', line 198

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

.linux?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/base/environment.rb', line 173

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

.mac?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/base/environment.rb', line 165

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

.OSObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/base/environment.rb', line 149

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

.unix?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/base/environment.rb', line 169

def self.unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/base/environment.rb', line 161

def self.windows?
  Gem.win_platform?
end

Instance Method Details

#admin?Boolean

####End LEGACY support

Returns:

  • (Boolean)


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

def admin?
  rights = `whoami /priv`
  rights.include?("SeCreateGlobalPrivilege")
end

#colorize?Boolean

Returns:

  • (Boolean)


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

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

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  return true if defined?(DEBUG)

  false
end

#dropbox_dirObject



56
57
58
59
60
61
62
63
# File 'lib/base/environment.rb', line 56

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

#get_env(key) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/base/environment.rb', line 95

def get_env(key)
  return @env[key] if !@env.nil? && @env.key?(key)

  value = ENV[key]
  if value.nil? && @env_aliases.key?(key)
    @env_aliases[key].each do |akey|
      value = get_env(akey) if value.nil?
    end
  end
  value
end

#has_work?Boolean

Returns:

  • (Boolean)


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

def has_work?
  true
end

#home_dirObject



46
47
48
# File 'lib/base/environment.rb', line 46

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

#infoObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/base/environment.rb', line 202

def info
  puts "Environment"
  puts "  ruby version: #{`ruby --version`}"
  puts " ruby platform: #{RUBY_PLATFORM}"
  puts "      dev_root: #{root_dir}"
  puts "       machine: #{machine}"
  puts "          user: #{user}"
  puts "            os: #{Environment.OS}"
  # puts " configuration: #{self.configuration}"
  puts "         debug: #{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



50
51
52
53
54
# File 'lib/base/environment.rb', line 50

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

#machineObject



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

def machine
  return ENV["COMPUTERNAME"] unless ENV["COMPUTERNAME"].nil?

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

#make_dirObject



71
72
73
74
75
# File 'lib/base/environment.rb', line 71

def make_dir
  dir = "#{root_dir}/make"
  FileUtils.mkdir_p dir unless File.exist? dir
  dir
end

#out(message) ⇒ Object



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

def out(message)
  puts message unless get_env("SUPPRESS_CONSOLE_OUTPUT")
  @output = "#{@output}#{message}\\n"
end

#root_dirObject



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

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

#set_env(key, value) ⇒ Object



107
108
109
# File 'lib/base/environment.rb', line 107

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

#show_success?Boolean

Returns:

  • (Boolean)


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

def show_success?
  true
end

#tmp_dirObject



65
66
67
68
69
# File 'lib/base/environment.rb', line 65

def tmp_dir
  dir = "#{root_dir}/tmp"
  FileUtils.mkdir_p dir unless File.exist? dir
  dir
end

#userObject



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

def user
  get_env("USERNAME")
end

#working?Boolean

Returns:

  • (Boolean)


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

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

  false
end

#wrk_dirObject



77
78
79
80
81
# File 'lib/base/environment.rb', line 77

def wrk_dir
  dir = "#{root_dir}/work"
  FileUtils.mkdir_p dir unless File.exist? dir
  dir
end