Sha256: 1f771783c9522e06adede02d0815c1d4398397b7733b4519871a8f05ddca3a7c

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

class CGen::Util::ShellCommand

  def initialize(command, execution_dir, log_file=nil)
    @command = command
    @execution_dir = execution_dir
    @log_file = log_file
  end

  def run
    CGen::Util::Logging.log(:executing_command, cmd: @command, exec_dir: @execution_dir, log_file: @log_file)

    status = true

    Process.waitpid(
        fork do
          original_stdout, original_stderr = $stdout, $stderr
          FileUtils.chdir @execution_dir do
            File.open(@log_file, 'a') do |log_file|
              $stderr = $stdout = log_file
              system @command
              $stdout, $stderr = original_stdout, original_stderr
            end
          end
        end)

    status # return
  end

  def self.exist?(command)
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
      exts.collect do |ext|
        exe = File.join(path, "#{command}#{ext}")
        return true if File.executable? exe
      end
    end
    false # return
  end

  def exist?
    self.class.exist? @command
  end

  def find_executable_part(command)
    command.split(' ').first
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
curriculum-generator-1.0.4 lib/cgen/util/shell_command.rb
curriculum-generator-1.0.3 lib/cgen/util/shell_command.rb
curriculum-generator-1.0.2 lib/cgen/util/shell_command.rb
curriculum-generator-1.0.1 lib/cgen/util/shell_command.rb
curriculum-generator-1.0.0 lib/cgen/util/shell_command.rb