class Lintrunner::CLI

Public Instance Methods

run(args) click to toggle source

Takes an array of arguments Returns exit code

# File lib/lintrunner/cli.rb, line 8
def run(args)
  options = Options.new.parse(args)

  handle_options(options)

  path = options[:path]
  context = options[:context]

  # If context is different from path that we want to lint, check that it's up to date
  path != context && is_up_to_date?(context)

  warnings = []
  reporter = initialize_reporter(options[:reporter], path)

  config = JSON.parse(File.read(Dir.pwd + "/.lintrunner_config"))
  include_paths(config["include_paths"])
  require_files(config["require"])

  config["linters"].each do |name, config|
    next if config["disabled"]
    if config["command"] && config["parser"]
      parser = Lintrunner::Parser.const_get(config["parser"]).new
      executor = Lintrunner::Executor.new(
        command: config["command"],
        parser: parser
      )
    else
      executor = Lintrunner::Executor.new(
        plugin: Lintrunner::Plugin.const_get(config["plugin"])
      )
    end

    runner = Lintrunner::Runner.const_get(config["runner"]).new(
      path: path,
      match: config["match"],
      executor: executor
    )
    reporter.start(name)
    results = runner.run(reporter)
    reporter.finish(results)
    warnings.concat(results)
  end
  exit warnings.empty? ? 0 : 1
end

Private Instance Methods

handle_options(options) click to toggle source
# File lib/lintrunner/cli.rb, line 59
def handle_options(options)

end
include_paths(include_paths = []) click to toggle source
# File lib/lintrunner/cli.rb, line 87
def include_paths(include_paths = [])
  $:.unshift(*include_paths)
end
initialize_reporter(reporter, path) click to toggle source
# File lib/lintrunner/cli.rb, line 55
def initialize_reporter(reporter, path)
  Lintrunner::Reporter.const_get(reporter.capitalize).new(path)
end
is_up_to_date?(path) click to toggle source
# File lib/lintrunner/cli.rb, line 63
def is_up_to_date?(path)
  context_repo = Rugged::Repository.new(path)
  return unless context_repo.branches['master'].head?
  return if context_repo.branches['master'].upstream.nil?
  Dir.chdir path do
    # update remote ref
    `git fetch #{context_repo.branches['master'].upstream.name.split("/").join(" ")} > /dev/null 2>&1`
    status_output = `git status`
    if status_output =~ /Your branch is behind .* and can be fast-forwarded./

      puts "Repo is not up to date!".color(:red)
      puts "Would you like to update it (Y/N)?"
      input = STDIN.gets
      if input.strip == "Y"
        `git pull --ff-only #{context_repo.branches['master'].upstream.name.split("/").join(" ")}`
      else
        exit 1
      end
    end
  end
rescue Rugged::RepositoryError => e
  # noop
end
require_files(files) click to toggle source
# File lib/lintrunner/cli.rb, line 91
def require_files(files)
  files.each do |file|
    require file
  end
end