diff --git a/src/instructions.cr b/src/instructions.cr index 93a7c6c..cf8b509 100644 --- a/src/instructions.cr +++ b/src/instructions.cr @@ -1,7 +1,57 @@ class Package::Instructions + enum Status + Success + Failed + Pass + end + + class BuildDefault + getter name : String + getter callback : Proc(Recipe, Status) + def initialize(@name, &block : Proc(Recipe, Status)) + @callback = block + end + end + class Set < Array(String) + def initialize(@defaults_if_empty = Array(BuildDefault).new) + super() + end # FIXME: def execute + + def run(recipe : Recipe) : Instructions::Status + if size > 0 + # FIXME: Maybe do that for [1] and the others, no? + child = Process.run "sh", ["-c", self[0]], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit + + if child.exit_status == 0 + return Instructions::Status::Success + else + return Instructions::Status::Failed + end + end + + @defaults_if_empty.each do |default| + rvalue = default.callback.call recipe + + if rvalue == Status::Pass + next + end + + return rvalue + end + + Status::Pass + rescue e + # Possible TODO: print the origin of the exception (backend, user-provided code, other/unknown). + STDERR << "Exception caught: " << e.message << "\n" + Status::Failed + end + + def <<(other : BuildDefault) + @defaults_if_empty << other + end end getter configure = Set.new @@ -9,10 +59,41 @@ class Package::Instructions getter install = Set.new def initialize + @build << BuildDefault.new "autotools" do |recipe| + Dir.cd recipe.dirname + + unless File.exists? "configure" + next Instructions::Status::Pass + end + + child = Process.run("./configure", ["--prefix=/package"], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit) + + if child.exit_status == 0 + Instructions::Status::Success + else + Instructions::Status::Failed + end + end + + @build << BuildDefault.new "make" do |recipe| + Dir.cd recipe.dirname + + unless File.exists? "Makefile" + next Instructions::Status::Pass + end + + child = Process.run("make", output: Process::Redirect::Inherit, error: Process::Redirect::Inherit) + + if child.exit_status == 0 + Instructions::Status::Success + else + Instructions::Status::Failed + end + end end - def map(&block : Proc(String, Nil)) - (configure + build + install).map &block + def to_a + [configure, build, install] end end diff --git a/src/main.cr b/src/main.cr index c0e7e27..2fc8b25 100644 --- a/src/main.cr +++ b/src/main.cr @@ -13,11 +13,11 @@ Context.new().tap do |context| recipe.download recipe.extract - recipe.instructions.configure << "cd hello-#{recipe.version} && ./configure" - recipe.instructions.build << "cd hello-#{recipe.version} && make" + #recipe.instructions.configure << "cd hello-#{recipe.version} && ./configure" + #recipe.instructions.build << "cd hello-#{recipe.version} && make" recipe.instructions.install << "cd hello-#{recipe.version} && make DESTDIR='${PKG}' install" - recipe.build + raise "oh no, build failed" unless recipe.build recipe.package recipe.clean diff --git a/src/recipe.cr b/src/recipe.cr index 40575b5..8ccf71c 100644 --- a/src/recipe.cr +++ b/src/recipe.cr @@ -20,6 +20,7 @@ class Package::Recipe getter name : String getter version : String + getter dirname : String? getter sources : Sources getter packages : Array(Package) @@ -47,6 +48,10 @@ class Package::Recipe "#{working_directory}/root" end + def dirname + @dirname || "#{name}-#{version}" + end + def download sources.map do |url| unless File.exists? url.basename @@ -73,15 +78,26 @@ class Package::Recipe # goes somehow wrong. # - Make things thread-safe. (those ENV[]= calls are definitely not) def build + success = true + 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 + old_dir = Dir.current + + instructions.to_a.each do |instruction| + Dir.cd building_directory + if instruction.run(self).failed? + break Instructions::Status::Failed + end end + Dir.cd old_dir + ENV["PKG"] = nil + + success end def package