Skip to content

Commit 97953c3

Browse files
committed
Refactor force_install code in preparation for windows CI
1 parent 4320bf1 commit 97953c3

5 files changed

+132
-22
lines changed

lib/arduino_ci/arduino_downloader.rb

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def initialize(desired_ide_version)
1010
end
1111

1212
# Provide guidelines to the implementer of this class
13-
def must_implement(method)
14-
raise NoMethodError("#{self.class.name} failed to implement ArduinoDownloader.#{method}")
13+
def self.must_implement(method)
14+
raise NotImplementedError, "#{self.class.name} failed to implement ArduinoDownloader.#{method}"
1515
end
1616

1717
# Make any preparations or run any checks prior to making changes
@@ -23,7 +23,7 @@ def prepare
2323
# The autolocated executable of the installation
2424
#
2525
# @return [string] or nil
26-
def self.autolocation
26+
def self.autolocated_executable
2727
# Arbitrarily, I'm going to pick the force installed location first
2828
# if it exists. I'm not sure why we would have both, but if we did
2929
# a force install then let's make sure we actually use it.
@@ -36,30 +36,52 @@ def self.autolocation
3636
nil
3737
end
3838

39+
# The autolocated directory of the installation
40+
#
41+
# @return [string] or nil
42+
def self.autolocated_installation
43+
# Arbitrarily, I'm going to pick the force installed location first
44+
# if it exists. I'm not sure why we would have both, but if we did
45+
# a force install then let's make sure we actually use it.
46+
locations = [self.force_install_location, self.existing_installation]
47+
locations.each do |loc|
48+
next if loc.nil?
49+
next unless File.exist? loc
50+
return loc
51+
end
52+
nil
53+
end
54+
55+
# The path to the directory of an existing installation, or nil
56+
# @return [string]
57+
def self.existing_installation
58+
self.must_implement(__method__)
59+
end
60+
3961
# The executable Arduino file in an existing installation, or nil
4062
# @return [string]
4163
def self.existing_executable
42-
must_implement(__method__)
64+
self.must_implement(__method__)
4365
end
4466

4567
# The executable Arduino file in a forced installation, or nil
4668
# @return [string]
4769
def self.force_installed_executable
48-
must_implement(__method__)
70+
self.must_implement(__method__)
4971
end
5072

5173
# The technology that will be used to complete the download
5274
# (for logging purposes)
5375
# @return [string]
5476
def downloader
55-
must_implement(__method__)
77+
self.class.must_implement(__method__)
5678
end
5779

5880
# The technology that will be used to extract the download
5981
# (for logging purposes)
6082
# @return [string]
6183
def extracter
62-
must_implement(__method__)
84+
self.class.must_implement(__method__)
6385
end
6486

6587
# The URL of the desired IDE package (zip/tar/etc) for this platform
@@ -71,13 +93,13 @@ def package_url
7193
# The local file (dir) name of the desired IDE package (zip/tar/etc)
7294
# @return [string]
7395
def package_file
74-
must_implement(__method__)
96+
self.class.must_implement(__method__)
7597
end
7698

7799
# The local filename of the extracted IDE package (zip/tar/etc)
78100
# @return [string]
79101
def extracted_file
80-
must_implement(__method__)
102+
self.class.must_implement(__method__)
81103
end
82104

83105
# @return [String] The location where a forced install will go
@@ -88,19 +110,19 @@ def self.force_install_location
88110
# Download the package_url to package_file, and maybe print a line of dots......
89111
# @return [bool] whether successful
90112
def download
91-
must_implement(__method__)
113+
self.class.must_implement(__method__)
92114
end
93115

94116
# Extract the package_file to extracted_file
95117
# @return [bool] whether successful
96118
def extract
97-
must_implement(__method__)
119+
self.class.must_implement(__method__)
98120
end
99121

100122
# Move the extracted package file from extracted_file to the force_install_location
101123
# @return [bool] whether successful
102124
def install
103-
must_implement(__method__)
125+
self.class.must_implement(__method__)
104126
end
105127

106128
# Forcibly install Arduino on linux from the web

lib/arduino_ci/arduino_downloader_linux.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def extracted_file
3232
"arduino-#{@desired_ide_version}"
3333
end
3434

35+
# The path to the directory of an existing installation, or nil
36+
# @return [string]
37+
def self.existing_installation
38+
exe = self.existing_executable
39+
return nil if exe.nil?
40+
File.dirname(exe) # it's not really this
41+
# but for this platform it doesn't really matter
42+
end
43+
3544
# The executable Arduino file in an existing installation, or nil
3645
# @return [string]
3746
def self.existing_executable

lib/arduino_ci/arduino_downloader_osx.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ArduinoDownloaderOSX < ArduinoDownloaderPosix
88
# The local filename of the desired IDE package (zip/tar/etc)
99
# @return [string]
1010
def package_file
11-
"#arduino-#{@desired_ide_version}-macosx.zip"
11+
"arduino-#{@desired_ide_version}-macosx.zip"
1212
end
1313

1414
# The technology that will be used to extract the download
@@ -30,17 +30,33 @@ def extracted_file
3030
"Arduino.app"
3131
end
3232

33-
# The an existing Arduino file in one of the given directories, or nil
33+
# An existing Arduino directory in one of the given directories, or nil
34+
# @param Array<string> a list of places to look
35+
# @return [string]
36+
def self.find_existing_arduino_dir(paths)
37+
paths.each do |path|
38+
return path if File.exist? path
39+
end
40+
nil
41+
end
42+
43+
# An existing Arduino file in one of the given directories, or nil
3444
# @param Array<string> a list of places to look for the executable
3545
# @return [string]
3646
def self.find_existing_arduino_exe(paths)
3747
paths.each do |path|
38-
exe = File.join(path, "Arduino")
48+
exe = File.join(path, "MacOS", "Arduino")
3949
return exe if File.exist? exe
4050
end
4151
nil
4252
end
4353

54+
# The path to the directory of an existing installation, or nil
55+
# @return [string]
56+
def self.existing_installation
57+
self.find_existing_arduino_dir(["/Applications/Arduino.app/Contents"])
58+
end
59+
4460
# The executable Arduino file in an existing installation, or nil
4561
# @return [string]
4662
def self.existing_executable

lib/arduino_ci/arduino_installation.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,23 @@ def autolocate
2424
when :osx then
2525
ret = autolocate_osx
2626
when :linux then
27-
loc = ArduinoDownloaderLinux.autolocation
27+
loc = ArduinoDownloaderLinux.autolocated_executable
2828
return nil if loc.nil?
2929
ret = ArduinoCmdLinux.new
3030
ret.base_cmd = [loc]
31+
# when :windows then
32+
# ArduinoDownloaderWindows.autolocation
33+
# return nil if loc.nil?
34+
# ret = ArduinoCmdWindows.new
35+
# ret.base_cmd = [loc]
3136
end
3237
ret
3338
end
3439

3540
# @return [ArduinoCI::ArduinoCmdOSX] an instance of the command or nil if it can't be found
3641
def autolocate_osx
37-
osx_root = ArduinoDownloaderOSX.autolocation
42+
osx_root = ArduinoDownloaderOSX.autolocated_installation
43+
return nil if osx_root.nil?
3844
return nil unless File.exist? osx_root
3945

4046
launchers = [

spec/arduino_downloader_spec.rb

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,74 @@
33
DESIRED_VERSION = "rhubarb"
44
RSpec.describe ArduinoCI::ArduinoDownloader do
55
context "Basics" do
6-
it "can instantiate" do
7-
ArduinoCI::ArduinoDownloader.new(DESIRED_VERSION)
6+
it "has correct class properties" do
7+
ad = ArduinoCI::ArduinoDownloader
8+
9+
expect{ad.autolocated_executable}.to raise_error(NotImplementedError)
10+
expect{ad.autolocated_installation}.to raise_error(NotImplementedError)
11+
expect{ad.existing_installation}.to raise_error(NotImplementedError)
12+
expect{ad.existing_executable}.to raise_error(NotImplementedError)
13+
expect{ad.force_installed_executable}.to raise_error(NotImplementedError)
14+
expect(ad.force_install_location).to eq(File.join(ENV['HOME'], 'arduino_ci_ide'))
815
end
9-
end
1016

17+
it "has correct instance properties" do
18+
ad = ArduinoCI::ArduinoDownloader.new(DESIRED_VERSION)
19+
expect(ad.prepare).to be nil
20+
expect{ad.downloader}.to raise_error(NotImplementedError)
21+
expect{ad.extracter}.to raise_error(NotImplementedError)
22+
expect{ad.package_url}.to raise_error(NotImplementedError)
23+
expect{ad.package_file}.to raise_error(NotImplementedError)
24+
end
25+
end
1126
end
27+
1228
RSpec.describe ArduinoCI::ArduinoDownloaderLinux do
1329
context "Basics" do
14-
it "can instantiate" do
15-
ArduinoCI::ArduinoDownloaderLinux.new(DESIRED_VERSION)
30+
it "has correct class properties" do
31+
ad = ArduinoCI::ArduinoDownloaderLinux
32+
# these will vary with CI. Don't test them.
33+
# expect(ad.autolocated_executable).to be nil
34+
# expect(ad.autolocated_installation).to be nil
35+
# expect(ad.existing_installation).to be nil
36+
# expect(ad.existing_executable).to be nil
37+
# expect(ad.force_installed_executable).to be nil
38+
39+
expect(ad.force_install_location).to eq(File.join(ENV['HOME'], 'arduino_ci_ide'))
40+
end
41+
42+
it "has correct instance properties" do
43+
ad = ArduinoCI::ArduinoDownloaderLinux.new(DESIRED_VERSION)
44+
expect(ad.prepare).to be nil
45+
expect(ad.downloader).to eq("wget")
46+
expect(ad.extracter).to eq("tar")
47+
expect(ad.package_url).to eq("https://downloads.arduino.cc/arduino-rhubarb-linux64.tar.xz")
48+
expect(ad.package_file).to eq("arduino-rhubarb-linux64.tar.xz")
1649
end
1750
end
51+
end
52+
53+
RSpec.describe ArduinoCI::ArduinoDownloaderOSX do
54+
context "Basics" do
55+
it "has correct class properties" do
56+
ad = ArduinoCI::ArduinoDownloaderOSX
57+
# these will vary with CI. Don't test them.
58+
# expect(ad.autolocated_executable).to be nil
59+
# expect(ad.autolocated_installation).to be nil
60+
# expect(ad.existing_installation).to be nil
61+
# expect(ad.existing_executable).to be nil
62+
# expect(ad.force_installed_executable).to be nil
1863

64+
expect(ad.force_install_location).to eq(File.join(ENV['HOME'], 'arduino_ci_ide'))
65+
end
66+
67+
it "has correct instance properties" do
68+
ad = ArduinoCI::ArduinoDownloaderOSX.new(DESIRED_VERSION)
69+
expect(ad.prepare).to be nil
70+
expect(ad.downloader).to eq("wget")
71+
expect(ad.extracter).to eq("unzip")
72+
expect(ad.package_url).to eq("https://downloads.arduino.cc/arduino-rhubarb-macosx.zip")
73+
expect(ad.package_file).to eq("arduino-rhubarb-macosx.zip")
74+
end
75+
end
1976
end

0 commit comments

Comments
 (0)