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) super() end # FIXME: def execute def run(context : Context, recipe : Recipe) : Instructions::Status 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 else return Instructions::Status::Failed end end @defaults_if_empty.each do |default| Dir.cd recipe.building_directory rvalue = default.callback.call context, 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 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 def to_a [configure, build, install] end end