Skip to content

Commit 4c149da

Browse files
committed
centralize download and unzip logic into ruby libs
1 parent 048705e commit 4c149da

6 files changed

+49
-72
lines changed

lib/arduino_ci/arduino_downloader.rb

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
require "fileutils"
2+
require 'open-uri'
3+
require 'zip'
4+
15
DOWNLOAD_ATTEMPTS = 3
26

37
module ArduinoCI
@@ -74,14 +78,14 @@ def self.force_installed_executable
7478
# (for logging purposes)
7579
# @return [string]
7680
def downloader
77-
self.class.must_implement(__method__)
81+
"open-uri"
7882
end
7983

8084
# The technology that will be used to extract the download
8185
# (for logging purposes)
8286
# @return [string]
8387
def extracter
84-
self.class.must_implement(__method__)
88+
"Zip"
8589
end
8690

8791
# The URL of the desired IDE package (zip/tar/etc) for this platform
@@ -107,22 +111,44 @@ def self.force_install_location
107111
File.join(ENV['HOME'], 'arduino_ci_ide')
108112
end
109113

110-
# Download the package_url to package_file, and maybe print a line of dots......
114+
# Download the package_url to package_file
111115
# @return [bool] whether successful
112116
def download
113-
self.class.must_implement(__method__)
117+
# Turned off ssl verification
118+
# This should be acceptable because it won't happen on a user's machine, just CI
119+
120+
# define a progress-bar printer
121+
chunk_size = 1024 * 1024 * 1024
122+
total_size = 0
123+
dots = 0
124+
dot_printer = lambda do |size|
125+
total_size += size
126+
needed_dots = (total_size / chunk_size).to_i
127+
unprinted_dots = needed_dots - dots
128+
print("." * unprinted_dots) if unprinted_dots > 0
129+
dots = needed_dots
130+
end
131+
132+
open(package_url, ssl_verify_mode: 0, progress_proc: dot_printer) do |url|
133+
File.open(package_file, 'wb') { |file| file.write(url.read) }
134+
end
114135
end
115136

116137
# Extract the package_file to extracted_file
117138
# @return [bool] whether successful
118139
def extract
119-
self.class.must_implement(__method__)
140+
Zip::File.open(package_file) do |zip|
141+
zip.each do |file|
142+
file.extract(file.name)
143+
end
144+
end
120145
end
121146

122147
# Move the extracted package file from extracted_file to the force_install_location
123148
# @return [bool] whether successful
124149
def install
125-
self.class.must_implement(__method__)
150+
# Move only the content of the directory
151+
FileUtils.mv extracted_file, self.class.force_install_location
126152
end
127153

128154
# Forcibly install Arduino on linux from the web

lib/arduino_ci/arduino_downloader_linux.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
require "arduino_ci/arduino_downloader_posix"
1+
require "arduino_ci/arduino_downloader"
22

33
USE_BUILDER = false
44

55
module ArduinoCI
66

77
# Manage the linux download & install of Arduino
8-
class ArduinoDownloaderLinux < ArduinoDownloaderPosix
8+
class ArduinoDownloaderLinux < ArduinoDownloader
99

1010
# The local filename of the desired IDE package (zip/tar/etc)
1111
# @return [string]
1212
def package_file
1313
"#{extracted_file}-linux64.tar.xz"
1414
end
1515

16+
# Make any preparations or run any checks prior to making changes
17+
# @return [string] Error message, or nil if success
18+
def prepare
19+
reqs = [extracter]
20+
reqs.each do |req|
21+
return "#{req} does not appear to be installed!" unless Host.which(req)
22+
end
23+
nil
24+
end
25+
1626
# The technology that will be used to extract the download
1727
# (for logging purposes)
1828
# @return [string]

lib/arduino_ci/arduino_downloader_osx.rb

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
require "arduino_ci/arduino_downloader_posix"
1+
require "arduino_ci/arduino_downloader"
22

33
module ArduinoCI
44

55
# Manage the OSX download & install of Arduino
6-
class ArduinoDownloaderOSX < ArduinoDownloaderPosix
6+
class ArduinoDownloaderOSX < ArduinoDownloader
77

88
# The local filename of the desired IDE package (zip/tar/etc)
99
# @return [string]
1010
def package_file
1111
"arduino-#{@desired_ide_version}-macosx.zip"
1212
end
1313

14-
# The technology that will be used to extract the download
15-
# (for logging purposes)
16-
# @return [string]
17-
def extracter
18-
"unzip"
19-
end
20-
21-
# Extract the package_file to extracted_file
22-
# @return [bool] whether successful
23-
def extract
24-
system(extracter, package_file)
25-
end
26-
2714
# The local file (dir) name of the extracted IDE package (zip/tar/etc)
2815
# @return [string]
2916
def extracted_file

lib/arduino_ci/arduino_downloader_posix.rb

-38
This file was deleted.

lib/arduino_ci/arduino_downloader_windows.rb

-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
require 'shellwords' # fingers crossed this works on win32
33
require 'win32/registry'
44
require "arduino_ci/arduino_downloader"
5-
require 'open-uri'
6-
require 'zip'
75
require "fileutils"
86

97
module ArduinoCI
@@ -39,8 +37,6 @@ def download
3937
def install
4038
# Move only the content of the directory
4139
FileUtils.mv extracted_file, self.class.force_install_location
42-
# clean up the no longer required root extracted folder
43-
FileUtils.rm_rf extracted_file
4440
end
4541

4642
# The local filename of the desired IDE package (zip/tar/etc)
@@ -64,8 +60,6 @@ def extract
6460
file.extract(file.name)
6561
end
6662
end
67-
# clean up the no longer required zip
68-
FileUtils.rm_rf package_file
6963
end
7064

7165
# The local file (dir) name of the extracted IDE package (zip/tar/etc)

spec/arduino_downloader_spec.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
it "has correct instance properties" do
1818
ad = ArduinoCI::ArduinoDownloader.new(DESIRED_VERSION)
1919
expect(ad.prepare).to be nil
20-
expect{ad.downloader}.to raise_error(NotImplementedError)
21-
expect{ad.extracter}.to raise_error(NotImplementedError)
2220
expect{ad.package_url}.to raise_error(NotImplementedError)
2321
expect{ad.package_file}.to raise_error(NotImplementedError)
2422
end
@@ -42,7 +40,7 @@
4240
it "has correct instance properties" do
4341
ad = ArduinoCI::ArduinoDownloaderLinux.new(DESIRED_VERSION)
4442
expect(ad.prepare).to be nil
45-
expect(ad.downloader).to eq("wget")
43+
expect(ad.downloader).to eq("open-uri")
4644
expect(ad.extracter).to eq("tar")
4745
expect(ad.package_url).to eq("https://downloads.arduino.cc/arduino-rhubarb-linux64.tar.xz")
4846
expect(ad.package_file).to eq("arduino-rhubarb-linux64.tar.xz")
@@ -67,8 +65,8 @@
6765
it "has correct instance properties" do
6866
ad = ArduinoCI::ArduinoDownloaderOSX.new(DESIRED_VERSION)
6967
expect(ad.prepare).to be nil
70-
expect(ad.downloader).to eq("wget")
71-
expect(ad.extracter).to eq("unzip")
68+
expect(ad.downloader).to eq("open-uri")
69+
expect(ad.extracter).to eq("Zip")
7270
expect(ad.package_url).to eq("https://downloads.arduino.cc/arduino-rhubarb-macosx.zip")
7371
expect(ad.package_file).to eq("arduino-rhubarb-macosx.zip")
7472
end

0 commit comments

Comments
 (0)