Sha256: e440b7180fe42ba08d3f87533abc92eab199ea2f2201d10c990ee43337472eca

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require_relative 'punchlist/options'

# XXX: need to include BUG in list
# XXX: need to include BUG in my rubocop config
# BUG need to fix the fact that we create blank lines on files with no issues
module Punchlist
  # Counts the number of 'todo' comments in your code.
  class Punchlist
    def initialize(outputter: STDOUT,
                   globber: Dir,
                   file_opener: File,
                   options_parser: Options.new(default_punchlist_line_regexp))
      @outputter = outputter
      @globber = globber
      @file_opener = file_opener
      @options_parser = options_parser
    end

    def run
      @options = @options_parser.parse_options

      analyze_files

      0
    end

    def source_files_glob
      @options[:glob] ||
        '{app,lib,test,spec,feature}/**/*.{rb,swift,scala,js,cpp,c,java,py}'
    end

    def analyze_files
      all_output = []
      source_files.each do |filename|
        all_output.concat(look_for_punchlist_items(filename))
      end
      @outputter.print render(all_output)
    end

    def source_files
      @globber.glob(source_files_glob)
    end

    def default_punchlist_line_regexp
      /XXX|TODO/
    end

    def punchlist_line_regexp
      return @regexp if @regexp

      regexp_string = @options[:regexp]
      if regexp_string
        @regexp = Regexp.new(regexp_string)
      else
        default_punchlist_line_regexp
      end
    end

    def look_for_punchlist_items(filename)
      lines = []
      line_num = 0
      @file_opener.open(filename, 'r') do |file|
        file.each_line do |line|
          line_num += 1
          lines << [filename, line_num, line] if line =~ punchlist_line_regexp
        end
      end
      lines
    end

    def render(output)
      lines = output.map do |filename, line_num, line|
        "#{filename}:#{line_num}: #{line}"
      end
      lines.join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
punchlist-1.0.0 lib/punchlist.rb