From 20376a36ed5042b4826715bb03bde0e745ee6a6a Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 28 Feb 2021 20:56:51 +0100 Subject: [PATCH] `backend`#install packages --- src/backends.cr | 6 ++++ src/backends/apk.cr | 78 +++++++++++++++++++++++----------------- src/backends/baguette.cr | 11 ++++-- src/context.cr | 9 +++++ src/main.cr | 14 ++------ src/package.cr | 8 ++--- 6 files changed, 76 insertions(+), 50 deletions(-) diff --git a/src/backends.cr b/src/backends.cr index 3e9911d..010f439 100644 --- a/src/backends.cr +++ b/src/backends.cr @@ -6,6 +6,12 @@ abstract class Package::Backend::Packaging end abstract def package(context : Context, package : Package) : Bool + def self.install(packages : Array(String)) + raise "'install' unimplemented for this backend, yet" + end + def install(packages : Array(String)) + self.install packages + end end class Package::Backend::Splitter diff --git a/src/backends/apk.cr b/src/backends/apk.cr index 1392be4..d2472c5 100644 --- a/src/backends/apk.cr +++ b/src/backends/apk.cr @@ -1,36 +1,5 @@ require "../backends.cr" -# FIXME: Where should this go? We can’t just leave it here. :( -def pkginfo(package) - du = `du -sk #{package.fake_root_directory}` - size = du.sub(/[ \t].*/, "").to_i * 1024 - - lines = [] of String - - lines << "# Generated by `package`." - lines << "pkgname = #{package.name}" - lines << "pkgver = #{package.version}-r#{package.release}" - lines << "url = #{package.url || ""} " - lines << "size = #{size}" - lines << "origin = #{package.recipe.name}" - lines << "buildtype = host" # This’ll need to be imported from Context. - lines << "builddate = #{Time.utc.to_unix}" - - package.dependencies.each do |atom| - lines << "depend = #{atom.to_s}" - end - - package.provides.each do |atom| - lines << "provides = #{atom.to_s}" - end - - package.conflicts.each do |atom| - lines << "conflicts = #{atom.to_s}" - end - - lines.join("\n") + "\n" -end - class ApkBackend < Package::Backend::Packaging def initialize @name = "apk" @@ -41,7 +10,7 @@ class ApkBackend < Package::Backend::Packaging # to work properly. old_cwd = Dir.current - File.write "#{package.fake_root_directory}/.PKGINFO", pkginfo package + File.write "#{package.fake_root_directory}/.PKGINFO", ApkBackend.pkginfo package # Create data.tar.gz here. package_target = "#{context.packages_directory}/#{context.architecture}/#{package.name}-#{package.version}-r#{package.release}.apk" @@ -54,5 +23,50 @@ class ApkBackend < Package::Backend::Packaging r.exit_status == 0 end + + # This generated file content is specific to the Apk package manager. + def self.pkginfo(package) + du = `du -sk #{package.fake_root_directory}` + size = du.sub(/[ \t].*/, "").to_i * 1024 + + lines = [] of String + + lines << "# Generated by `packaging`." + lines << "pkgname = #{package.name}" + lines << "pkgver = #{package.version}-r#{package.release}" + lines << "url = #{package.url || ""} " + lines << "size = #{size}" + lines << "origin = #{package.recipe.name}" + lines << "buildtype = host" # This’ll need to be imported from Context. + lines << "builddate = #{Time.utc.to_unix}" + + package.dependencies.each do |atom| + lines << "depend = #{atom.to_s}" + end + + package.provides.each do |atom| + lines << "provides = #{atom.to_s}" + end + + package.conflicts.each do |atom| + lines << "conflicts = #{atom.to_s}" + end + + lines.join("\n") + "\n" + end + + # Install programs. + def self.install(packages : Array(String)) + opts = ["add"] + (ENV["APK_FLAGS"]? || "-i").split(/[ \t]+/) + packages + puts "+ apk #{opts.join " "}" + r = Process.run "apk", opts, + output: Process::Redirect::Inherit, + input: Process::Redirect::Inherit, + error: Process::Redirect::Inherit + if r.exit_status != 0 + STDERR.puts "!! Running apk failed!" + exit 8 + end + end end diff --git a/src/backends/baguette.cr b/src/backends/baguette.cr index 26c372d..d862c33 100644 --- a/src/backends/baguette.cr +++ b/src/backends/baguette.cr @@ -18,7 +18,7 @@ class BaguetteBackend < Package::Backend::Packaging control_spec_file_path = "#{tmpdir}/control.spec" manifest_file_path = "#{tmpdir}/manifest" - package_filename = "#{context.packages_directory}/#{package.name}-#{package.version}-#{package.release}.baguette" + package_target = "#{context.packages_directory}/#{package.name}-#{package.version}-#{package.release}.baguette" context.detail "Archiving package content" context.run fake_root, "tar", ["cvf", data_archive_path, "."] @@ -33,9 +33,9 @@ class BaguetteBackend < Package::Backend::Packaging context.detail "Generating manifest" generate_manifest context, package, manifest_file_path - context.detail "Assembling '#{package_filename}'" + context.detail "Assembling '#{package_target}'" r = context.run tmpdir, "tar", [ - "cf", package_filename, + "cf", package_target, # WARNING: relative paths are necessary. "control.spec", "manifest", "data.tar.zst" ] @@ -93,5 +93,10 @@ class BaguetteBackend < Package::Backend::Packaging ensure Dir.cd old_pwd.not_nil! end + + def self.install(packages : Array(String)) + # TODO: install packages through package-install + raise "call to package-install not implemented, yet" + end end diff --git a/src/context.cr b/src/context.cr index 5b58685..e6bb44c 100644 --- a/src/context.cr +++ b/src/context.cr @@ -153,6 +153,11 @@ class Package::Context end end + # Splitters = creating new packages: + # keep prefixes + # split names (man, src, ...) + # split files + @splitter_backends << Backend::Splitter.new do |recipe| Package.new(recipe, true).tap do |split| prefixes = (@prefixes + [recipe.prefix]).uniq @@ -331,5 +336,9 @@ class Package::Context end end end + + def install(packages : Array(String)) + @selected_packaging_backend.install packages + end end diff --git a/src/main.cr b/src/main.cr index 0a3a85a..441bc5f 100644 --- a/src/main.cr +++ b/src/main.cr @@ -158,17 +158,9 @@ begin end if dependencies.size > 0 && ! skip_build_dependencies && !download_only - # FIXME: We’ll probably want to have backends other than apk at some point. - opts = ["add"] + (ENV["APK_FLAGS"]? || "-i").split(/[ \t]+/) + dependencies - puts "+ apk #{opts.join " "}" - r = Process.run "apk", opts, - output: Process::Redirect::Inherit, - input: Process::Redirect::Inherit, - error: Process::Redirect::Inherit - if r.exit_status != 0 - STDERR.puts "!! Running apk failed!" - exit 8 - end + # TODO: change this. We want other backends. + # TODO: write something in Context + context.install dependencies end recipes.each do |recipe| diff --git a/src/package.cr b/src/package.cr index 7a8e4b4..0d5c243 100644 --- a/src/package.cr +++ b/src/package.cr @@ -23,8 +23,8 @@ class Package::Package when "description" @description = value.as_s_or_ls when "dependencies" - # Remember, no build-dep versus run-dep for - # packages! Build-deps are recipe-only! + # Remember, no build-dep versus run-dep for packages! + # Build-deps are recipe-only! @dependencies = value.as_a_or_s when "conflicts" @conflicts = value.as_a_or_s @@ -77,8 +77,8 @@ class Package::Package @fake_root_directory || "#{recipe.working_directory}/root-#{name}" end - def to_s - "" + def to_s(io : IO) + io << "" end end