From e6645e4f3def4beb15fcfa3547746ff3b663e08c Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 3 Jul 2019 03:48:31 +0200 Subject: [PATCH] Grooming. --- src/context.cr | 35 ++++++++++++++++- src/instructions.cr | 95 +++++++++++++++------------------------------ src/recipe.cr | 2 +- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/context.cr b/src/context.cr index 792aa78..314cf9f 100644 --- a/src/context.cr +++ b/src/context.cr @@ -16,7 +16,8 @@ class Package::Context property sources_directory = Dir.current property packages_directory = Dir.current - @packaging_backends = [] of Backend::Packaging + getter packaging_backends = [] of Backend::Packaging + getter building_backends = [] of Backend::Building def initialize @packaging_backends << Backend::Packaging.new "pkgutils" do |package| @@ -27,6 +28,38 @@ class Package::Context end @selected_packaging_backend = @packaging_backends[0] + + @building_backends << Backend::Building.new "configure", "autotools" do |context, recipe| + Dir.cd recipe.dirname + + unless File.exists? "configure" + next BuildStatus::Pass + end + + child = context.run "./configure", ["--prefix=/package"] + + if child.exit_status == 0 + BuildStatus::Success + else + BuildStatus::Failed + end + end + + @building_backends << Backend::Building.new "build", "make" do |context, recipe| + Dir.cd recipe.dirname + + unless File.exists? "Makefile" + next BuildStatus::Pass + end + + child = context.run "make" + + if child.exit_status == 0 + BuildStatus::Success + else + BuildStatus::Failed + end + end end def packaging_backend=(name : String) diff --git a/src/instructions.cr b/src/instructions.cr index 524701b..ddb01ce 100644 --- a/src/instructions.cr +++ b/src/instructions.cr @@ -1,98 +1,65 @@ +enum Package::BuildStatus + Success + Failed + Pass +end + +class Package::Backend::Building + getter phase : String + getter name : String + getter callback : Proc(Context, Recipe, BuildStatus) + def initialize(@phase, @name, &block : Proc(Context, Recipe, BuildStatus)) + @callback = block + end + def build(context : Context, recipe : Recipe) + @callback.call context, recipe + end +end + class Package::Instructions - enum Status - Success - Failed - Pass - end - - class BuildDefault - getter name : String - getter callback : Proc(Context, Recipe, Status) - def initialize(@name, &block : Proc(Context, Recipe, Status)) - @callback = block - end - end - class Set < Array(String) - def initialize(@defaults_if_empty = Array(BuildDefault).new) + def initialize(@phase : String) super() end # FIXME: def execute - def run(context : Context, recipe : Recipe) : Instructions::Status + def run(context : Context, recipe : Recipe) : BuildStatus if size > 0 # FIXME: Maybe do that for [1] and the others, no? child = context.run recipe.building_directory, "sh", ["-c", self[0]] if child.exit_status == 0 - return Instructions::Status::Success + return BuildStatus::Success else - return Instructions::Status::Failed + return BuildStatus::Failed end end - @defaults_if_empty.each do |default| + context.building_backends.select(&.phase.==(@phase)).each do |backend| Dir.cd recipe.building_directory - rvalue = default.callback.call context, recipe + rvalue = backend.build context, recipe - if rvalue == Status::Pass + if rvalue == BuildStatus::Pass next end return rvalue end - Status::Pass + BuildStatus::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 + BuildStatus::Failed end end - getter configure = Set.new - getter build = Set.new - getter install = Set.new - - def initialize - @build << BuildDefault.new "autotools" do |context, recipe| - Dir.cd recipe.dirname - - unless File.exists? "configure" - next Instructions::Status::Pass - end - - child = context.run "./configure", ["--prefix=/package"] - - if child.exit_status == 0 - Instructions::Status::Success - else - Instructions::Status::Failed - end - end - - @build << BuildDefault.new "make" do |context, recipe| - Dir.cd recipe.dirname - - unless File.exists? "Makefile" - next Instructions::Status::Pass - end - - child = context.run "make" - - if child.exit_status == 0 - Instructions::Status::Success - else - Instructions::Status::Failed - end - end - end + # FIXME: Switch to an enum at some point? + getter configure = Set.new "configure" + getter build = Set.new "build" + getter install = Set.new "install" def to_a [configure, build, install] diff --git a/src/recipe.cr b/src/recipe.cr index e8d69f6..794f860 100644 --- a/src/recipe.cr +++ b/src/recipe.cr @@ -94,7 +94,7 @@ class Package::Recipe instructions.to_a.each do |instruction| if instruction.run(@context, self).failed? - break Instructions::Status::Failed + break BuildStatus::Failed end end