Class: Sprout::UnixUser

Inherits:
Object
  • Object
show all
Defined in:
lib/sprout/user.rb

Overview

UnixUser class

Direct Known Subclasses

OSXUser, WinUser

Instance Method Summary collapse

Constructor Details

#initializeUnixUser

:nodoc:



130
131
132
133
# File 'lib/sprout/user.rb', line 130

def initialize
  setup_user
  @home = nil
end

Instance Method Details

#application_home(name) ⇒ Object



277
278
279
# File 'lib/sprout/user.rb', line 277

def application_home(name)
  return File.join(library, format_application_name(name.to_s));
end

#clean_path(path) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/sprout/user.rb', line 265

def clean_path(path)
  repair_executable path

  if(path.index(' '))
    # Changed 2/26/2008 in attempt to support 
    # ToolTask.PathsParam s that have spaces in the values
    return path.split(' ').join('\ ')
#        return %{'#{path}'}
  end
  return path
end

#execute(tool, options = '') ⇒ Object

Creates a new process, executes the command and returns the result and throws if the process writes to stderr



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/sprout/user.rb', line 211

def execute(tool, options='')
  Log.puts(">> Execute: #{File.basename(tool)} #{options}")
  tool = clean_path(tool)
  runner = get_process_runner("#{tool} #{options}")

  error = runner.read_err
  result = runner.read

  if(result.size > 0)
    Log.puts result
  end

  if(error.size > 0)
    raise ExecutionError.new("[ERROR] #{error}")
  end

  result || error
end

#execute_silent(tool, options = '') ⇒ Object

Creates and returns the process without attempting to read or write to the stream. This is useful for interacting with long-lived CLI processes like FCSH or FDB.



234
235
236
237
# File 'lib/sprout/user.rb', line 234

def execute_silent(tool, options='')
  tool = clean_path(tool)
  return get_process_runner("#{tool} #{options}")
end

#execute_thread(tool, options = '') ⇒ Object



239
240
241
242
243
# File 'lib/sprout/user.rb', line 239

def execute_thread(tool, options='')
  return Thread.new do
    execute(tool, options)
  end
end

#format_application_name(name) ⇒ Object



281
282
283
284
285
286
# File 'lib/sprout/user.rb', line 281

def format_application_name(name)
  if(name.index('.') != 0)
    name = '.' + name
  end
  return name.split(" ").join("_").downcase
end

#get_exe_path(executable) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sprout/user.rb', line 170

def get_exe_path(executable)
  paths = get_paths
  file = nil
  paths.each do |path|
    file = File.join(path, executable)
    if(File.exists?(file))
      return file
    end
  end
  return nil
end

#get_pathsObject



186
187
188
# File 'lib/sprout/user.rb', line 186

def get_paths
  return ENV['PATH'].split(':')
end

#get_process_runner(command) ⇒ Object



204
205
206
# File 'lib/sprout/user.rb', line 204

def get_process_runner(command)
  return ProcessRunner.new(command)
end

#homeObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sprout/user.rb', line 142

def home
  if(@home)
    return @home
  end
  
  # Original implementation:
  #["HOME", "USERPROFILE"].each do |homekey|
  # Change submitted by Michael Fleet (disinovate)
  # Does this work for everyone on Windows?
  ["USERPROFILE", "HOME"].each do |homekey|
    return @home = ENV[homekey] if ENV[homekey]
  end

  if ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
    return @home = "#{ENV["HOMEDRIVE"]}:#{ENV["HOMEPATH"]}"
  end

  begin
    return @home = File.expand_path("~")
  rescue StandardError => e
    if File::ALT_SEPARATOR
      return @home = "C:\\"
    else
      return @home = "/"
    end
  end
end

#home=(path) ⇒ Object



138
139
140
# File 'lib/sprout/user.rb', line 138

def home=(path)
  @home = path
end

#in_path?(executable) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/sprout/user.rb', line 182

def in_path?(executable)
  return !get_exe_path(executable).nil?
end

#libraryObject



190
191
192
# File 'lib/sprout/user.rb', line 190

def library
  return home
end

#platformObject



194
195
196
197
198
199
200
201
202
# File 'lib/sprout/user.rb', line 194

def platform
  if(Platform::OS == :win32)
    return :win32
  elsif(Platform::IMPL == :macosx)
    return :macosx
  else
    return Platform::IMPL
  end
end

#repair_executable(path) ⇒ Object

Repair Windows Line endings found in non-windows executables (Flex SDK is regularly published with broken CRLFs)



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/sprout/user.rb', line 249

def repair_executable(path)
  return unless should_repair_executable(path)

  content = File.read(path)
  if(content.match(/\r\n/))
    content.gsub!(/\r\n/, "\n")
    File.open(path, 'w+') do |f|
      f.write content
    end
  end
end

#setup_userObject



135
136
# File 'lib/sprout/user.rb', line 135

def setup_user
end

#should_repair_executable(path) ⇒ Object



261
262
263
# File 'lib/sprout/user.rb', line 261

def should_repair_executable(path)
  return (File.exists?(path) && !File.directory?(path) && File.read(path).match(/^\#\!/))
end