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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pdk/cli/exec.rb', line 78

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

  # Extra environment vars to add to base set.
  @environment = {}

  # Register the ExecGroup when running in parallel
  @exec_group = nil
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



72
73
74
# File 'lib/pdk/cli/exec.rb', line 72

def argv
  @argv
end

#contextObject

Returns the value of attribute context.



73
74
75
# File 'lib/pdk/cli/exec.rb', line 73

def context
  @context
end

#environmentObject

Returns the value of attribute environment.



75
76
77
# File 'lib/pdk/cli/exec.rb', line 75

def environment
  @environment
end

#exec_group=(value) ⇒ Object (writeonly)

Sets the attribute exec_group

Parameters:

  • value

    the value to set the attribute exec_group to.



76
77
78
# File 'lib/pdk/cli/exec.rb', line 76

def exec_group=(value)
  @exec_group = value
end

#timeoutObject

Returns the value of attribute timeout.



74
75
76
# File 'lib/pdk/cli/exec.rb', line 74

def timeout
  @timeout
end

Instance Method Details

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



116
117
118
119
120
121
122
# File 'lib/pdk/cli/exec.rb', line 116

def add_spinner(message, opts = {})
  return if PDK.logger.debug?
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

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

#execute!Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pdk/cli/exec.rb', line 124

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

  # Add custom env vars.
  @environment.each do |k, v|
    @process.environment[k] = v
  end

  if context == :module
    # `bundle install --path` ignores all "system" installed gems and causes unnecessary package installs
    # `bundle install` (without --path) installs into GEM_HOME, which by default is non-user writeable
    # To still use the pre-installed packages, but allow folks to install additional gems
    # we set GEM_HOME to the user's cachedir, and put all other cache locations onto GEM_PATH
    # See https://stackoverflow.com/a/11277228 for background
    @process.environment['GEM_HOME'] = File.join(PDK::Util.cachedir, 'ruby', RbConfig::CONFIG['ruby_version'])

    if PDK::Util.package_install?
      # Subprocesses use their own set of gems which are managed by pdk or installed with the package.
      @process.environment['GEM_PATH'] = File.join(PDK::Util.package_cachedir, 'ruby', RbConfig::CONFIG['ruby_version'])
    else
      # This allows the subprocess to find the 'bundler' gem, which isn't in the cachedir above for gem installs.
      bundler_gem_path = File.absolute_path(File.join(`gem which bundler`, '..', '..', '..', '..'))
      @process.environment['GEM_PATH'] = bundler_gem_path
    end

    # Make sure invocation of Ruby prefers our private installation.
    package_binpath = PDK::Util.package_install? ? File.join(PDK::Util.pdk_package_basedir, 'bin') : nil
    @process.environment['PATH'] = [
      RbConfig::CONFIG['bindir'],
      File.join(@process.environment['GEM_HOME'], 'bin'),
      File.join(@process.environment['GEM_PATH'], 'bin'),
      package_binpath,
      ENV['PATH'],
    ].compact.join(File::PATH_SEPARATOR)

    mod_root = PDK::Util.module_root

    unless mod_root
      @spinner.error if @spinner

      raise PDK::CLI::FatalError, _('Current working directory is not part of a module. (No metadata.json was found.)')
    end

    if Dir.pwd == mod_root
      run_process_in_clean_env!
    else
      Dir.chdir(mod_root) do
        run_process_in_clean_env!
      end
    end
  else
    run_process!
  end

  # Stop spinning when done (if configured).
  stop_spinner

  @stdout.rewind
  @stderr.rewind

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

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

#register_spinner(spinner, opts = {}) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/pdk/cli/exec.rb', line 108

def register_spinner(spinner, opts = {})
  return if PDK.logger.debug?
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

  @spinner = spinner
end