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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pdk/cli/exec.rb', line 74

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.



68
69
70
# File 'lib/pdk/cli/exec.rb', line 68

def argv
  @argv
end

#contextObject

Returns the value of attribute context.



69
70
71
# File 'lib/pdk/cli/exec.rb', line 69

def context
  @context
end

#environmentObject

Returns the value of attribute environment.



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

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.



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

def exec_group=(value)
  @exec_group = value
end

#timeoutObject

Returns the value of attribute timeout.



70
71
72
# File 'lib/pdk/cli/exec.rb', line 70

def timeout
  @timeout
end

Instance Method Details

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



112
113
114
115
116
117
118
# File 'lib/pdk/cli/exec.rb', line 112

def add_spinner(message, opts = {})
  return unless PDK::CLI::Util.interactive?
  @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
# 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

  @process.environment['BUNDLE_IGNORE_CONFIG'] = '1'

  if [:module, :pwd].include?(context)
    @process.environment['GEM_HOME'] = PDK::Util::RubyVersion.gem_home
    @process.environment['GEM_PATH'] = PDK::Util::RubyVersion.gem_path

    # 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'] = [
      PDK::Util::RubyVersion.bin_path,
      File.join(@process.environment['GEM_HOME'], 'bin'),
      PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path| File.join(gem_path, 'bin') },
      package_binpath,
      ENV['PATH'],
      PDK::Util.package_install? ? PDK::Util::Git.git_paths : nil,
    ].compact.flatten.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 || context == :pwd
      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



104
105
106
107
108
109
110
# File 'lib/pdk/cli/exec.rb', line 104

def register_spinner(spinner, opts = {})
  return unless PDK::CLI::Util.interactive?
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

  @spinner = spinner
end

#update_environment(additional_env) ⇒ Object



120
121
122
# File 'lib/pdk/cli/exec.rb', line 120

def update_environment(additional_env)
  @environment.merge!(additional_env)
end