This commit is contained in:
Artur Gurgul1 2025-08-06 10:17:48 +02:00
parent 045864495f
commit 757921be5b
4 changed files with 94 additions and 29 deletions

View file

@ -12,6 +12,7 @@ options = OpenStruct.new
# take the first so OptionParser will not see it # take the first so OptionParser will not see it
subcommand = ARGV.shift&.to_sym subcommand = ARGV.shift&.to_sym
options.name = ARGV[0] && ARGV[0] !~ /^-/ ? ARGV.shift : nil
OptionParser.new do |opt| OptionParser.new do |opt|
opt.on('-i', '--install', 'Install dat for the user') { |o| options.type = :install } opt.on('-i', '--install', 'Install dat for the user') { |o| options.type = :install }

View file

@ -13,7 +13,17 @@ module Archive
"\xFD\x37\x7A\x58" => 'xz' "\xFD\x37\x7A\x58" => 'xz'
} }
def self.fetch_and_extract(url, cache_path, destination_dir) def self.extract(archive_path, destination_dir)
format = detect_format_from_url(archive_path)
unless format
format = detect_format_from_magic(archive_path)
end
extract_archive(archive_path, destination_dir, format)
end
def self.fetch_and_extract(url, cache_path, destination_dir)
uri = URI.parse(url) uri = URI.parse(url)
file_name = File.basename(uri.path) file_name = File.basename(uri.path)
archive_path = File.join(destination_dir, file_name) archive_path = File.join(destination_dir, file_name)
@ -22,6 +32,7 @@ module Archive
puts "format #{format}" puts "format #{format}"
download_file(uri, archive_path) download_file(uri, archive_path)
# Probably do not work
unless format unless format
puts "case 1" puts "case 1"
format = detect_format_from_headers(uri) format = detect_format_from_headers(uri)
@ -37,6 +48,20 @@ module Archive
extract_archive(archive_path, format) extract_archive(archive_path, format)
end end
def self.download_file(uri, output_path, forced=false)
return if File.exist?(output_path) && !forced
# Ensure the directory exists
dir = File.dirname(output_path)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
URI.open(uri) do |input|
File.open(output_path, 'wb') do |output|
IO.copy_stream(input, output)
end
end
end
private private
def self.detect_format_from_url(url) def self.detect_format_from_url(url)
@ -71,33 +96,60 @@ module Archive
nil nil
end end
def self.download_file(uri, output_path)
URI.open(uri) do |input| require 'shellwords'
File.open(output_path, 'wb') do |output|
IO.copy_stream(input, output) def self.extract_archive(file_path, destination_dir, format)
end # TODO: need to be checked
escaped_file = Shellwords.escape(file_path)
escaped_dir = Shellwords.escape(destination_dir)
system("mkdir -p #{escaped_dir}")
case format
when 'gzip'
system("tar -xzf #{escaped_file} -C #{escaped_dir}")
when 'zip'
system("unzip -d #{escaped_dir} #{escaped_file}")
when 'zstd'
if `file #{escaped_file}`.include?('tar archive')
system("unzstd -c #{escaped_file} | tar -xf - -C #{escaped_dir}")
else
system("unzstd -o \"#{escaped_dir}\" #{escaped_file}")
end end
when 'xz'
if `file #{escaped_file}`.include?('tar archive')
system("tar -xJf #{escaped_file} -C #{escaped_dir}")
else
system("xz -dk #{escaped_file}")
decompressed = file_path.sub(/\.xz$/, '')
system("mv #{Shellwords.escape(decompressed)} #{escaped_dir}/")
end
else
raise "Unsupported archive format: #{format}"
end
end end
def self.extract_archive(file_path, format)
case format # def self.extract_archive(file_path, destination_dir, format)
when 'gzip' # case format
system("tar -xzf #{Shellwords.escape(file_path)}") # when 'gzip'
when 'zip' # system("tar -xzf #{Shellwords.escape(file_path)}")
system("unzip #{Shellwords.escape(file_path)}") # when 'zip'
when 'zstd' # system("unzip #{Shellwords.escape(file_path)}")
system("unzstd #{Shellwords.escape(file_path)}") # when 'zstd'
when 'xz' # system("unzstd #{Shellwords.escape(file_path)}")
# if file_path.end_with?('.tar.xz') # when 'xz'
if `file #{Shellwords.escape(file_path)}`.include?('tar archive') # # if file_path.end_with?('.tar.xz')
system("tar -xJf #{Shellwords.escape(file_path)}") # if `file #{Shellwords.escape(file_path)}`.include?('tar archive')
else # system("tar -xJf #{Shellwords.escape(file_path)}")
system("xz -dk #{Shellwords.escape(file_path)}") # else
end # system("xz -dk #{Shellwords.escape(file_path)}")
else # end
raise "Unsupported archive format: #{format}" # else
end # raise "Unsupported archive format: #{format}"
end # end
# end
end end
# Example usage: # Example usage:
# fetch_and_extract("https://codeberg.org/forgejo/forgejo/releases/download/v12.0.1/forgejo-12.0.1-linux-amd64.xz") # fetch_and_extract("https://codeberg.org/forgejo/forgejo/releases/download/v12.0.1/forgejo-12.0.1-linux-amd64.xz")

View file

@ -123,6 +123,12 @@ module Make
"#{ENV["HOME"]}/.cache/dat/repo/#{@name}.git" "#{ENV["HOME"]}/.cache/dat/repo/#{@name}.git"
end end
def download_path
require 'addressable/uri'
uri = Addressable::URI.parse(archive.url)
File.join("#{ENV["HOME"]}/.cache/dat/downloads/", uri.domain, uri.path)
end
def to_s def to_s
vars = instance_variables.map do |var| vars = instance_variables.map do |var|
"#{var.to_s.delete('@')}: #{instance_variable_get(var).inspect}" "#{var.to_s.delete('@')}: #{instance_variable_get(var).inspect}"
@ -184,10 +190,16 @@ module Make
end end
def download_and_extract def download_and_extract
archive = @context.archive # gem install addressable
archive = @context.archive
puts @context.download_path
puts archive puts archive
exit -1
require 'archive'
Archive.download_file(@context.archive.url, @context.download_path)
Archive.extract(@context.download_path, @cwd)
end end
def checkout def checkout

View file

@ -3,4 +3,4 @@ archive:
url: https://codeberg.org/forgejo/forgejo/releases/download/v12.0.1/forgejo-12.0.1-linux-amd64.xz url: https://codeberg.org/forgejo/forgejo/releases/download/v12.0.1/forgejo-12.0.1-linux-amd64.xz
steps: steps:
- echo "ls" - mv forgejo-12.0.1-linux-amd64 ~/.local/bin/forgejo