slackbuilder/lib/package.rb

103 lines
2.5 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
class Package
attr_reader :path, :version, :homepage, :requires
2020-12-09 18:28:42 +01:00
def initialize(path, version:, homepage:, requires: [])
@path = path
@version = version
@homepage = homepage
@requires = requires
end
def name
File.basename @path
end
2022-06-17 22:49:30 +02:00
def name_version
"#{name}-#{@version}"
end
2023-05-23 20:36:19 +02:00
def self.parse(path, info_contents)
current_line = String.new ''
variables = {}
info_contents.each_line(chomp: true) do |file_line|
current_line << file_line.delete_suffix('\\')
next if file_line.end_with? '\\'
variables.store(*parse_pair(current_line))
current_line.clear
end
from_hash path, variables
end
private_class_method def self.parse_pair(current_line)
variable_name, variable_value = current_line.split '='
[variable_name, variable_value[1...-1].split]
end
private_class_method def self.from_hash(path, variables)
Package.new path,
version: variables['VERSION'].join,
homepage: variables['HOMEPAGE'].join,
requires: variables['REQUIRES']
end
2020-12-09 18:28:42 +01:00
end
2021-05-04 07:47:24 +02:00
class Download
attr_reader :download, :md5sum
def initialize(download, md5sum, is64: false)
@download = download
@md5sum = md5sum
@is64 = is64
end
def is64?
@is64
end
end
def info_template(package, downloads)
downloads64, downloads32 = downloads.partition(&:is64?)
download32, md5sum32, download64, md5sum64 = download_entries downloads64, downloads32
2020-12-09 18:28:42 +01:00
<<~INFO_FILE
PRGNAM="#{package.name}"
VERSION="#{package.version}"
HOMEPAGE="#{package.homepage}"
2021-05-04 07:47:24 +02:00
DOWNLOAD="#{download32}"
MD5SUM="#{md5sum32}"
DOWNLOAD_x86_64="#{download64}"
MD5SUM_x86_64="#{md5sum64}"
REQUIRES="#{requires_entry package.requires}"
2020-12-09 18:28:42 +01:00
MAINTAINER="Eugene Wissner"
EMAIL="belka@caraus.de"
INFO_FILE
end
2021-05-04 07:47:24 +02:00
private
def requires_entry(requires)
2021-10-12 07:42:35 +02:00
requires * ' '
2021-05-04 07:47:24 +02:00
end
def download_entries(downloads64, downloads32)
download32 =
if downloads32.empty? && !downloads64.empty?
'UNSUPPORTED'
else
downloads32.map(&:download) * " \\\n "
end
md5sum32 = downloads32.map(&:md5sum) * " \\\n "
download64 = downloads64.map(&:download) * " \\\n "
md5sum64 = downloads64.map(&:md5sum) * " \\\n "
[download32, md5sum32, download64, md5sum64]
end