This repository has been archived on 2022-01-17. You can view files and clone it, but cannot push or open issues/pull-requests.
packaging/src/context.cr

149 lines
4.2 KiB
Crystal
Raw Normal View History

require "colorize"
require "specparser"
2019-08-29 00:20:15 +02:00
2019-08-16 14:55:25 +02:00
require "./exception.cr"
2019-08-03 13:12:23 +02:00
require "./config.cr"
2019-09-06 00:39:11 +02:00
require "./backends.cr"
2019-07-02 03:50:50 +02:00
class Package::Context
2021-02-22 00:31:51 +01:00
property working_directory = "/tmp/packaging"
2019-07-02 03:50:50 +02:00
property sources_directory = Dir.current
property packages_directory = Dir.current
2019-07-03 03:48:31 +02:00
getter packaging_backends = [] of Backend::Packaging
getter building_backends = [] of Backend::Building
2019-07-04 23:18:54 +02:00
getter splitter_backends = [] of Backend::Splitter
2019-07-03 03:35:35 +02:00
2019-08-03 12:55:47 +02:00
# Directories where ports can be found.
getter repositories = [] of String
2019-07-03 05:23:48 +02:00
# Well, this will need configuration, auto-detection and conversion,
# but itll be kind of enough for now.
property architecture = "x86_64"
# prefixes for `packaging` running environment and child processes
# = where to search for binaries and libraries for the build
property prefixes = ["/usr/baguette", "/usr", "/"]
2021-02-22 17:14:00 +01:00
# By default, building a package only uses one core
property build_cores = 1
# list of environment variables we want to have when building
#property environment = {} of String => String
2021-02-22 17:14:00 +01:00
property recipe : Recipe? = nil
2019-07-02 03:50:50 +02:00
def initialize
# Add package backends: baguette (package-tools) and apk.
# They implement the Backend::Packaging abstract class:
# init (@name) + package (context, package) method
@packaging_backends << Backend::Packaging::Baguette.new
@packaging_backends << Backend::Packaging::APK.new
@packaging_backends << Backend::Packaging::Pkgutils.new
2019-07-03 05:23:48 +02:00
2019-07-03 03:35:35 +02:00
@selected_packaging_backend = @packaging_backends[0]
2019-07-03 03:48:31 +02:00
# Add building backends: configuration, build and install.
@building_backends << Backend::Configure.autotools
@building_backends << Backend::Configure.cmake
@building_backends << Backend::Build.make
@building_backends << Backend::Install.make
# Split = new package from a recipe, each one with different files being included.
@splitter_backends << Backend::Splitter.man @prefixes
@splitter_backends << Backend::Splitter.dev @prefixes
@splitter_backends << Backend::Splitter.src @prefixes
2019-07-03 03:35:35 +02:00
end
def packaging_backend=(name : String)
@selected_packaging_backend = @packaging_backends.find(&.name.==(name)).not_nil!
end
def packaging_backend=(backend : Backend::Packaging)
@selected_packaging_backend = backend
2019-07-02 03:50:50 +02:00
end
2019-07-02 19:45:33 +02:00
def package(package : Package) : Bool
@selected_packaging_backend.package @packages_directory, @architecture, package
end
2019-08-02 17:43:09 +02:00
def read_recipe(filename : String)
2021-02-22 17:14:00 +01:00
@recipe = Recipe.new self, filename
@recipe.not_nil!
2019-08-02 17:43:09 +02:00
end
2019-08-03 12:55:47 +02:00
def find_recipe(name : String) : Recipe?
recipe_file_name = ""
repo = @repositories.find do |repo|
repo_dir_name = "#{repo}/#{name}"
recipe_file_name = "#{repo_dir_name}/recipe.spec"
if Dir.exists?(repo_dir_name) && File.exists?(recipe_file_name)
next true
end
end
read_recipe recipe_file_name if repo
end
2019-08-29 00:20:15 +02:00
def read_configuration(filename : String)
2019-11-05 13:52:37 +01:00
specs = SpecParser.parse(File.read(filename))
2019-08-29 00:20:15 +02:00
specs.assignments.each do |key, value|
case key
when "packages-directory"
@packages_directory = value.as_s
when "sources-directory"
@sources_directory = value.as_s
2021-02-22 00:31:51 +01:00
when "working-directory"
@working_directory = value.as_s
2019-08-29 00:20:15 +02:00
when "prefixes"
# Prefixes during the build process.
2019-08-29 00:20:15 +02:00
@prefixes = value.as_a_or_s
2021-02-22 17:14:00 +01:00
when "build-cores"
# NB of cores used to compile applications.
@build_cores = value.as_s.to_i
when "environment"
# Environment variables during the build process.
value.as_a_or_s.each do |entry|
match = entry.split(':').map(
&.gsub(/^[ \t]*/, "").gsub(/[ \t]*$/, ""))
if match.size != 2
STDERR.puts "WARNING: misformed environment definition: #{entry}"
next
end
key, value = match
Do.environment[key] = value
if Baguette::Context.verbosity > 2
2021-02-22 17:14:00 +01:00
STDOUT.puts "environment: #{key} => #{value}"
end
end
2021-02-22 17:14:00 +01:00
2019-09-06 00:39:11 +02:00
when "package-manager"
# Targeted package manager (default: package, for BaguetteOS).
begin
self.packaging_backend = value.as_s
rescue e
STDERR.puts "Error during selecting packaging backend: #{e}"
STDERR.puts "#{value.as_s} seems not to be a valid backend"
STDERR.puts "valid backends:"
@packaging_backends.each do |backend|
STDERR.puts "- #{backend.name}"
end
exit 1
end
2019-08-29 00:20:15 +02:00
end
end
end
2021-02-28 20:56:51 +01:00
def install(packages : Array(String))
@selected_packaging_backend.install packages
end
2019-07-02 03:50:50 +02:00
end