Archived
3
0

PoC automatic building.

This commit is contained in:
Luka Vandervelden 2019-07-02 08:47:11 +02:00
parent 928559f71f
commit 0a565ebb26
3 changed files with 104 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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