Class: Sprout::System::BaseSystem
- Inherits:
-
Object
- Object
- Sprout::System::BaseSystem
- 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
Instance Method Summary (collapse)
- - (Boolean) alt_separator? protected
-
- (Object) application_home(name)
Different operating systems will store Application data different default locations.
- - (Boolean) can_execute?(platform)
-
- (Object) clean_path(path)
Clean the provided path String for the current operating system.
- - (Object) env_home protected
- - (Object) env_homedrive protected
- - (Object) env_homedrive_and_homepath protected
- - (Object) env_homepath protected
- - (Object) env_userprofile protected
-
- (Object) execute(tool, options = '')
Creates a new process, executes the command and returns whatever the process wrote to stdout, or stderr.
-
- (Object) execute_silent(tool, options = '')
Creates and returns the process without attempting to read or write to the stream.
-
- (Sprout::ProcessRunner) execute_thread(tool, options = '', prompt = nil) {|String| ... }
Execute a new process in a separate thread and yield whatever output is written to its stderr and stdout.
- - (Object) find_home protected
-
- (Object) format_application_name(name)
Template method that should be overridden by subclasses.
-
- (Object) get_and_execute_process_runner(tool, options = nil)
protected
Get a process runner and execute the provided executable, with the provided options.
-
- (Object) get_process_runner
Instantiate and return a new Sprout::ProcessRunner so that we can execute it.
-
- (Object) home
Get the home path for a system on a particular operating system.
-
- (Object) home=(home)
Set the home path for a system on a particular operating system.
-
- (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.
- - (Object) read_from(pipe, prompt, &block)
- - (Object) tilde_home protected
- - (Object) worst_case_home protected
Instance Method Details
- (Boolean) alt_separator? (protected)
223 224 225 |
# File 'lib/sprout/system/base_system.rb', line 223 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.
183 184 185 |
# File 'lib/sprout/system/base_system.rb', line 183 def application_home(name) return File.join(library, format_application_name(name.to_s)); end |
- (Boolean) can_execute?(platform)
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.
170 171 |
# File 'lib/sprout/system/base_system.rb', line 170 def clean_path(path) end |
- (Object) env_home (protected)
215 216 217 |
# File 'lib/sprout/system/base_system.rb', line 215 def env_home ENV['HOME'] end |
- (Object) env_homedrive (protected)
197 198 199 |
# File 'lib/sprout/system/base_system.rb', line 197 def env_homedrive ENV['HOMEDRIVE'] end |
- (Object) env_homedrive_and_homepath (protected)
205 206 207 208 209 |
# File 'lib/sprout/system/base_system.rb', line 205 def env_homedrive_and_homepath drive = env_homedrive path = env_homepath "#{drive}:#{path}" if drive && path end |
- (Object) env_homepath (protected)
201 202 203 |
# File 'lib/sprout/system/base_system.rb', line 201 def env_homepath ENV['HOMEPATH'] end |
- (Object) env_userprofile (protected)
211 212 213 |
# File 'lib/sprout/system/base_system.rb', line 211 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, ='') Sprout.stdout.puts("#{tool} #{}") runner = get_and_execute_process_runner(tool, ) error = runner.read_err result = runner.read if(result.size > 0) Sprout.stdout.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, ='') get_and_execute_process_runner(tool, ) end |
- (Sprout::ProcessRunner) execute_thread(tool, options = '', prompt = nil) {|String| ... }
Execute a new process in a separate thread and yield whatever output is written to its stderr and stdout.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sprout/system/base_system.rb', line 107 def execute_thread tool, ='', prompt=nil, &block t = Thread.new do Thread.current.abort_on_exception = true runner = execute_silent(tool, ) Thread.current['runner'] = runner out = read_from runner.r, prompt, &block err = read_from runner.e, prompt, &block out.join && err.kill end # Wait for the runner to be created # before returning a nil reference # that never gets populated... while t['runner'].nil? do sleep(0.1) end if !t.alive? raise Sprout::Errors::UsageError.new(t['runner'].read_err) end t end |
- (Object) find_home (protected)
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/sprout/system/base_system.rb', line 232 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.
191 192 193 |
# File 'lib/sprout/system/base_system.rb', line 191 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.
255 256 257 258 259 |
# File 'lib/sprout/system/base_system.rb', line 255 def get_and_execute_process_runner tool, =nil runner = get_process_runner runner.execute_open4 clean_path(tool), 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) read_from(pipe, prompt, &block)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sprout/system/base_system.rb', line 131 def read_from pipe, prompt, &block Thread.new do Thread.current.abort_on_exception = true lines = '' line = '' pipe.sync = true pipe.each_char do |char| break if pipe.closed? line << char if line.match prompt yield line if block_given? lines << line lines = '' line = '' next end if char == "\n" lines << line yield line if block_given? line = '' end end end end |
- (Object) tilde_home (protected)
219 220 221 |
# File 'lib/sprout/system/base_system.rb', line 219 def tilde_home File.("~") end |
- (Object) worst_case_home (protected)
227 228 229 230 |
# File 'lib/sprout/system/base_system.rb', line 227 def worst_case_home return "C:\\" if alt_separator? return "/" end |