Class: CurriculumGenerator::Util::ShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/curriculum-generator/util/shell_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, execution_dir, log_file = nil) ⇒ ShellCommand



5
6
7
8
9
# File 'lib/curriculum-generator/util/shell_command.rb', line 5

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

Class Method Details

.exist?(command) ⇒ Boolean



31
32
33
34
35
36
37
38
39
40
# File 'lib/curriculum-generator/util/shell_command.rb', line 31

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

Instance Method Details

#exist?Boolean



42
43
44
# File 'lib/curriculum-generator/util/shell_command.rb', line 42

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

#find_executable_part(command) ⇒ Object



46
47
48
# File 'lib/curriculum-generator/util/shell_command.rb', line 46

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

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/curriculum-generator/util/shell_command.rb', line 11

def run
  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