Class: PDK::CLI::Exec::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/cli/exec.rb

Overview

TODO: decide how/when to connect stdin to child process for things like pry TODO: need a way to set callbacks on new stdout/stderr data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv) ⇒ Command

Returns a new instance of Command.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pdk/cli/exec.rb', line 59

def initialize(*argv)
  @argv = argv

  @process = ChildProcess.build(*@argv)
  @process.leader = true

  @stdout = Tempfile.new('stdout').tap { |io| io.sync = true }
  @stderr = Tempfile.new('stderr').tap { |io| io.sync = true }

  @process.io.stdout = @stdout
  @process.io.stderr = @stderr

  # Default to running things in the system context.
  @context = :system
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



55
56
57
# File 'lib/pdk/cli/exec.rb', line 55

def argv
  @argv
end

#contextObject

Returns the value of attribute context.



56
57
58
# File 'lib/pdk/cli/exec.rb', line 56

def context
  @context
end

#timeoutObject

Returns the value of attribute timeout.



57
58
59
# File 'lib/pdk/cli/exec.rb', line 57

def timeout
  @timeout
end

Instance Method Details

#add_spinner(message, opts = {}) ⇒ Object



83
84
85
86
87
88
# File 'lib/pdk/cli/exec.rb', line 83

def add_spinner(message, opts = {})
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

  @spinner = TTY::Spinner.new("[:spinner] #{message}", opts)
end

#execute!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pdk/cli/exec.rb', line 90

def execute!
  # Start spinning if configured.
  @spinner.auto_spin if @spinner

  if context == :module
    # TODO: we should probably more carefully manage PATH and maybe other things too
    @process.environment['GEM_HOME'] = File.join(PDK::Util.cachedir, 'bundler', 'ruby', RbConfig::CONFIG['ruby_version'])
    @process.environment['GEM_PATH'] = pdk_gem_path

    Dir.chdir(PDK::Util.module_root) do
      ::Bundler.with_clean_env do
        run_process!
      end
    end
  else
    run_process!
  end

  # Stop spinning when done (if configured).
  if @spinner
    if @process.exit_code.zero?
      @spinner.success(@success_message || '')
    else
      @spinner.error(@failure_message || '')
    end
  end

  @stdout.rewind
  @stderr.rewind

  process_data = {
    stdout: @stdout.read,
    stderr: @stderr.read,
    exit_code: @process.exit_code,
  }

  return process_data
ensure
  @stdout.close
  @stderr.close
end