Module: RProgram::System

Extended by:
Env::Variables
Defined in:
lib/rprogram/system.rb

Overview

Since:

  • 0.3.0

Class Method Summary collapse

Class Method Details

.archString

Determines the native architecture.

Examples:

System.arch
# => "x86-64"

Returns:

  • (String)

    The native architecture.

Since:

  • 0.3.0



28
29
30
# File 'lib/rprogram/system.rb', line 28

def System.arch
  @arch
end

.find_program(name) ⇒ Pathname?

Finds the full-path of the program with the matching name.

Examples:

System.find_program('as')
#=> #<Pathname:/usr/bin/as>

Parameters:

  • name (String)

    The name of the program to find.

Returns:

  • (Pathname, nil)

    The full-path of the desired program.

Since:

  • 0.3.0



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

def System.find_program(name)
  # add the `.exe` suffix to the name, if running on Windows
  if windows?
    name = "#{name}.exe"
  end

  paths.each do |dir|
    full_path = dir.join(name).expand_path

    return full_path if full_path.file?
  end

  return nil
end

.find_program_by_names(*names) ⇒ Pathname?

Finds the program matching one of the matching names.

Examples:

System.find_program_by_names("gas","as")
# => #<Pathname:/usr/bin/as>

Parameters:

  • names (Array)

    The names of the program to use while searching for the program.

Returns:

  • (Pathname, nil)

    The first full-path for the program.

Since:

  • 0.3.0



128
129
130
131
132
133
134
135
136
# File 'lib/rprogram/system.rb', line 128

def System.find_program_by_names(*names)
  names.each do |name|
    if (path = find_program(name))
      return path
    end
  end

  return nil
end

.jruby?Boolean

Determines if the current Ruby VM is JRuby.

Returns:

  • (Boolean)

    Specifies whether the Ruby VM is JRuby.

Since:

  • 0.3.0



82
83
84
85
# File 'lib/rprogram/system.rb', line 82

def System.jruby?
  Object.const_defined?(:RUBY_ENGINE) && \
    Object.const_get(:RUBY_ENGINE) == 'jruby'
end

.platformString

Determines the native platform.

Examples:

System.platform
# => "linux"

Returns:

  • (String)

    The native platform.

Since:

  • 0.3.0



42
43
44
# File 'lib/rprogram/system.rb', line 42

def System.platform
  @platform
end

.ruby_1_8?Boolean

Determines if the current Ruby VM is from the 1.8.x family.

Returns:

  • (Boolean)

    Specifies if the current Ruby VM is from the 1.8.x family.

Since:

  • 0.3.0



70
71
72
# File 'lib/rprogram/system.rb', line 70

def System.ruby_1_8?
  RUBY_VERSION[0,4] == '1.8.'
end

.run(path, *arguments) ⇒ Boolean .run(path, *arguments, options) ⇒ Boolean

Runs a program.

Overloads:

  • .run(path, *arguments) ⇒ Boolean

    Run the program with the given arguments.

    Parameters:

    • path (Pathname, String)

      The path of the program to run.

    • arguments (Array)

      Additional arguments to run the program with.

  • .run(path, *arguments, options) ⇒ Boolean

    Run the program with the given arguments and options.

    Parameters:

    • path (Pathname, String)

      The path of the program to run.

    • arguments (Array)

      Additional arguments to run the program with.

    • options (Hash)

      Additional options to execute the program with.

    Options Hash (options):

    • :env (Hash{String => String})

      Environment variables to execute the program with.

    • :popen (String)

      Specifies to run the program using IO.popen with the given IO mode.

Returns:

  • (Boolean)

    Specifies whether the program exited successfully.

Raises:

  • (RuntimeError)

    Passing :popen, :env or exec options is not supported before Ruby 1.9.1.

See Also:

Since:

  • 0.3.0



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
228
229
230
231
232
233
# File 'lib/rprogram/system.rb', line 179

def System.run(*arguments)
  # extra tailing options and ENV variables from arguments
  if arguments.last.kind_of?(Hash)
    options = arguments.pop
    env = (options.delete(:env) || {})
    popen = options.delete(:popen)
  else
    options = {}
    env = {}
  end

  # all arguments must be Strings
  arguments = arguments.map { |arg| arg.to_s }

  # print debugging information
  if RProgram.debug
    command = ''

    env.each do |name,value|
      command << "#{name}=#{value} "
    end
    
    command << arguments.join(' ')
    command << " #{options.inspect}" unless options.empty?

    STDERR.puts ">>> #{command}"
  end

  # passing ENV variables or exec options is not supported before 1.9.1
  if (!options.empty? && ruby_1_8?)
    raise("cannot pass exec options to Kernel.system in #{RUBY_VERSION}")
  end

  if popen
    # IO.popen does not accept multiple arguments on Ruby 1.8.x.
    if ruby_1_8?
      raise("cannot use :popen on #{RUBY_VERSION}, please use 1.9.x")
    end

    # :popen can only be used on Unix, or on Windows with JRuby
    if (windows? && !jruby?)
      raise("cannot use :popen on Windows, unless under JRuby")
    end
  end

  # re-add ENV variables and exec options
  arguments.unshift(env) unless env.empty?
  arguments.push(options) unless options.empty?

  if popen
    IO.popen(arguments,popen)
  else
    Kernel.system(*arguments)
  end
end

.run(path, *arguments) ⇒ Boolean .run(path, *arguments, options) ⇒ Boolean

Runs a program under sudo.

Overloads:

  • .run(path, *arguments) ⇒ Boolean

    Run the program with the given arguments.

    Parameters:

    • path (Pathname, String)

      The path of the program to run.

    • arguments (Array)

      Additional arguments to run the program with.

  • .run(path, *arguments, options) ⇒ Boolean

    Run the program with the given arguments and options.

    Parameters:

    • path (Pathname, String)

      The path of the program to run.

    • arguments (Array)

      Additional arguments to run the program with.

    • options (Hash)

      Additional options to execute the program with.

Returns:

  • (Boolean)

    Specifies whether the program exited successfully.

Raises:

  • (ProgramNotFound)

    Indicates that the sudo program could not be located.

See Also:

Since:

  • 0.1.8



308
309
310
311
312
313
314
# File 'lib/rprogram/system.rb', line 308

def System.sudo(*arguments)
  unless sudo?
    raise(ProgramNotFound,'could not find the "sudo" program')
  end

  return run(sudo_path,*arguments)
end

.sudo?Boolean

Determines whether sudo is available on the system.

Returns:

  • (Boolean)

    Specifies whether the sudo program is installed on the system.

Since:

  • 0.3.0



270
271
272
# File 'lib/rprogram/system.rb', line 270

def System.sudo?
  !sudo_path.nil?
end

.sudo_pathPathname?

The path to the sudo program.

Returns:

  • (Pathname, nil)

    The path to the sudo program.

Since:

  • 0.3.0



243
244
245
# File 'lib/rprogram/system.rb', line 243

def System.sudo_path
  @sudo ||= find_program('sudo')
end

.sudo_path=(path) ⇒ Pathanme

Sets the path to the sudo program.

Parameters:

  • path (String, Pathname)

    The new path to use.

Returns:

  • (Pathanme)

    The new path to the sudo program.

Since:

  • 0.3.0



258
259
260
# File 'lib/rprogram/system.rb', line 258

def System.sudo_path=(path)
  @sudo = Pathname.new(path)
end

.windows?Boolean

Determines if the platform is Windows.

Returns:

  • (Boolean)

    Specifies whether the platform is Windows.

Since:

  • 0.3.0



54
55
56
57
58
59
60
# File 'lib/rprogram/system.rb', line 54

def System.windows?
  if @platform
    @platform.include?('mingw') || @platform.include?('mswin')
  else
    false
  end
end