Implement downloading progress
This commit is contained in:
parent
1dd5639bcb
commit
3a5a07afb4
3
Gemfile
3
Gemfile
@ -10,3 +10,6 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
|||||||
|
|
||||||
gem 'rake', '~> 13.0'
|
gem 'rake', '~> 13.0'
|
||||||
gem 'rubocop', '~> 1.7', require: false
|
gem 'rubocop', '~> 1.7', require: false
|
||||||
|
|
||||||
|
gem 'progressbar', '~> 1.11'
|
||||||
|
gem 'term-ansicolor', '~> 1.7'
|
||||||
|
@ -6,6 +6,7 @@ GEM
|
|||||||
parallel (1.22.1)
|
parallel (1.22.1)
|
||||||
parser (3.1.2.1)
|
parser (3.1.2.1)
|
||||||
ast (~> 2.4.1)
|
ast (~> 2.4.1)
|
||||||
|
progressbar (1.11.0)
|
||||||
rainbow (3.1.1)
|
rainbow (3.1.1)
|
||||||
rake (13.0.6)
|
rake (13.0.6)
|
||||||
regexp_parser (2.6.0)
|
regexp_parser (2.6.0)
|
||||||
@ -23,14 +24,21 @@ GEM
|
|||||||
rubocop-ast (1.23.0)
|
rubocop-ast (1.23.0)
|
||||||
parser (>= 3.1.1.0)
|
parser (>= 3.1.1.0)
|
||||||
ruby-progressbar (1.11.0)
|
ruby-progressbar (1.11.0)
|
||||||
|
sync (0.5.0)
|
||||||
|
term-ansicolor (1.7.1)
|
||||||
|
tins (~> 1.0)
|
||||||
|
tins (1.32.1)
|
||||||
|
sync
|
||||||
unicode-display_width (2.3.0)
|
unicode-display_width (2.3.0)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
progressbar (~> 1.11)
|
||||||
rake (~> 13.0)
|
rake (~> 13.0)
|
||||||
rubocop (~> 1.7)
|
rubocop (~> 1.7)
|
||||||
|
term-ansicolor (~> 1.7)
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
2.2.33
|
2.2.33
|
||||||
|
@ -8,6 +8,8 @@ require_relative '../config/config'
|
|||||||
require_relative 'package'
|
require_relative 'package'
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
require 'progressbar'
|
||||||
|
require 'term/ansicolor'
|
||||||
|
|
||||||
module SlackBuilder
|
module SlackBuilder
|
||||||
extend Rake::FileUtilsExt
|
extend Rake::FileUtilsExt
|
||||||
@ -28,16 +30,22 @@ module SlackBuilder
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_download(target, response)
|
def write_chunk(response, checksum, progressbar, io)
|
||||||
checksum = Digest::MD5.new
|
|
||||||
|
|
||||||
File.open target, 'w' do |io|
|
|
||||||
response.read_body do |chunk|
|
response.read_body do |chunk|
|
||||||
print '.'
|
progressbar.progress += chunk.length
|
||||||
io << chunk
|
io << chunk
|
||||||
checksum << chunk
|
checksum << chunk
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_download(target, response)
|
||||||
|
checksum = Digest::MD5.new
|
||||||
|
progressbar = ProgressBar.create title: target, total: response.header.content_length
|
||||||
|
|
||||||
|
File.open target, 'w' do |io|
|
||||||
|
write_chunk response, checksum, progressbar, io
|
||||||
end
|
end
|
||||||
|
progressbar.finish
|
||||||
|
|
||||||
checksum
|
checksum
|
||||||
end
|
end
|
||||||
@ -63,7 +71,7 @@ def start_download(uri, target, http)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def download(uri, target)
|
def download(uri, target)
|
||||||
print "Downloading #{uri} "
|
print Term::ANSIColor.green "Downloading #{uri} "
|
||||||
checksum = nil
|
checksum = nil
|
||||||
|
|
||||||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
||||||
|
Loading…
Reference in New Issue
Block a user