lib/pennyworth/cli.rb in pennyworth-3.2.0 vs lib/pennyworth/cli.rb in pennyworth-4.0.0

- old
+ new

@@ -1,61 +1,93 @@ -require "yaml" +# frozen_string_literal: true + require "thor" require "thor/actions" require "thor_plus/actions" +require "refinements/strings" +require "runcom" module Pennyworth # The Command Line Interface (CLI) for the gem. + # rubocop:disable Metrics/ClassLength class CLI < Thor include Thor::Actions include ThorPlus::Actions + using Refinements::Strings - package_name Pennyworth::Identity.version_label + package_name Identity.version_label - # Overwrites the Thor template source root. def self.source_root File.expand_path File.join(File.dirname(__FILE__), "templates") end - # Initialize. + def self.configuration + Runcom::Configuration.new file_name: Identity.file_name + end + def initialize args = [], options = {}, config = {} super args, options, config - @settings_file = File.join ENV["HOME"], ".pennyworth", "settings.yml" - @settings = load_yaml @settings_file end - desc "-s, [string=STRING]", "Manipulate strings." - map %w(-s --string) => :string - method_option :downcase, aliases: "-d", desc: "Downcase a string.", type: :array - method_option :upcase, aliases: "-u", desc: "Upcase a string.", type: :array - method_option :capitalize, aliases: "-c", desc: "Capitalize words in a string.", type: :array - method_option :length, aliases: "-l", desc: "Answer the length of a string.", type: :array - def string - case - when options[:downcase] - Pennyworth::Kits::String.downcase options[:downcase] - when options[:upcase] - Pennyworth::Kits::String.upcase options[:upcase] - when options[:capitalize] - Pennyworth::Kits::String.capitalize options[:capitalize] - when options[:length] - Pennyworth::Kits::String.length options[:length] - else say("Type 'pennyworth help string' for usage.") + desc "-s, [--string=VALUE]", "Manipulate strings." + map %w[-s --string] => :string + method_option :downcase, + aliases: "-d", + desc: "Downcase string.", + type: :boolean, + default: false + method_option :upcase, + aliases: "-u", + desc: "Upcase string.", + type: :boolean, + default: false + method_option :titleize, + aliases: "-t", + desc: "Capitalize each word and delimit with space or forward slash.", + type: :boolean, + default: false + method_option :camelcase, + aliases: "-c", + desc: "Capitalize each word and delimit with nothing or double colon.", + type: :boolean, + default: false + method_option :snakecase, + aliases: "-s", + desc: "Downcase each word and delimit with underscore or forward slash.", + type: :boolean, + default: false + method_option :size, + aliases: "-s", + desc: "Calculate string size.", + type: :boolean, + default: false + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/PerceivedComplexity + def string value + if options.downcase? then say(value.downcase) + elsif options.upcase? then say(value.upcase) + elsif options.titleize? then say(value.titleize) + elsif options.camelcase? then say(value.camelcase) + elsif options.snakecase? then say(value.snakecase) + elsif options.size? then say(value.size) + else say("Type 'pennyworth help string' for usage.") end end - desc "-i, [install]", "Install Alfred Workflows." - map %w(-i --install) => :install + desc "-i, [--install]", "Install Alfred Workflows." + map %w[-i --install] => :install def install say - if valid_file?(@settings[:alfred_settings_root], "Invalid directory for Alfred settings root") + alfred_settings_root = self.class.configuration.to_h[:alfred_settings_root] + + if valid_file?(alfred_settings_root, "Invalid directory for Alfred settings root") if yes? "Installing Alfred Workflows will destroy exiting workflows of the same name. Continue (y/n)?" info "Installing Alfred Workflows..." workflows = Dir.glob File.join(self.class.source_root, "workflows", "**") - alfred_workflows_root = File.join @settings[:alfred_settings_root], "workflows" + alfred_workflows_root = File.join alfred_settings_root, "workflows" workflows.each do |workflow| name = File.basename workflow destination = File.join alfred_workflows_root, name remove_file destination directory File.join("workflows", name), destination @@ -64,30 +96,43 @@ info "Alfred Workflows installed." else info "Alfred Workflows installation cancelled." end else - error "Invalid Alfred settings directory: #{@settings[:alfred_settings_root]}" + error "Invalid Alfred settings directory: #{alfred_settings_root}" end say end - desc "-e, [--edit]", "Edit #{Pennyworth::Identity.label} settings in default editor." - map %w(-e --edit) => :edit - def edit - `#{editor} #{@settings_file}` + desc "-c, [--config]", %(Manage gem configuration ("#{configuration.computed_path}").) + map %w[-c --config] => :config + method_option :edit, + aliases: "-e", + desc: "Edit gem configuration.", + type: :boolean, default: false + method_option :info, + aliases: "-i", + desc: "Print gem configuration.", + type: :boolean, default: false + def config + path = self.class.configuration.computed_path + + if options.edit? then `#{editor} #{path}` + elsif options.info? then say(path) + else help(:config) + end end - desc "-v, [--version]", "Show #{Pennyworth::Identity.label} version." - map %w(-v --version) => :version + desc "-v, [--version]", "Show gem version." + map %w[-v --version] => :version def version - say Pennyworth::Identity.version_label + say Identity.version_label end - desc "-h, [--help=HELP]", "Show this message or get help for a command." - map %w(-h --help) => :help + desc "-h, [--help=COMMAND]", "Show this message or get help for a command." + map %w[-h --help] => :help def help task = nil - say && super + say and super end end end