class TodoLint::Cli

Here we bring together all the pieces and see if it comes together

Attributes

options[R]

Options hash for all configurations @return [Hash] @api private

path[R]

Where are we looking for files? @return [String] @api private

Public Class Methods

new(args) click to toggle source

Startup the thing that actually checks the files for todos! @example

Cli.new(["-i", ".rb,.js"])

@api public

# File lib/todo_lint/cli.rb, line 10
def initialize(args) # rubocop:disable
  @options = Options.new.parse(args)
  if @options[:config_file]
    @options.merge!(ConfigFile.new.read_config_file(@options[:config_file]))
  elsif File.exist?("./.todo_lint.yml")
    @options.merge!(ConfigFile.new.read_config_file("./.todo_lint.yml"))
  end
  @path = File.expand_path(".")
  add_default_extensions unless @options.fetch(:files, []).any?
end

Public Instance Methods

load_files(file_finder) click to toggle source

Loads the files to be read @return [Array<String>] @example cli.load_files(file_finder) @api public

# File lib/todo_lint/cli.rb, line 39
def load_files(file_finder)
  if file_finder.options.fetch(:files).empty?
    file_finder.list(*options[:extensions])
  else
    file_finder.options.fetch(:files, [])
  end
end
run!() click to toggle source

Perform the actions requested based on the options specified

@example

Cli.new(["-i", ".rb"]).run!

@return exit code 0 for success, 1 for failure @api public

# File lib/todo_lint/cli.rb, line 27
def run!
  if options[:report]
    print_report
  else
    lint_codebase
  end
end

Private Instance Methods

add_default_extensions() click to toggle source

Set default extensions if none given @api private @return [Array<String>]

# File lib/todo_lint/cli.rb, line 130
def add_default_extensions
  return if options[:extensions]
  options[:extensions] = [".rb"]
end
lint_codebase() click to toggle source

Check requested files for problematic TODO comments @return exit code 0 for success, 1 for failure @api private

# File lib/todo_lint/cli.rb, line 62
def lint_codebase
  finder = FileFinder.new(path, options)
  files = load_files(finder)
  files_count = files.count
  reports = files.map do |file|
    Todo.within(File.open(file), :config => @options).map do |todo|
      reporter = Reporter.new(todo,
                              :judge => Judge.new(todo))
      reporter.report.tap do |report|
        print Rainbow(".").public_send(report.nil? ? :green : :red)
      end
    end
  end.flatten.compact
  if reports.any?
    puts
    reports.each do |report|
      puts report
    end
    puts "\nFound #{pluralize('problematic todo', reports.count)} in " \
      "#{pluralize('file', files_count)}"
    exit 1
  else
    puts "\nGreat job! No overdue todos in " \
         "#{pluralize('file', files_count)}"
    exit 0
  end
end
pluralize(word, count) click to toggle source

Pluralize a word based on the count @return [String] @api private

# File lib/todo_lint/cli.rb, line 122
def pluralize(word, count)
  s = count == 1 ? "" : "s"
  "#{count} #{word + s}"
end
print_report() click to toggle source

Print report of todos in codebase, then exit

@return by exiting with 0 @api private