67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/erhu/app.rb', line 67
def package(package_url, name: nil, extension: nil, &block)
raise "package url is required" if package_url.blank?
raise "name: 'package name' is required" if name.blank?
_extension = (package_url)
if _extension == ".zip" or _extension == ".tar.gz"
extension ||= _extension
else
extension ||= ".zip"
end
package_name = name
package_hex = name.unpack('H*').first
package_file_path = "#{@erhu_path}/#{package_name}-#{package_hex}#{extension}"
if File.exist?(package_file_path)
puts "ignored #{package_url}"
return
end
bar = TTY::ProgressBar.new("downloading #{package_name} :percent [:bar]", total: 50)
total_size = 0
http.download package_url, destination: package_file_path,
content_length_proc: -> (content_length) {
total_size = content_length
},
progress_proc: -> (progress) {
if total_size > 0
bar.ratio = progress / total_size.to_f
end
}
bar.finish
unless block.blank?
block.call(package_file_path, self)
return
end
self.zip(package_file_path, package_name) if extension == ".zip"
ungzip(package_file_path, File.join(@target, package_name)) if extension == ".tar.gz"
end
|