Class: Sprout::System::BaseSystem

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

Overview

The abstract base class for all supported system/platform types. In general, users are created by calling the create factory method on the System module.

System.create

Assuming you call the create method, you should wind up with a concrete system that matches your system, and these concrete users will generally be derived from this base class.

Direct Known Subclasses

JavaSystem, UnixSystem, WinSystem

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) alt_separator? (protected)

Returns:

  • (Boolean)


179
180
181
# File 'lib/sprout/system/base_system.rb', line 179

def alt_separator?
  File::ALT_SEPARATOR
end

- (Object) application_home(name)

Different operating systems will store Application data different default locations.

Subclasses will generally override this method and return the appropriate location for their operating system.

name String value of the Application name for which we'd like to store data.



139
140
141
# File 'lib/sprout/system/base_system.rb', line 139

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

- (Boolean) can_execute?(platform)

Returns:

  • (Boolean)


60
61
62
# File 'lib/sprout/system/base_system.rb', line 60

def can_execute? platform
  platform == :universal
end

- (Object) clean_path(path)

Clean the provided path String for the current operating system.

Each operating system behaves differently when we attempt to execute a file with spaces in the path to the file.

Subclasses will generally override this method and clean the path appropriately for their operating system.



126
127
# File 'lib/sprout/system/base_system.rb', line 126

def clean_path(path)
end

- (Object) env_home (protected)



171
172
173
# File 'lib/sprout/system/base_system.rb', line 171

def env_home
  ENV['HOME']
end

- (Object) env_homedrive (protected)



153
154
155
# File 'lib/sprout/system/base_system.rb', line 153

def env_homedrive
  ENV['HOMEDRIVE']
end

- (Object) env_homedrive_and_homepath (protected)



161
162
163
164
165
# File 'lib/sprout/system/base_system.rb', line 161

def env_homedrive_and_homepath
  drive = env_homedrive
  path = env_homepath
  "#{drive}:#{path}" if drive && path
end

- (Object) env_homepath (protected)



157
158
159
# File 'lib/sprout/system/base_system.rb', line 157

def env_homepath
  ENV['HOMEPATH']
end

- (Object) env_userprofile (protected)



167
168
169
# File 'lib/sprout/system/base_system.rb', line 167

def env_userprofile
  ENV['USERPROFILE']
end

- (Object) execute(tool, options = '')

Creates a new process, executes the command and returns whatever the process wrote to stdout, or stderr.

Raises a Sprout::Errors::ExecutionError if the process writes to stderr



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sprout/system/base_system.rb', line 70

def execute(tool, options='')
  Sprout::Log.puts("#{tool} #{options}")
  runner = get_and_execute_process_runner(tool, options)
  error  = runner.read_err
  result = runner.read

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

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

  result || error
end

- (Object) execute_silent(tool, options = '')

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.



93
94
95
# File 'lib/sprout/system/base_system.rb', line 93

def execute_silent(tool, options='')
  get_and_execute_process_runner(tool, options)
end

- (Object) execute_thread(tool, options = '')

Execute a new process in a separate thread.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sprout/system/base_system.rb', line 100

def execute_thread(tool, options='')
  runner = nil
  Thread.new do
    runner = execute_silent(tool, options)
  end
  # Wait for the runner to be created
  # before returning a nil reference
  # that never gets populated...
  while runner.nil? do
    sleep(0.1)
  end
  runner
end

- (Object) find_home (protected)



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sprout/system/base_system.rb', line 188

def find_home
  [:env_userprofile, :env_home, :env_homedrive_and_homepath].each do |key|
    value = self.send(key)
    return value unless value.nil?
  end

  begin
    return tilde_home
  rescue StandardError
    worst_case_home
  end
end

- (Object) format_application_name(name)

Template method that should be overridden by subclasses.



147
148
149
# File 'lib/sprout/system/base_system.rb', line 147

def format_application_name(name)
  name
end

- (Object) get_and_execute_process_runner(tool, options = nil) (protected)

Get a process runner and execute the provided executable, with the provided options.

executable String path to the external executable file.

options String commandline options to send to the executable.



211
212
213
214
215
# File 'lib/sprout/system/base_system.rb', line 211

def get_and_execute_process_runner tool, options=nil
  runner = get_process_runner
  runner.execute_open4 clean_path(tool), options
  runner
end

- (Object) get_process_runner

Instantiate and return a new Sprout::ProcessRunner so that we can execute it.



56
57
58
# File 'lib/sprout/system/base_system.rb', line 56

def get_process_runner
  Sprout::ProcessRunner.new
end

- (Object) home

Get the home path for a system on a particular operating system.

This path will be different, depending on which system owns the curren process, and which operating system they are on.



22
23
24
# File 'lib/sprout/system/base_system.rb', line 22

def home
  @home ||= find_home
end

- (Object) home=(home)

Set the home path for a system on a particular operating system.

If you request the home path before setting it, we will attempt to determine the home path of the current system for the current operating system.

This is just a simple way to override the default behavior.



35
36
37
# File 'lib/sprout/system/base_system.rb', line 35

def home=(home)
  @home = home
end

- (Object) library

Some operating systems (like OS X and Windows) have a specific location where applications are expected to store files for a particular system. This location is generally a subdirectory of home.

The value of this location will usually be overridden in concrete System classes.



48
49
50
# File 'lib/sprout/system/base_system.rb', line 48

def library
  return home
end

- (Object) tilde_home (protected)



175
176
177
# File 'lib/sprout/system/base_system.rb', line 175

def tilde_home
  File.expand_path("~")
end

- (Object) worst_case_home (protected)



183
184
185
186
# File 'lib/sprout/system/base_system.rb', line 183

def worst_case_home
  return "C:\\" if alt_separator?
  return "/"
end