slackbuilder/lib/download.rb

137 lines
3.7 KiB
Ruby
Raw Normal View History

2020-12-09 18:28:42 +01:00
# 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
2021-01-02 07:39:32 +01:00
require_relative '../config/config'
2020-12-09 18:28:42 +01:00
require_relative 'package'
require 'net/http'
2022-06-17 22:49:30 +02:00
require 'pathname'
2022-12-04 15:03:14 +01:00
require 'progressbar'
require 'term/ansicolor'
2022-06-17 22:49:30 +02:00
module SlackBuilder
extend Rake::FileUtilsExt
2023-03-18 13:15:12 +01:00
def self.clone(repo, tarball, tag_prefix = 'v')
name_version = File.basename tarball, '.tar.xz'
remote_path = tarball[tarball.index('/')..]
2022-06-17 22:49:30 +02:00
2023-03-18 13:15:12 +01:00
if remote_file_exists?(remote_path)
uri = URI hosted_sources(remote_path)
return download(uri, "slackbuilds/#{tarball}").hexdigest
2022-06-17 22:49:30 +02:00
end
2023-03-18 13:15:12 +01:00
clone_and_archive repo, name_version, tarball, tag_prefix
sh(*upload_command(tarball, remote_path))
2022-06-17 22:49:30 +02:00
2023-03-18 13:15:12 +01:00
Digest::MD5.hexdigest File.read("slackbuilds/#{tarball}")
2022-06-17 22:49:30 +02:00
end
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
def self.download(uri, target)
print Term::ANSIColor.green "Downloading #{uri} "
checksum = nil
2022-12-04 15:03:14 +01:00
2023-01-04 10:51:08 +01:00
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
checksum = start_download uri, target, http
end
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
puts
checksum
2020-12-09 18:28:42 +01:00
end
def self.hosted_sources(absolute_url)
2023-03-18 13:15:12 +01:00
CONFIG[:download_url] + absolute_url
end
def self.remote_file_exists?(url)
`./bin/slackbuilder exists #{url}`.strip == 'True'
2023-03-18 13:15:12 +01:00
end
def self.download_and_deploy(uri, tarball)
remote_path = tarball[tarball.index('/')..]
if remote_file_exists?(remote_path)
2023-03-18 13:15:12 +01:00
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
2023-01-04 10:51:08 +01:00
private_class_method def self.redirect_download(location, target)
puts 'redirecting...'
new_location = URI location
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
download new_location, target
end
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
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
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
private_class_method def self.write_download(target, response)
checksum = Digest::MD5.new
progressbar = ProgressBar.create title: target, total: response.header.content_length
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
File.open target, 'w' do |io|
write_chunk response, checksum, progressbar, io
2020-12-09 18:28:42 +01:00
end
2023-01-04 10:51:08 +01:00
progressbar.finish
checksum
2020-12-09 18:28:42 +01:00
end
2023-01-04 10:51:08 +01:00
private_class_method def self.start_download(uri, target, http)
request = Net::HTTP::Get.new uri
2020-12-09 18:28:42 +01:00
2023-01-04 10:51:08 +01:00
http.request request do |response|
case response
when Net::HTTPRedirection
return redirect_download response['location'], target
else
return write_download target, response
end
end
2020-12-09 18:28:42 +01:00
end
2023-03-18 13:15:12 +01:00
private_class_method def self.upload_command(local_path, remote_path)
['scp', "slackbuilds/#{local_path}", CONFIG[:remote_path] + remote_path]
2020-12-09 18:28:42 +01:00
end
2023-03-18 13:15:12 +01:00
private_class_method def self.clone_and_archive(repo, name_version, tarball, tag_prefix = 'v')
_, _, version = name_version.rpartition '-'
2020-12-09 18:28:42 +01:00
2023-03-18 13:15:12 +01:00
rm_rf name_version
2020-12-09 18:28:42 +01:00
2023-03-18 13:15:12 +01:00
sh 'git', 'clone', repo, name_version
sh 'git', '-C', name_version, 'checkout', "#{tag_prefix}#{version}"
sh 'git', '-C', name_version, 'submodule', 'update', '--init', '--recursive'
2020-12-09 18:28:42 +01:00
2023-03-18 13:15:12 +01:00
sh 'tar', 'Jcvf', "slackbuilds/#{tarball}", name_version
rm_rf name_version
2020-12-09 18:28:42 +01:00
end
end
2021-05-04 07:47:24 +02:00
def write_info(package, downloads:)
2022-06-14 06:52:24 +02:00
File.write "slackbuilds/#{package.path}/#{package.name}.info",
info_template(package, downloads)
2020-12-09 18:28:42 +01:00
end
def update_slackbuild_version(package_path, version)
2023-08-15 10:33:19 +02:00
sh './bin/slackbuilder', 'slackbuild', package_path, version
2020-12-09 18:28:42 +01:00
end
def commit(package_path, version)
2023-08-15 10:33:19 +02:00
sh './bin/slackbuilder', 'commit', package_path, version
2020-12-09 18:28:42 +01:00
end