204 lines
4.9 KiB
Crystal
204 lines
4.9 KiB
Crystal
|
require "option_parser"
|
||
|
require "file_utils"
|
||
|
require "colorize"
|
||
|
require "baguette-crystal-base"
|
||
|
|
||
|
require "../context.cr"
|
||
|
require "../recipe.cr"
|
||
|
require "../do.cr"
|
||
|
|
||
|
extend Package
|
||
|
|
||
|
context = Context.new()
|
||
|
context.repositories << "."
|
||
|
|
||
|
class CLIOpts
|
||
|
class_property requested_recipes = [] of String
|
||
|
class_property download_only = false
|
||
|
class_property do_not_clean = false
|
||
|
class_property watch_only = false
|
||
|
class_property skip_build_dependencies = false
|
||
|
class_property print_deps = false
|
||
|
class_property only_print_configuration = false
|
||
|
|
||
|
class_property configuration_file = "#{SYSCONF_DIR}/packaging.cfg"
|
||
|
class_property configuration_file_requested = false
|
||
|
end
|
||
|
|
||
|
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("-c FILE", "--conf FILE", "Use a configuration file other than the default one.") { |file|
|
||
|
CLIOpts.configuration_file = file
|
||
|
CLIOpts.configuration_file_requested = true
|
||
|
}
|
||
|
|
||
|
parser.on("-D", "--download-only", "Only download sources, do not build.") {
|
||
|
CLIOpts.download_only = true
|
||
|
}
|
||
|
|
||
|
parser.on("-k", "--keep-work-dir", "Do not clean after building.") {
|
||
|
CLIOpts.do_not_clean = true
|
||
|
}
|
||
|
|
||
|
parser.on("-h", "--help", "Prints this help message.") {
|
||
|
puts parser
|
||
|
exit 0
|
||
|
}
|
||
|
|
||
|
parser.on("-V", "--version", "Prints package's version and exits.") {
|
||
|
puts Package::Version
|
||
|
exit 0
|
||
|
}
|
||
|
|
||
|
parser.on("-w", "--watch", "Checks if the recipe is up to date and exits.") {
|
||
|
CLIOpts.watch_only = true
|
||
|
}
|
||
|
|
||
|
parser.on("-B", "--print-deps", "Prints build dependencies and exits.") {
|
||
|
CLIOpts.print_deps = true
|
||
|
}
|
||
|
|
||
|
parser.on("-v", "--verbose", "Runs more verbosely.") {
|
||
|
Baguette::Context.verbosity += 1
|
||
|
}
|
||
|
|
||
|
parser.on("-q", "--quiet", "Runs more quietely.") {
|
||
|
Baguette::Context.verbosity -= 1
|
||
|
}
|
||
|
|
||
|
parser.on("-n", "--ignore-dependencies", "Do not try to install build-dependencies.") {
|
||
|
CLIOpts.skip_build_dependencies = true
|
||
|
}
|
||
|
|
||
|
parser.on("-p", "--print-configuration", "Only print configuration.") {
|
||
|
CLIOpts.only_print_configuration = true
|
||
|
}
|
||
|
|
||
|
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|
|
||
|
CLIOpts.requested_recipes = arg
|
||
|
end
|
||
|
end
|
||
|
|
||
|
|
||
|
# Verify package-create dependencies: tar, strip
|
||
|
Do.require_cmd "bsdtar"
|
||
|
Do.require_cmd "strip"
|
||
|
|
||
|
if File.exists? CLIOpts.configuration_file
|
||
|
context.read_configuration CLIOpts.configuration_file
|
||
|
elsif CLIOpts.configuration_file_requested
|
||
|
STDERR.puts "ERROR: configuration file '#{CLIOpts.configuration_file}' does not exist"
|
||
|
exit 1
|
||
|
end
|
||
|
|
||
|
if CLIOpts.only_print_configuration
|
||
|
pp! context
|
||
|
exit 0
|
||
|
end
|
||
|
|
||
|
found_recipes = CLIOpts.requested_recipes.map do |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.
|
||
|
|
||
|
if CLIOpts.watch_only
|
||
|
if recipes.size == 0
|
||
|
context.repositories.each do |repo|
|
||
|
Dir.children(repo).each do |i|
|
||
|
recipe_file_name = "#{repo}/#{i}/recipe.spec"
|
||
|
if File.exists? recipe_file_name
|
||
|
recipes << context.read_recipe recipe_file_name
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
recipes.each &.watch
|
||
|
exit 0
|
||
|
end
|
||
|
|
||
|
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 CLIOpts.print_deps
|
||
|
puts dependencies.join "\n"
|
||
|
exit 0
|
||
|
end
|
||
|
|
||
|
# Install build dependencies
|
||
|
if dependencies.size > 0 && ! CLIOpts.skip_build_dependencies && !CLIOpts.download_only
|
||
|
context.install dependencies
|
||
|
end
|
||
|
|
||
|
recipes.each do |recipe|
|
||
|
latest_build_dir = recipe.building_directory
|
||
|
|
||
|
Baguette::Context.logfile_path = "#{context.working_directory}/"
|
||
|
Baguette::Context.logfile_path += "#{recipe.working_uuid}.log"
|
||
|
|
||
|
Baguette::Log.title recipe.name
|
||
|
|
||
|
recipe.download
|
||
|
|
||
|
next if CLIOpts.download_only
|
||
|
|
||
|
recipe.extract
|
||
|
|
||
|
# Perform every building operation:
|
||
|
# sources-split, (pre-)(configure|build|install) and post-install
|
||
|
recipe.run
|
||
|
|
||
|
recipe.package
|
||
|
|
||
|
recipe.clean unless CLIOpts.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
|