Class: Sprout::System::BaseSystem

Inherits:
Object
  • Object
show all
Defined in:
sprout/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)


219
220
221
# File 'sprout/lib/sprout/system/base_system.rb', line 219

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.



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

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 'sprout/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.



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

def clean_path(path)
end

- (Object) env_home (protected)



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

def env_home
  ENV['HOME']
end

- (Object) env_homedrive (protected)



193
194
195
# File 'sprout/lib/sprout/system/base_system.rb', line 193

def env_homedrive
  ENV['HOMEDRIVE']
end

- (Object) env_homedrive_and_homepath (protected)



201
202
203
204
205
# File 'sprout/lib/sprout/system/base_system.rb', line 201

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

- (Object) env_homepath (protected)



197
198
199
# File 'sprout/lib/sprout/system/base_system.rb', line 197

def env_homepath
  ENV['HOMEPATH']
end

- (Object) env_userprofile (protected)



207
208
209
# File 'sprout/lib/sprout/system/base_system.rb', line 207

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 'sprout/lib/sprout/system/base_system.rb', line 70

def execute(tool, options='')
  Sprout.stdout.puts("#{tool} #{options}")
  runner = get_and_execute_process_runner(tool, options)
  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 'sprout/lib/sprout/system/base_system.rb', line 93

def execute_silent(tool, options='')
  get_and_execute_process_runner(tool, options)
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.

Parameters:

  • (File) tool

    Path to the executable.

  • (String) options (defaults to: '')

    The command line options that the executable accepts.

  • (Regex) prompt (defaults to: nil)

    The prompt that will trigger the listener block to be called.

Yields:

  • (String)

    Message that was received from the process, called when #prompt is encountered.

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'sprout/lib/sprout/system/base_system.rb', line 107

def execute_thread tool, options='', prompt=nil, &block
  t = Thread.new do
    Thread.current.abort_on_exception = true
    runner = execute_silent(tool, options)
    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

  t
end

- (Object) find_home (protected)



228
229
230
231
232
233
234
235
236
237
238
239
# File 'sprout/lib/sprout/system/base_system.rb', line 228

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.



187
188
189
# File 'sprout/lib/sprout/system/base_system.rb', line 187

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.



251
252
253
254
255
# File 'sprout/lib/sprout/system/base_system.rb', line 251

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 'sprout/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 'sprout/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 'sprout/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 'sprout/lib/sprout/system/base_system.rb', line 48

def library
  return home
end

- (Object) read_from(pipe, prompt, &block)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'sprout/lib/sprout/system/base_system.rb', line 127

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)



215
216
217
# File 'sprout/lib/sprout/system/base_system.rb', line 215

def tilde_home
  File.expand_path("~")
end

- (Object) worst_case_home (protected)



223
224
225
226
# File 'sprout/lib/sprout/system/base_system.rb', line 223

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