class Conductor::Script

Script runner

Attributes

args[R]
path[R]

Public Class Methods

new(script) click to toggle source

Initializes the given script.

@param script The script/path

# File lib/conductor/script.rb, line 13
def initialize(script)
  parts = Shellwords.split(script)
  self.path = parts[0]
  self.args = parts[1..].join(" ")
end

Public Instance Methods

args=(array) click to toggle source

Set the args array

@param array The array

# File lib/conductor/script.rb, line 45
def args=(array)
  @args = if array.is_a?(Array)
            array.join(" ")
          else
            array
          end
end
path=(path) click to toggle source

Set script path, automatically expands/tests

@param path The path

# File lib/conductor/script.rb, line 24
def path=(path)
  @path = if %r{^[%/]}.match?(path)
            File.expand_path(path)
          else
            script_dir = File.expand_path("~/.config/conductor/scripts")
            if File.exist?(File.join(script_dir, path))
              File.join(script_dir, path)
            elsif TTY::Which.exist?(path)
              TTY::Which.which(path)
            else
              raise "Path to #{path} not found"

            end
          end
end
run() click to toggle source

Execute the script

@return [String] script results (STDOUT)

# File lib/conductor/script.rb, line 58
def run
  stdin = Conductor.stdin

  raise "Script path not defined" unless @path

  raise "Script not executable" unless File.executable?(@path)

  use_stdin = true
  if /\$\{?file\}?/.match?(args)
    use_stdin = false
    args.sub!(/\$\{?file\}?/, Env.env[:filepath])
  else
    raise "No input" unless stdin

  end

  if use_stdin
    `echo #{Shellwords.escape(stdin)} | #{Env} #{@path} #{@args}`
  else
    `#{Env} #{@path} #{@args}`
  end
end