class Goodcheck::Commands::Check

Constants

DEFAULT_EXCLUSIONS

Attributes

config_path[R]
force_download[R]
home_path[R]
reporter[R]
rules[R]
stderr[R]
targets[R]

Public Class Methods

new(config_path:, rules:, targets:, reporter:, stderr:, home_path:, force_download:) click to toggle source
# File lib/goodcheck/commands/check.rb, line 18
def initialize(config_path:, rules:, targets:, reporter:, stderr:, home_path:, force_download:)
  @config_path = config_path
  @rules = rules
  @targets = targets
  @reporter = reporter
  @stderr = stderr
  @force_download = force_download
  @home_path = home_path
end

Public Instance Methods

each_check() { |buffer, rule, trigger| ... } click to toggle source
# File lib/goodcheck/commands/check.rb, line 71
def each_check
  targets.each do |target|
    Goodcheck.logger.info "Checking target: #{target}"
    each_file target, immediate: true do |path|
      Goodcheck.logger.debug "Checking file: #{path}"
      reporter.file(path) do
        buffers = {}

        config.rules_for_path(path, rules_filter: rules) do |rule, glob, trigger|
          Goodcheck.logger.debug "Checking rule: #{rule.id}"
          begin
            encoding = glob&.encoding || Encoding.default_external.name

            if buffers[encoding]
              buffer = buffers[encoding]
            else
              content = path.read(encoding: encoding).encode(Encoding.default_internal || Encoding::UTF_8)
              buffer = Buffer.new(path: path, content: content)
              buffers[encoding] = buffer
            end

            yield buffer, rule, trigger
          rescue ArgumentError => exn
            stderr.puts "#{path}: #{exn.inspect}"
          end
        end
      end
    end
  end
end
each_file(path, immediate: false) { |path| ... } click to toggle source
# File lib/goodcheck/commands/check.rb, line 102
def each_file(path, immediate: false, &block)
  case
  when path.symlink?
    # noop
  when path.directory?
    case
    when DEFAULT_EXCLUSIONS.include?(path.basename.to_s)
      # noop
    when immediate || !excluded?(path)
      path.children.sort.each do |child|
        each_file(child, &block)
      end
    end
  when path.file?
    case
    when path == config_path
      # Skip the config file unless explicitly given by command line
      yield path if immediate
    when excluded?(path)
      # Skip excluded files unless explicitly given by command line
      yield path if immediate
    else
      yield path
    end
  end
end
excluded?(path) click to toggle source
# File lib/goodcheck/commands/check.rb, line 129
def excluded?(path)
  config.exclude_path?(path)
end
missing_rules() click to toggle source
# File lib/goodcheck/commands/check.rb, line 64
def missing_rules
  @missing_rules ||= begin
    config_rule_ids = config.rules.map(&:id)
    rules.reject { |rule| config_rule_ids.include?(rule) }
  end
end
run() click to toggle source
# File lib/goodcheck/commands/check.rb, line 28
def run
  handle_config_errors(stderr) do
    issue_reported = false

    reporter.analysis do
      load_config!(force_download: force_download, cache_path: cache_dir_path)

      unless missing_rules.empty?
        missing_rules.each do |rule|
          stderr.puts "missing rule: #{rule}"
        end
        return EXIT_ERROR
      end

      each_check do |buffer, rule, trigger|
        reported_issues = Set[]

        reporter.rule(rule) do
          analyzer = Analyzer.new(rule: rule, buffer: buffer, trigger: trigger)
          analyzer.scan do |issue|
            next if issue.location && buffer.line_disabled?(issue.location.start_line)
            if reported_issues.add?(issue)
              issue_reported = true
              reporter.issue(issue)
            end
          end
        end
      end
    end

    reporter.summary

    issue_reported ? EXIT_MATCH : EXIT_SUCCESS
  end
end