Archived
3
0

Improved error reporting a bit.

This commit is contained in:
Luka Vandervelden 2019-08-16 14:55:25 +02:00
parent ef774194dc
commit 6fb2715267
5 changed files with 94 additions and 47 deletions

View File

@ -1,3 +1,4 @@
require "./exception.cr"
require "./config.cr"

32
src/exception.cr Normal file
View File

@ -0,0 +1,32 @@
class Package::Exception < Exception
getter recipe : Recipe
def initialize(@recipe, message : String)
super message
end
end
class Package::DownloadError < Package::Exception
getter url : URI
def initialize(recipe : Recipe, @url)
super recipe, "Downloading one of the sources failed."
end
end
class Package::ExtractionError < Package::Exception
getter url : URI
def initialize(recipe : Recipe, @url)
super recipe, "Extracting one of the sources failed."
end
end
class Package::BuildError < Package::Exception
end
class Package::PackagingError < Package::Exception
getter package : Package
def initialize(recipe : Recipe, @package)
super recipe, "Assembling a package failed."
end
end

View File

@ -19,7 +19,8 @@ end
class Package::Instructions
class Set < Array(String)
def initialize(@phase : String)
getter phase : String
def initialize(@phase)
super()
end
# FIXME: def execute

View File

@ -74,20 +74,31 @@ end
# FIXME: Now we need to build their respective deptrees and to deduplicate
# the list of recipes.
begin
latest_build_dir = ""
recipes.each do |recipe|
latest_build_dir = recipe.building_directory
puts " >> #{recipe.name}".colorize :white
raise "oh no, download failed" unless recipe.download
recipe.download
if download_only
exit 0
end
next if download_only
raise "oh no, extraction failed" unless recipe.extract
recipe.extract
raise "oh no, build failed" unless recipe.build
raise "oh no, packaging failed" unless recipe.package
recipe.build
recipe.package
recipe.clean unless do_not_clean
end
rescue e : Package::Exception
STDERR.puts "!! #{e.message}".colorize(:red).bright
STDERR.puts "!! You may want to inspect the build directory at #{latest_build_dir}"
exit 1
rescue e
STDERR.puts "!! An unexpected error occured:".colorize(:red).bright
STDERR.puts e
exit 1
end

View File

@ -1,4 +1,3 @@
require "uuid"
require "uri"
require "file_utils"
@ -9,6 +8,7 @@ require "./context.cr"
require "./package.cr"
require "./instructions.cr"
require "./sources.cr"
require "./exception.cr"
# 🤔
class URI
@ -184,28 +184,32 @@ class Package::Recipe
@dirname || "#{name}-#{version}"
end
def download : Bool
sources
.compact_map do |url|
def download
sources.each do |url|
unless File.exists? url.basename
@context.run @context.sources_directory, "wget", [ url.to_s, "-O", url.basename ]
status = @context.run @context.sources_directory, "wget", [ url.to_s, "-O", url.basename ]
raise DownloadError.new self, url unless status.success?
end
end
.map(&.success?)
.reduce(true) { |a, b| a && b }
end
def extract : Bool
def extract
Dir.mkdir_p building_directory
sources
.map do |url|
sources.each do |url|
basename = url.basename
@context.run building_directory, "bsdtar", [ "xvf", @context.sources_directory + "/" + url.basename ]
status = @context.run(
building_directory,
"bsdtar", [
"xvf",
@context.sources_directory + "/" + url.basename
]
)
raise ExtractionError.new self, url unless status.success?
end
.map(&.success?)
.reduce true { |a, b| a && b }
end
# TODO:
@ -215,9 +219,7 @@ class Package::Recipe
# - 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 : Bool
success = true
def build
Dir.mkdir_p fake_root_directory
ENV["PKG"] = fake_root_directory
@ -227,7 +229,7 @@ class Package::Recipe
instructions.to_a.each do |instruction|
if instruction.run(@context, self).failed?
success = false
raise BuildError.new self, "Building (#{instruction.phase} phase) failed."
break
end
end
@ -236,11 +238,11 @@ class Package::Recipe
ENV["PKG"] = nil
success = false if Dir.children(fake_root_directory).size == 0
if Dir.children(fake_root_directory).size == 0
raise BuildError.new self, "No file was installed in the fake root."
end
do_splits if success
success
do_splits
end
private def do_splits
@ -295,18 +297,18 @@ class Package::Recipe
# TODO:
# - Errors management. Stop at first failure?
# - Splits. This should be done between #build and #package.
def package : Bool
def package
# This tries to build them all and stops at the first failure
# (failures are currently reported by Context#package)
(@packages + auto_splits)
.find do |package|
(@packages + auto_splits).each do |package|
if package.automatic && ! File.exists? package.fake_root_directory
next
end
! @context.package package
unless @context.package package
raise PackagingError.new self, package
end
end
.== nil
end
def clean