Class: SmartTodo::CLI
- Inherits:
-
Object
- Object
- SmartTodo::CLI
- Defined in:
- lib/smart_todo/cli.rb
Overview
This class is the entrypoint of the SmartTodo library and is responsible
to retrieve the command line options as well as iterating over each files/directories
to run the CommentParser on.
Instance Method Summary collapse
-
#define_options ⇒ OptionParser
An instance of OptionParser.
-
#dispatcher ⇒ Class
A Dispatchers::Base subclass.
-
#initialize(dispatcher = nil) ⇒ CLI
constructor
A new instance of CLI.
-
#normalize_path(path) ⇒ Array<String>
All the directories the parser should run on.
- #process_todos(todos) ⇒ Object
- #run(args = ARGV) ⇒ Object
- #validate_options! ⇒ void
Constructor Details
#initialize(dispatcher = nil) ⇒ CLI
Returns a new instance of CLI.
11 12 13 14 15 |
# File 'lib/smart_todo/cli.rb', line 11 def initialize(dispatcher = nil) @options = {} @errors = [] @dispatcher = dispatcher end |
Instance Method Details
#define_options ⇒ OptionParser
Returns an instance of OptionParser.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/smart_todo/cli.rb', line 57 def OptionParser.new do |opts| opts. = "Usage: smart_todo [options] file_or_path1 file_or_path2 ..." opts.on("--slack_token TOKEN") do |token| @options[:slack_token] = token end opts.on("--fallback_channel CHANNEL") do |channel| @options[:fallback_channel] = channel end opts.on("--dispatcher DISPATCHER") do |dispatcher| @options[:dispatcher] = dispatcher end opts.on("--repo [REPO]", "Repository name to include in notifications") do |repo| @options[:repo] = repo || File.basename(Dir.pwd) end end end |
#dispatcher ⇒ Class
Returns a Dispatchers::Base subclass.
76 77 78 |
# File 'lib/smart_todo/cli.rb', line 76 def dispatcher @dispatcher ||= Dispatchers::Base.class_for(@options[:dispatcher]) end |
#normalize_path(path) ⇒ Array<String>
Returns all the directories the parser should run on.
82 83 84 85 86 87 88 |
# File 'lib/smart_todo/cli.rb', line 82 def normalize_path(path) if File.file?(path) [path] else Dir["#{path}/**/*.rb"] end end |
#process_todos(todos) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/smart_todo/cli.rb', line 90 def process_todos(todos) events = Events.new dispatches = [] todos.each do |todo| = nil event_met = todo.events.find do |event| = events.public_send(event.method_name, *event.arguments) rescue => e = "Error while parsing #{todo.filepath} on event `#{event.method_name}` " \ "with arguments #{event.arguments.map(&:inspect)}: " \ "#{e.}" @errors << nil end @errors.concat(todo.errors) next unless event_met = append_context_if_applicable(, todo, event_met, events) dispatches << [, todo] end dispatches end |
#run(args = ARGV) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/smart_todo/cli.rb', line 18 def run(args = ARGV) paths = .parse!(args) paths << "." if paths.empty? comment_parser = CommentParser.new paths.each do |path| normalize_path(path).each do |filepath| comment_parser.parse_file(filepath) $stdout.print(".") $stdout.flush end end process_dispatches(process_todos(comment_parser.todos)) if @errors.empty? 0 else $stderr.puts "There were errors while checking for TODOs:\n" @errors.each do |error| $stderr.puts error end 1 end end |
#validate_options! ⇒ void
This method returns an undefined value.
52 53 54 |
# File 'lib/smart_todo/cli.rb', line 52 def dispatcher.(@options) end |