require "../context.cr" require "../recipe.cr" require "../package.cr" require "../backends/baguette.cr" require "option_parser" require "baguette-crystal-base" class Context class_property command = "not-implemented" class_property configuration_file = "#{SYSCONF_DIR}/packaging.cfg" class_property args = Array(String).new end class Action property the_call = {} of String => Proc(Nil) property context : Package::Context def initialize(@context) @the_call["package"] = ->package end def package pkg, uuid = Context.args[0..1] Baguette::Log.info "package #{pkg} uuid #{uuid}" recipe = context.find_recipe(pkg) unless recipe Baguette::Log.error "Error: no recipes" exit 1 end begin # Change working UUID. old_uuid = recipe.working_uuid new_uuid = UUID.new(uuid) recipe.working_uuid = new_uuid recipe.packages.each do |pkg| if frd = pkg.fake_root_directory pkg.fake_root_directory = frd.gsub old_uuid.to_s, new_uuid.to_s end end latest_build_dir = recipe.building_directory context.title recipe.name # recipe.download # recipe.extract # recipe.build recipe.package # recipe.clean unless Context.do_not_clean rescue e : Package::Exception Baguette::Log.error "#{e.message}" Baguette::Log.error "You may want to inspect the build directory at #{latest_build_dir}" exit 1 rescue e Baguette::Log.error "An unexpected error occured:" Baguette::Log.error e exit 1 end end end def main opt_help = -> (parser : OptionParser) { parser.on "help", "Prints this help message." do puts parser exit 0 end } # Unrecognized parameters are used to create commands with multiple arguments. # Example: user add _login email phone_ # Here, login, email and phone are unrecognized arguments. # Still, the "user add" command expect them. unrecognized_args_to_context_args = -> (parser : OptionParser, n_expected_args : Int32) { # With the right args, these will be interpreted as serialized data. parser.unknown_args do |args| if args.size != n_expected_args Baguette::Log.error "expected number of arguments: #{n_expected_args}, received: #{args.size}" Baguette::Log.error "args: #{args}" Baguette::Log.error "#{parser}" exit 1 end args.each do |arg| Baguette::Log.debug "Unrecognized argument: #{arg} (adding to Context.args)" Context.args << arg end end } OptionParser.parse do |parser| parser.banner = "usage: baguette [options] command [parameters]" parser.on "package", "Create a package." do parser.banner = "Usage: baguette [opt] package package-name uuid" Baguette::Log.info "Create a Baguette package." Context.command = "package" opt_help.call parser # package name, uuid unrecognized_args_to_context_args.call parser, 2 end parser.on "-c configuration-file", "--config configuration-file", "Configuration file." do |f| Baguette::Log.info "configuration file is: #{f}" Context.configuration_file = f end parser.on "-h", "--help", "Show this help" do puts parser exit 0 end end context = Package::Context.new() context.repositories << "." context.read_configuration Context.configuration_file actions = Action.new context # Now we did read the intent, we should proceed doing what was asked. begin actions.the_call[Context.command].call rescue e Baguette::Log.info "The command is not recognized (or implemented)." end rescue e : OptionParser::Exception Baguette::Log.error e.message rescue e Baguette::Log.error "exception raised: #{e.message}" e.backtrace.try &.each do |line| STDERR << " - " << line << '\n' end end main