Sha256: 8ae36a691af5b6081ab25a07c5f5e45bf17d0ffc661bf47f07dd6357088c5392

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "boxt_ruby_style_guide"
require "rubocop"

require "boxt_ruby_style_guide/git_diff"
require "boxt_ruby_style_guide/filepath_matcher"

##
# The base branch to compare HEAD with for changes
BASE_BRANCH = "develop"

##
# Name of the master Rubocop lint task to run
RUBOCOP_TASK_NAME = :"lint:execute_rubocop"

##
# Pattern for matching autofix options
AUTO_REGEX = /\A-a\Z/i.freeze

namespace :lint do
  desc "Runs rubocop against all files with committed changes different from base branch"
  task :rubocop do
    Rake::Task[RUBOCOP_TASK_NAME].invoke
  end

  desc "Runs rubocop against all files using -a (soft autofix) option"
  task :rubocop_a do
    Rake::Task[RUBOCOP_TASK_NAME].invoke("-a")
  end

  desc "Runs rubocop against all files using -A (hard autofix) option"
  task :rubocop_A do
    Rake::Task[RUBOCOP_TASK_NAME].invoke("-A")
  end

  task :execute_rubocop, [:auto_flag] do |_t, args|
    if sanitized_file_paths.any?
      # Sanitize args to make sure only a single "a" or "A" is accepted
      auto_flag = AUTO_REGEX.match(args[:auto_flag])
      exec("bundle exec rubocop #{sanitized_file_paths.join(" ")} #{auto_flag}".strip)
    else
      puts "No matching Ruby files changed"
    end
  end
end

private

# Returns Array
def sanitized_file_paths
  base = ENV.fetch("RUBOCOP_LINT_BASE", BASE_BRANCH)
  changed_files = BoxtRubyStyleGuide::GitDiff.new(base).all
  BoxtRubyStyleGuide::FilepathMatcher.new(*changed_files).all_matches
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boxt_ruby_style_guide-7.7.3 lib/tasks/lint.rake
boxt_ruby_style_guide-7.7.2 lib/tasks/lint.rake