This repository has been archived on 2022-01-17. You can view files and clone it, but cannot push or open issues/pull-requests.
packaging/src/main.cr

122 lines
2.7 KiB
Crystal
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require "option_parser"
require "file_utils"
require "colorize"
require "./context.cr"
require "./recipe.cr"
extend Package
context = Context.new()
context.packaging_backend = "apk"
context.repositories << "."
requested_recipes = [] of String
download_only = false
do_not_clean = false
used_X = false
OptionParser.parse! do |parser|
parser.banner = "Usage: package [options] <port <port <…>>>"
parser.on("-X DIR", "--repository DIR", "Sets a ports repository for finding dependencies (default=.).") { |dir|
unless used_X
while context.repositories.pop?
end
used_X = true
end
context.repositories << dir
}
parser.on("-D", "--download-only", "Only download sources, do not build.") {
download_only = true
}
parser.on("-k", "--keep-work-dir", "Do not clean after building.") {
do_not_clean = true
}
parser.on("-h", "--help", "Prints this help message.") {
puts parser
exit 0
}
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser
exit(1)
end
parser.unknown_args do |arg|
requested_recipes = arg
end
end
found_recipes = requested_recipes.map do |name|
puts context.find_recipe(name)
context.find_recipe(name) || name
end
invalid_recipes = found_recipes.select &.is_a?(String)
if invalid_recipes.size > 0
STDERR.puts "ERROR: some of the requested recipes could not be found:"
invalid_recipes.each do |name|
STDERR.puts " - #{name}"
end
STDERR.puts " (try using -X?)"
end
recipes = found_recipes.compact_map do |recipe_or_string|
recipe_or_string unless recipe_or_string.is_a?(String)
end
# FIXME: Now we need to build their respective deptrees and to deduplicate
# the list of recipes.
begin
latest_build_dir = ""
dependencies = [] of String
recipes.each do |recipe|
recipe.build_dependencies.each do |dep|
dependencies << dep unless dependencies.any? &.==(dep)
end
end
if dependencies.size > 0
# FIXME: Well probably want to have backends other than apk at some point.
r = context.sh "apk add #{ENV["APK_FLAGS"]? || "-i"} #{dependencies.join " "}"
if r.exit_status != 0
STDERR.puts "!! Running apk failed!"
exit 8
end
end
recipes.each do |recipe|
latest_build_dir = recipe.building_directory
puts " >> #{recipe.name}".colorize :white
recipe.download
next if download_only
recipe.extract
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