Skip to content

Commit ebd776b

Browse files
committed
hide chattiness of system commands behind member variables
1 parent 413005b commit ebd776b

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

lib/arduino_ci/arduino_cmd.rb

+25-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def self.flag(name, text = nil)
2020
attr_accessor :base_cmd
2121

2222
attr_reader :library_is_indexed
23+
attr_reader :last_out
24+
attr_reader :last_err
25+
attr_reader :last_msg
2326

2427
# set the command line flags (undefined for now).
2528
# These vary between gui/cli
@@ -32,9 +35,12 @@ def self.flag(name, text = nil)
3235
flag :verify
3336

3437
def initialize
35-
@prefs_cache = {}
36-
@prefs_fetched = false
37-
@library_is_indexed = false
38+
@prefs_cache = {}
39+
@prefs_fetched = false
40+
@library_is_indexed = false
41+
@last_out = ""
42+
@last_err = ""
43+
@last_msg = ""
3844
end
3945

4046
def parse_pref_string(arduino_output)
@@ -94,9 +100,16 @@ def _run(*args, **kwargs)
94100

95101
# build and run the arduino command
96102
def run(*args, **kwargs)
97-
# TODO: detect env!!
98-
full_args = @base_cmd + args
99-
_run(*full_args, **kwargs)
103+
# do some work to extract & merge environment variables if they exist
104+
has_env = !args.empty? && args[0].class == Hash
105+
env_vars = has_env ? args[0] : {}
106+
actual_args = has_env ? args[1..-1] : args # need to shift over if we extracted args
107+
full_args = @base_cmd + actual_args
108+
full_cmd = env_vars.empty? ? full_args : [env_vars] + full_args
109+
110+
shell_vars = env_vars.map { |k, v| "#{k}=#{v}" }.join(" ")
111+
@last_msg = " $ #{shell_vars} #{full_args.join(' ')}"
112+
_run(*full_cmd, **kwargs)
100113
end
101114

102115
# run a command and capture its output
@@ -113,6 +126,8 @@ def run_and_capture(*args, **kwargs)
113126
str_err = pipe_err.read
114127
pipe_out.close
115128
pipe_err.close
129+
@last_err = str_err
130+
@last_out = str_out
116131
{ out: str_out, err: str_err, success: success }
117132
end
118133

@@ -189,11 +204,11 @@ def use_board!(boardname)
189204
def verify_sketch(path)
190205
ext = File.extname path
191206
unless ext.casecmp(".ino").zero?
192-
puts "Refusing to verify sketch with '#{ext}' extension -- rename it to '.ino'!"
207+
@last_msg = "Refusing to verify sketch with '#{ext}' extension -- rename it to '.ino'!"
193208
return false
194209
end
195210
unless File.exist? path
196-
puts "Can't verify nonexistent Sketch at '#{path}'!"
211+
@last_msg = "Can't verify Sketch at nonexistent path '#{path}'!"
197212
return false
198213
end
199214
run(flag_verify, path, err: :out)
@@ -215,11 +230,11 @@ def install_local_library(path)
215230
# maybe it's a symlink? that would be OK
216231
if File.symlink?(destination_path)
217232
return destination_path if File.readlink(destination_path) == realpath
218-
puts "#{uhoh} and it's not symlinked to #{realpath}"
233+
@last_msg = "#{uhoh} and it's not symlinked to #{realpath}"
219234
return nil
220235
end
221236

222-
puts "#{uhoh}. It may need to be removed manually."
237+
@last_msg = "#{uhoh}. It may need to be removed manually."
223238
return nil
224239
end
225240

lib/arduino_ci/host.rb

+1-11
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ def self.which(cmd)
2020

2121
# run a command in a display
2222
def self.run(*args, **kwargs)
23-
# do some work to extract & merge environment variables if they exist
24-
has_env = !args.empty? && args[0].class == Hash
25-
env_vars = has_env ? args[0] : {}
26-
actual_args = has_env ? args[1..-1] : args # need to shift over if we extracted args
27-
full_cmd = env_vars.empty? ? actual_args : [env_vars] + actual_args
28-
shell_vars = env_vars.map { |k, v| "#{k}=#{v}" }.join(" ")
29-
puts " $ #{shell_vars} #{actual_args.join(' ')}"
30-
ret = system(*full_cmd, **kwargs)
31-
status = ret ? "succeeded" : "failed"
32-
puts "Command '#{File.basename(actual_args[0])}' has #{status}"
33-
ret
23+
system(*args, **kwargs)
3424
end
3525

3626
def self.os

0 commit comments

Comments
 (0)