Skip to content

Commit dc2b35f

Browse files
committed
add ability to capture preferences
1 parent c3237b0 commit dc2b35f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
### Added
99
- `ArduinoInstallation` class for managing lib / executable paths
1010
- `DisplayManager` class for managing Xvfb instance if needed
11-
- `ArduinoCmd` can report on whether a board is installed
11+
- `ArduinoCmd` captures and caches preferences
12+
- `ArduinoCmd` reports on whether a board is installed
1213

1314
### Changed
1415
- `DisplayManger.with_display` doesn't `disable` if the display was enabled prior to starting the block

lib/arduino_ci/arduino_cmd.rb

+44-4
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,57 @@ def autolocate!
2222
end
2323

2424
attr_accessor :installation
25+
attr_reader :prefs_cache
26+
attr_reader :prefs_response_time
2527

2628
# @param installation [ArduinoInstallation] the location of the Arduino program installation
2729
def initialize(installation)
28-
@display_mgr = DisplayManager::instance
29-
@installation = installation
30+
@display_mgr = DisplayManager::instance
31+
@installation = installation
32+
@prefs_response_time = nil
33+
@prefs_cache = prefs
34+
end
35+
36+
# fetch preferences to a hash
37+
def prefs
38+
resp = nil
39+
@display_mgr.with_display do
40+
start = Time.now
41+
resp = run_and_capture("--get-pref")
42+
@prefs_response_time = Time.now - start
43+
puts "prefs_response_time = #{@prefs_response_time} = #{Time.now} - #{start}"
44+
end
45+
return nil unless resp[:success]
46+
lines = resp[:out].split("\n").select { |l| l.include? "=" }
47+
ret = lines.each_with_object({}) do |e, acc|
48+
parts = e.split("=", 2)
49+
acc[parts[0]] = parts[1]
50+
acc
51+
end
52+
ret
3053
end
3154

3255
# run the arduino command
33-
def run(*args)
56+
# @return [Hash] {:out => StringIO, :err => StringIO }
57+
def run(*args, **kwargs)
3458
full_args = [@installation.cmd_path] + args
35-
@display_mgr.run(*full_args)
59+
@display_mgr.run(*full_args, **kwargs)
60+
61+
end
62+
63+
# run a command and capture its output
64+
# @return [Hash] {:out => StringIO, :err => StringIO, :success => bool}
65+
def run_and_capture(*args)
66+
pipe_out, pipe_out_wr = IO.pipe
67+
pipe_err, pipe_err_wr = IO.pipe
68+
success = run(*args, out: pipe_out_wr, err: pipe_err_wr)
69+
pipe_out_wr.close
70+
pipe_err_wr.close
71+
str_out = pipe_out.read
72+
str_err = pipe_err.read
73+
pipe_out.close
74+
pipe_err.close
75+
{ out: str_out, err: str_err, success: success }
3676
end
3777

3878
def board_installed?(board)

spec/arduino_cmd_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
it "Finds the Arduino executable" do
1212
arduino_cmd = ArduinoCI::ArduinoCmd.autolocate!
1313
expect(arduino_cmd.installation.cmd_path).not_to be nil
14+
expect(arduino_cmd.prefs_cache.class).to be Hash
15+
expect(arduino_cmd.prefs_response_time).not_to be nil
1416
end
1517
end
1618

0 commit comments

Comments
 (0)