Class: Gel::Command::Exec

Inherits:
Gel::Command show all
Defined in:
lib/gel/command/exec.rb

Instance Attribute Summary

Attributes inherited from Gel::Command

#reraise

Instance Method Summary collapse

Methods inherited from Gel::Command

extract_word, handle_error, run

Instance Method Details

#execute_inline?(expanded_command) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/gel/command/exec.rb', line 59

def execute_inline?(expanded_command)
  if File.exist?(expanded_command) && File.executable?(expanded_command)
    File.open(expanded_command, "rb") do |f|
      f.read(2) == "#!" && f.gets.chomp =~ /\bruby\b/
    end
  end
end

#expand_executable(original_command) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gel/command/exec.rb', line 40

def expand_executable(original_command)
  if original_command.include?(File::SEPARATOR) || (File::ALT_SEPARATOR && original_command.include?(File::ALT_SEPARATOR))
    return [File.expand_path(original_command), :path]
  end

  if source = Gel::Environment.activate_for_executable([original_command])
    if found = Gel::Environment.find_executable(original_command)
      return [found, source]
    end
  end

  path_attempts = ENV["PATH"].split(File::PATH_SEPARATOR).map { |e| File.join(e, original_command) }
  if found = path_attempts.find { |path| File.executable?(path) }
    return [File.expand_path(found), :path]
  end

  [original_command, :original]
end

#run(command_line, from_stub: false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gel/command/exec.rb', line 4

def run(command_line, from_stub: false)
  original_command = command_line.shift
  expanded_command, command_source = expand_executable(original_command)

  if from_stub && [:original, :path].include?(command_source)
    raise Gel::Error::BrokenStubError.new(name: original_command)
  end

  gemfile = Gel::Environment.find_gemfile(error: false)

  if gemfile && command_source != :gem
    ENV["GEL_GEMFILE"] = File.expand_path(gemfile)
    ENV["GEL_LOCKFILE"] = File.expand_path(Gel::Environment.lockfile_name(gemfile))
  end

  ENV["RUBYLIB"] = Gel::Environment.modified_rubylib

  if execute_inline?(expanded_command)
    if command_source == :path || command_source == :original
      if ENV["GEL_LOCKFILE"]
        Gel::Environment.activate(output: $stderr)
      end
    end

    $0 = original_command
    ARGV.replace(command_line)

    # Any error after this point should bypass Gel's error
    # handling
    self.reraise = true
    Kernel.load(expanded_command)
  else
    Kernel.exec([original_command, expanded_command], *command_line)
  end
end