lib/pragmater/cli.rb in pragmater-0.1.0 vs lib/pragmater/cli.rb in pragmater-1.0.0
- old
+ new
@@ -14,26 +14,29 @@
package_name Pragmater::Identity.version_label
def initialize args = [], options = {}, config = {}
super args, options, config
+ @configuration = Configuration.new Identity.file_name
end
desc "-a, [--add=ADD]", "Add pragma comments to source file(s)."
map %w(-a --add) => :add
method_option :comments, aliases: "-c", desc: "Pragma comments", type: :array, default: []
- method_option :extensions, aliases: "-e", desc: "File extension whitelist", type: :array, default: [".rb", ".rake"]
+ method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
def add path
- write path, options[:comments], options[:extensions], :add
+ settings = configuration.merge add: {comments: options[:comments], whitelist: options[:whitelist]}
+ write path, settings, :add
end
desc "-r, [--remove=REMOVE]", "Remove pragma comments from source file(s)."
map %w(-r --remove) => :remove
method_option :comments, aliases: "-c", desc: "Pragma comments", type: :array, default: []
- method_option :extensions, aliases: "-e", desc: "File extension whitelist", type: :array, default: [".rb", ".rake"]
+ method_option :whitelist, aliases: "-w", desc: "File extension whitelist", type: :array, default: []
def remove path
- write path, options[:comments], options[:extensions], :remove
+ settings = configuration.merge remove: {comments: options[:comments], whitelist: options[:whitelist]}
+ write path, settings, :remove
end
desc "-e, [--edit]", "Edit #{Pragmater::Identity.label} settings in default editor."
map %w(-e --edit) => :edit
def edit
@@ -54,25 +57,41 @@
say && super
end
private
+ attr_reader :configuration
+
+ def whitelisted_files path, whitelist
+ file_filter = whitelist.empty? ? %(#{path}/**/*) : %(#{path}/**/*{#{whitelist.join ","}})
+ Pathname.glob(file_filter).select(&:file?)
+ end
+
def update_file path, comments, action
Writer.new(path, comments).public_send action
- say "Updated: #{path}."
+ info "Processed: #{path}."
+ rescue ArgumentError => error
+ formatted_message = error.message
+ formatted_message[0] = formatted_message[0].capitalize
+ error "#{formatted_message}: #{path}."
end
- def write path, comments, extensions, action
- pathname = Pathname path
-
+ def update_files path, comments, whitelist, action
case
- when pathname.file?
- update_file pathname, comments, action
- when pathname.directory?
- files = Pathname.glob %(#{pathname}/**/*{#{extensions.join ","}})
- files.each { |file_path| update_file file_path, comments, action }
+ when path.file?
+ update_file path, comments, action
+ when path.directory?
+ whitelisted_files(path, whitelist).each { |file_path| update_file file_path, comments, action }
else
error "Invalid path: #{path}."
end
+ end
+
+ def write path, settings, action
+ pathname = Pathname path
+ comments = Array settings.dig(action).dig(:comments)
+ whitelist = Array settings.dig(action).dig(:whitelist)
+
+ update_files pathname, comments, whitelist, action
end
end
end