require "uuid" require "uri" require "file_utils" require "./context.cr" require "./package.cr" require "./instructions.cr" require "./sources.cr" # 🤔 class URI def basename File.basename path end end class Package::Recipe @context : Context getter name : String getter version : String getter sources : Sources getter packages : Array(Package) getter instructions = Instructions.new def initialize(@context, @name, @version) @sources = Sources.new @packages = [ Package.new @name, @version ] @working_uuid = UUID.random end def working_directory @context.working_directory end def building_directory "#{working_directory}/build" end def fake_root_directory "#{working_directory}/root" end def download sources.map do |url| unless File.exists? url.basename Process.run "wget", [ url.to_s, "-O", @context.sources_directory + "/" + url.basename ], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit end end end def extract Dir.mkdir_p building_directory sources.map do |url| basename = url.basename Process.run "tar", [ "xvf", @context.sources_directory + "/" + url.basename ], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, chdir: building_directory end end # TODO: # - Export packaging directory in $PKG. # - Add (pre|post)-(configure|build|install) instructions. # - Have some instructions be non-critical, like the (pre|post] ones. # - Be careful about return values, flee from everything if something # goes somehow wrong. # - Make things thread-safe. (those ENV[]= calls are definitely not) def build Dir.mkdir_p fake_root_directory ENV["PKG"] = fake_root_directory instructions.map do |script| pp! Process.run "sh", ["-c", script], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, chdir: building_directory end ENV["PKG"] = nil end def package puts "#{fake_root_directory} -> #{@context.packages_directory}/#{name}##{version}.pkg.tar.xz" pp! Process.run "tar", ["cJf", "#{@context.packages_directory}/#{name}##{version}.pkg.tar.xz", "."], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, chdir: fake_root_directory end def clean FileUtils.rm_rf building_directory FileUtils.rm_rf fake_root_directory end def to_s "#{name}-#{version}" end end