Class: PDK::CLI::Exec::Command
- Inherits:
-
Object
- Object
- PDK::CLI::Exec::Command
- 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
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#context ⇒ Object
Returns the value of attribute context.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#exec_group ⇒ Object
writeonly
Sets the attribute exec_group.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add_spinner(message, opts = {}) ⇒ Object
- #execute! ⇒ Object
-
#initialize(*argv) ⇒ Command
constructor
A new instance of Command.
- #register_spinner(spinner, opts = {}) ⇒ Object
Constructor Details
#initialize(*argv) ⇒ Command
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pdk/cli/exec.rb', line 62 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
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
56 57 58 |
# File 'lib/pdk/cli/exec.rb', line 56 def argv @argv end |
#context ⇒ Object
Returns the value of attribute context.
57 58 59 |
# File 'lib/pdk/cli/exec.rb', line 57 def context @context end |
#environment ⇒ Object
Returns the value of attribute environment.
59 60 61 |
# File 'lib/pdk/cli/exec.rb', line 59 def environment @environment end |
#exec_group=(value) ⇒ Object (writeonly)
Sets the attribute exec_group
60 61 62 |
# File 'lib/pdk/cli/exec.rb', line 60 def exec_group=(value) @exec_group = value end |
#timeout ⇒ Object
Returns the value of attribute timeout.
58 59 60 |
# File 'lib/pdk/cli/exec.rb', line 58 def timeout @timeout end |
Instance Method Details
#add_spinner(message, opts = {}) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/pdk/cli/exec.rb', line 100 def add_spinner(, opts = {}) return unless PDK::CLI::Util.interactive? = opts.delete(:success) = opts.delete(:failure) @spinner = TTY::Spinner.new("[:spinner] #{message}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform)) end |
#execute! ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 |
# File 'lib/pdk/cli/exec.rb', line 108 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'], 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 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
92 93 94 95 96 97 98 |
# File 'lib/pdk/cli/exec.rb', line 92 def register_spinner(spinner, opts = {}) return unless PDK::CLI::Util.interactive? = opts.delete(:success) = opts.delete(:failure) @spinner = spinner end |