lib/kameleon/cli.rb in kameleon-builder-2.10.2 vs lib/kameleon/cli.rb in kameleon-builder-2.10.3

- old
+ new

@@ -9,11 +9,11 @@ module CLI class Repository < Thor include Thor::Actions - desc "add <NAME> <GIT_URL>", "Adds a new repository named <NAME> cloned from at <GIT_URL>." + desc "add <NAME> <GIT_URL>", "Adds a new repository named <NAME> cloned from <GIT_URL>." method_option :branch, :type => :string , :default => nil, :desc => "checkout <BRANCH>", :aliases => "-b" def add(name, url) @@ -30,37 +30,42 @@ desc "update <NAME>", "Updates repository named <NAME> from git" def update(name) Kameleon::Repository.update(name) end - map %w(-h --help) => :help - map %w(ls) => :list desc "remove <NAME>", "Remove repository named <NAME>" def remove(name) Kameleon::Repository.remove(name) end - map %w(-h --help) => :help + + desc "commands", "Lists all available commands", :hide => true + def commands + puts Repository.all_commands.keys - ["commands"] + end + map %w(ls) => :list map %w(rm) => :remove + map %w(completions) => :commands end class Template < Thor include Thor::Actions -# register CLI::Repository, 'repository', 'repository', 'Manages set of remote git repositories' - def self.source_root Kameleon.env.repositories_path end desc "list", "Lists all available templates" + method_option :progress, :type => :boolean, :default => true, + :desc => "Show progress bar while resolving templates", + :aliases => "-p" def list - Kameleon.ui.info "The following templates are available in " \ - "#{ Kameleon.env.repositories_path }:" - Utils.list_recipes(Kameleon.env.repositories_path) + Kameleon.ui.shell.say "Recipe templates available in: ", :red, false + Kameleon.ui.shell.say Kameleon.env.repositories_path.to_s, :yellow + Utils.list_recipes(Kameleon.env.repositories_path, options[:progress], true) end desc "import <TEMPLATE_NAME>", "Imports the given template" method_option :global, :type => :hash , :default => {}, :aliases => "-g", @@ -108,12 +113,18 @@ end tpl = RecipeTemplate.new(template_path) tpl.resolve! :strict => false tpl.display_info(false) end - map %w(-h --help) => :help + + desc "commands", "Lists all available commands", :hide => true + def commands + puts Template.all_commands.keys - ["commands"] + end + map %w(ls) => :list + map %w(completions) => :commands end end class Main < Thor @@ -131,27 +142,28 @@ class_option :debug, :type => :boolean, :default => Kameleon.default_values[:debug], :desc => "Enables debug output for kameleon developpers" class_option :script, :type => :boolean, :default => Kameleon.default_values[:script], :desc => "Never prompts for user intervention", :aliases => "-s" - map %w(-h --help) => :help desc "version", "Prints the Kameleon's version information" def version puts "Kameleon version #{Kameleon::VERSION}" end - map %w(-v --version) => :version def self.source_root Kameleon.env.repositories_path end desc "list", "Lists all defined recipes in the current directory" + method_option :progress, :type => :boolean, :default => false, + :desc => "Show progress bar while resolving templates", + :aliases => "-p" def list - Utils.list_recipes(Kameleon.env.workspace) + Kameleon.ui.shell.say "Workspace recipes:", :red + Utils.list_recipes(Kameleon.env.workspace, options[:progress]) end - map %w(ls) => :list desc "new <RECIPE_PATH> <TEMPLATE_NAME>", "Creates a new recipe from template <TEMPLATE_NAME>" method_option :global, :type => :hash , :default => {}, :aliases => "-g", :desc => "Set custom global variables." @@ -190,11 +202,22 @@ end Dir::mktmpdir do |tmp_dir| recipe_temp = File.join(tmp_dir, File.basename(recipe_path)) ## copying recipe File.open(recipe_temp, 'w+') do |file| - extend_erb_tpl = File.join(Kameleon.erb_dirpath, "extend.erb") + message="Use extend ERB template: " + extend_erb_tpl = [ + Kameleon.env.repositories_path.join(template_name + ".erb"), + Pathname.new(template_name).dirname.ascend.to_a.push(Pathname.new("")).map do |p| + Kameleon.env.repositories_path.join(p, ".kameleon-extend.yaml.erb") + end, + Pathname.new(Kameleon.erb_dirpath).join("extend.yaml.erb") + ].flatten.find do |f| + Kameleon.ui.verbose(message + f.to_s) + message = "-> Not found, fallback: " + File.readable?(f) + end erb = ERB.new(File.open(extend_erb_tpl, 'rb') { |f| f.read }) result = erb.result(binding) file.write(result) end copy_file(recipe_temp, recipe_path) @@ -207,13 +230,10 @@ :default => {}, :aliases => "-g", :desc => "Set custom global variables." method_option :from_cache, :type => :string , :default => nil, :desc => "Get info from a persistent cache tar file (ignore recipe path)" - method_option :dryrun, :type => :boolean , - :default => false, - :desc => "Show the build sequence but do not actually build" method_option :relative, :type => :boolean , :default => false, :desc => "Make pathnames relative to the current working directory" def info(*recipe_paths) if recipe_paths.empty? @@ -417,19 +437,31 @@ Kameleon.ui.info("Total duration : #{total_time} secs") end end desc "commands", "Lists all available commands", :hide => true - def commands - puts Main.all_commands.keys - ["commands", "completions"] + def commands(context="main") + Kameleon.ui.debug("Commands for '#{context}':") + case context + when "main" + puts Main.all_commands.keys - ["commands"] + when "repository" + invoke CLI::Repository, "commands", [], [] + when "template" + invoke CLI::Template, "commands", [], [] + end end desc "source_root", "Prints the kameleon directory path", :hide => true def source_root puts Kameleon.source_root end + map %w(-v --version) => :version + map %w(ls) => :list + map %w(completions) => :commands + def initialize(*args) super self.options ||= {} Kameleon.env = Kameleon::Environment.new(self.options) if !$stdout.tty? or !options["color"] @@ -443,10 +475,20 @@ Kameleon.ui.level = "verbose" end Kameleon.ui.verbose("The level of output is set to #{Kameleon.ui.level}") end - def self.start(*) + def self.start(*args) + # `kameleon build -h` does not work without the following, except for subcommands... + # Ref: https://stackoverflow.com/a/49044225/6431461 + if (Thor::HELP_MAPPINGS & ARGV).any? and subcommands.grep(/^#{ARGV[0]}/).empty? + Kameleon.ui.debug("Apply workaround to handle the help command in #{ARGV}") + Thor::HELP_MAPPINGS.each do |cmd| + if match = ARGV.delete(cmd) + ARGV.unshift match + end + end + end super rescue Exception => e Kameleon.ui = Kameleon::UI::Shell.new raise e end