slackbuilder/lib/download.rb

128 lines
3.4 KiB
Ruby

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# frozen_string_literal: true
require_relative '../config/config'
require_relative 'package'
require 'net/http'
require 'pathname'
require 'progressbar'
require 'term/ansicolor'
module SlackBuilder
extend Rake::FileUtilsExt
def self.clone(repo, tarball, tag_prefix = 'v')
name_version = File.basename tarball, '.tar.xz'
remote_path = tarball[tarball.index('/')..]
if remote_file_exists?(remote_path)
uri = URI hosted_sources(remote_path)
return download(uri, "slackbuilds/#{tarball}").hexdigest
end
clone_and_archive repo, name_version, tarball, tag_prefix
sh(*upload_command(tarball, remote_path))
Digest::MD5.hexdigest File.read("slackbuilds/#{tarball}")
end
def self.download(uri, target)
print Term::ANSIColor.green "Downloading #{uri} "
checksum = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
checksum = start_download uri, target, http
end
puts
checksum
end
def self.hosted_sources(absolute_url)
CONFIG[:download_url] + absolute_url
end
def self.remote_file_exists?(url)
`./bin/slackbuilder exists #{url}`.strip == 'True'
end
def self.download_and_deploy(uri, tarball)
remote_path = tarball[tarball.index('/')..]
if remote_file_exists?(remote_path)
uri = URI hosted_sources(remote_path)
return download(uri, "slackbuilds/#{tarball}").hexdigest
end
checksum = download uri, "slackbuilds/#{tarball}"
sh(*upload_command(tarball, remote_path))
checksum.hexdigest
end
private_class_method def self.redirect_download(location, target)
puts 'redirecting...'
new_location = URI location
download new_location, target
end
private_class_method def self.write_chunk(response, checksum, progressbar, io)
response.read_body do |chunk|
progressbar.progress += chunk.length
io << chunk
checksum << chunk
end
end
private_class_method def self.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
progressbar.finish
checksum
end
private_class_method def self.start_download(uri, target, http)
request = Net::HTTP::Get.new uri
http.request request do |response|
case response
when Net::HTTPRedirection
return redirect_download response['location'], target
else
return write_download target, response
end
end
end
private_class_method def self.upload_command(local_path, remote_path)
['scp', "slackbuilds/#{local_path}", CONFIG[:remote_path] + remote_path]
end
private_class_method def self.clone_and_archive(repo, name_version, tarball, tag_prefix = 'v')
sh './bin/slackbuilder', 'archive', repo, name_version, tarball, tag_prefix
end
end
def write_info(package, downloads:)
File.write "slackbuilds/#{package.path}/#{package.name}.info",
info_template(package, downloads)
end
def update_slackbuild_version(package_path, version)
sh './bin/slackbuilder', 'slackbuild', package_path, version
end
def commit(package_path, version)
sh './bin/slackbuilder', 'commit', package_path, version
end