Skip to content

Commit 823fe4a

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

5 files changed

+46
-65
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

-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ def download
3939
def install
4040
# Move only the content of the directory
4141
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
4442
end
4543

4644
# The local filename of the desired IDE package (zip/tar/etc)
@@ -64,8 +62,6 @@ def extract
6462
file.extract(file.name)
6563
end
6664
end
67-
# clean up the no longer required zip
68-
FileUtils.rm_rf package_file
6965
end
7066

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

0 commit comments

Comments
 (0)