1
- require "fileutils"
1
+ require 'fileutils'
2
+ require 'net/http'
2
3
require 'open-uri'
3
4
require 'zip'
4
5
@@ -9,8 +10,11 @@ module ArduinoCI
9
10
# Manage the OS-specific download & install of Arduino
10
11
class ArduinoDownloader
11
12
12
- def initialize ( desired_ide_version )
13
+ # @param desired_ide_version [string] Version string e.g. 1.8.7
14
+ # @param output [IO] $stdout, $stderr, File.new(/dev/null, 'w'), etc. where console output will be sent
15
+ def initialize ( desired_ide_version , output = $stdout)
13
16
@desired_ide_version = desired_ide_version
17
+ @output = output
14
18
end
15
19
16
20
# Provide guidelines to the implementer of this class
@@ -115,15 +119,15 @@ def download
115
119
total_size += size
116
120
needed_dots = ( total_size / chunk_size ) . to_i
117
121
unprinted_dots = needed_dots - dots
118
- print ( "." * unprinted_dots ) if unprinted_dots > 0
122
+ @output . print ( "." * unprinted_dots ) if unprinted_dots > 0
119
123
dots = needed_dots
120
124
end
121
125
122
126
open ( package_url , ssl_verify_mode : 0 , progress_proc : dot_printer ) do |url |
123
127
File . open ( package_file , 'wb' ) { |file | file . write ( url . read ) }
124
128
end
125
- rescue Net ::OpenTimeout , Net ::ReadTimeout => e
126
- puts "\n Arduino force-install failed downloading #{ package_url } : #{ e } "
129
+ rescue Net ::OpenTimeout , Net ::ReadTimeout , OpenURI :: HTTPError , URI :: InvalidURIError => e
130
+ @output . puts "\n Arduino force-install failed downloading #{ package_url } : #{ e } "
127
131
end
128
132
129
133
# Extract the package_file to extracted_file
@@ -133,7 +137,7 @@ def extract
133
137
batch_size = [ 1 , ( zip . size / 100 ) . to_i ] . max
134
138
dots = 0
135
139
zip . each do |file |
136
- print "." if ( dots % batch_size ) . zero?
140
+ @output . print "." if ( dots % batch_size ) . zero?
137
141
file . restore_permissions = true
138
142
file . extract { accept_all }
139
143
dots += 1
@@ -153,40 +157,40 @@ def install
153
157
def execute
154
158
error_preparing = prepare
155
159
unless error_preparing . nil?
156
- puts "Arduino force-install failed preparation: #{ error_preparing } "
160
+ @output . puts "Arduino force-install failed preparation: #{ error_preparing } "
157
161
return false
158
162
end
159
163
160
164
attempts = 0
161
165
162
166
loop do
163
167
if File . exist? package_file
164
- puts "Arduino package seems to have been downloaded already" if attempts . zero?
168
+ @output . puts "Arduino package seems to have been downloaded already" if attempts . zero?
165
169
break
166
170
elsif attempts >= DOWNLOAD_ATTEMPTS
167
- break puts "After #{ DOWNLOAD_ATTEMPTS } attempts, failed to download #{ package_url } "
171
+ break @output . puts "After #{ DOWNLOAD_ATTEMPTS } attempts, failed to download #{ package_url } "
168
172
else
169
- print "Attempting to download Arduino package with #{ downloader } "
173
+ @output . print "Attempting to download Arduino package with #{ downloader } "
170
174
download
171
- puts
175
+ @output . puts
172
176
end
173
177
attempts += 1
174
178
end
175
179
176
180
if File . exist? extracted_file
177
- puts "Arduino package seems to have been extracted already"
181
+ @output . puts "Arduino package seems to have been extracted already"
178
182
elsif File . exist? package_file
179
- print "Extracting archive with #{ extracter } "
183
+ @output . print "Extracting archive with #{ extracter } "
180
184
extract
181
- puts
185
+ @output . puts
182
186
end
183
187
184
188
if File . exist? self . class . force_install_location
185
- puts "Arduino package seems to have been installed already"
189
+ @output . puts "Arduino package seems to have been installed already"
186
190
elsif File . exist? extracted_file
187
191
install
188
192
else
189
- puts "Could not find extracted archive (tried #{ extracted_file } )"
193
+ @output . puts "Could not find extracted archive (tried #{ extracted_file } )"
190
194
end
191
195
192
196
File . exist? self . class . force_install_location
0 commit comments