boxt_ruby_style_guide 7.6.0 → 7.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fdb78910aef325a37d80abf635b2c675d190e63669133b14f19ebcad1c480d9
4
- data.tar.gz: 5c58cb56cfeb23d4769f8681bd21c16b5f39a3401ecbb6a016b3ba7cdf1c68f7
3
+ metadata.gz: 3269906781941588a2da2ed478c324852a270245e9aeb64cddc877835d6ede19
4
+ data.tar.gz: 9b1255d5557fd33a70e3cb5f6fccf61d5c7ac44f37055bff8e193108927e00d8
5
5
  SHA512:
6
- metadata.gz: be5aa640996292e3d526e91a27fb2365eebb84c2e2c02f32f0a1d2b47dbe04340e0e11f15a30d9b6c60a2b25a56430dce48a950cf2522b227ce0f51acb99c58d
7
- data.tar.gz: 667f275e3560ec91b6c20ca8bdd6f58cfc7f079c1207812d891af364e5a086823e0f7679c8e0537598c9dd4f03d230c7853b8828787a481eb2a8e5b023adfc28
6
+ metadata.gz: d1ce04bee4be54bdc9b5bdb4fa55695d364e32546f0c7a0fa93fb458c72d38510cc48667f51809387e39ab12e37058592a3ac4ea3d43e7910177e17f2da6179b
7
+ data.tar.gz: 4516330c35be5df90616fa65cf8370afa368b441fe3d495da0e5b9542bafd12d0218b1a331eeaa3c548cf93d3ea4f1a26098c5ad7da25ff6b8906dd5b4f543b6
data/README.md CHANGED
@@ -41,10 +41,10 @@ Add a `.rubocop.yml` file to the root of your project with the following setting
41
41
  inherit_gem:
42
42
  boxt_ruby_style_guide:
43
43
  - default.yml # use default cops
44
- - pending.yml # use pending cops
44
+ - pending.yml # use pending cops
45
45
  - rails.yml # use Rails cops - see Additional Extensions/Cops
46
- - rails-pending.yml # use pending rails cops
47
- - rspec.yml # use rspec cops
46
+ - rails-pending.yml # use pending rails cops
47
+ - rspec.yml # use rspec cops
48
48
  ```
49
49
 
50
50
  ### Additional Extensions/Cops
@@ -60,7 +60,7 @@ To enable add the following to your `.rubocop.yml` file.
60
60
  ```yml
61
61
  inherit_gem:
62
62
  boxt_ruby_style_guide:
63
- # .... add cops
63
+ # .... add cops
64
64
 
65
65
  require:
66
66
  - rubocop-faker # if your project is using the Faker gem then add this
@@ -75,9 +75,17 @@ Lint tasks to run against files listed as changed by Git.
75
75
  To run `rubocop` against any changed files use:
76
76
 
77
77
  ```sh
78
- rake lint:rubocop
78
+ rake lint:rubocop RUBOCOP_LINT_BASE=your-base-branch
79
79
  ```
80
80
 
81
+ To run `rubocop` with autofix, use one of the following:
82
+
83
+ ```sh
84
+ rake lint:rubocop_a RUBOCOP_LINT_BASE=your-base-branch
85
+ rake lint:rubocop_A RUBOCOP_LINT_BASE=your-base-branch
86
+ ```
87
+
88
+
81
89
  If there are no changed files the commands will run against all files.
82
90
 
83
91
  ## Editor Plugins
data/VERSION CHANGED
@@ -1 +1 @@
1
- 7.6.0
1
+ 7.7.0
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoxtRubyStyleGuide
4
+ ##
5
+ # Sanitizes a list of filepaths based on Rubocops exclusions
6
+ class FilepathMatcher
7
+ require "yaml"
8
+
9
+ ##
10
+ # Compare a given filepath with a grep-style filename pattern
11
+ FILEPATH_PATTERN_MATCH = proc do |filepath, pattern|
12
+ File.fnmatch(pattern, filepath)
13
+ end
14
+
15
+ ##
16
+ # Array of file patterns to make sure we only check files that are Ruby files
17
+ INCLUDE_PATTERNS = %w[
18
+ Gemfile
19
+ Rakefile
20
+ *.gemspec
21
+ **/*.rb
22
+ **/*.rake
23
+ ].freeze
24
+
25
+ ##
26
+ # Array of the excluded files from Rubocop default.yml config
27
+ EXCLUDE_PATTERNS = YAML.load_file(
28
+ BoxtRubyStyleGuide.root.join("default.yml")
29
+ ).dig("AllCops", "Exclude")
30
+
31
+ attr_reader :filepaths
32
+
33
+ def initialize(*filepaths)
34
+ @filepaths = filepaths
35
+ end
36
+
37
+ def all_matches
38
+ filepaths.select do |filepath|
39
+ filepath_proc = FILEPATH_PATTERN_MATCH.curry.call(filepath)
40
+ INCLUDE_PATTERNS.any?(filepath_proc) && EXCLUDE_PATTERNS.none?(filepath_proc)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoxtRubyStyleGuide
4
+ # Returns a list of files that have changed, as detected by `git-diff`
5
+ #
6
+ # TODO: Write tests for this to ensure we're pulling the desired diff files
7
+ # see: https://github.com/ruby-git/ruby-git
8
+ class GitDiff
9
+ require "git"
10
+
11
+ ##
12
+ # List of Git statuses we should test
13
+ # See: https://git-scm.com/docs/git-status#_short_format
14
+ TEST_STATUSES = %w[M A U].freeze
15
+
16
+ attr_reader :base
17
+
18
+ def initialize(base = "master")
19
+ @base = base
20
+ end
21
+
22
+ ##
23
+ # A list of the local file paths of Ruby files with committed changes.
24
+ #
25
+ # Returns Array
26
+ def all
27
+ @all ||= begin
28
+ git.diff(base).name_status.select { |_, stat| TEST_STATUSES.include?(stat) }.keys
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def git
35
+ @git ||= Git.open(".")
36
+ end
37
+ end
38
+ end
@@ -3,62 +3,53 @@
3
3
  require "boxt_ruby_style_guide"
4
4
  require "rubocop"
5
5
 
6
+ require "boxt_ruby_style_guide/git_diff"
7
+ require "boxt_ruby_style_guide/filepath_matcher"
8
+
6
9
  ##
7
10
  # The base branch to compare HEAD with for changes
8
11
  BASE_BRANCH = "develop"
9
12
 
10
13
  ##
11
- # Directories containing Ruby files to lint
12
- RUBY_DIRS = %w[app lib test spec].freeze
14
+ # Name of the master Rubocop lint task to run
15
+ RUBOCOP_TASK_NAME = :"lint:execute_rubocop"
13
16
 
14
17
  ##
15
- # Grep file pattern to make sure we only check files that are Ruby files
16
- # This pattern matches the following:
17
- #
18
- # - Gemfile
19
- # - Rakefile
20
- # - *.gemspec
21
- # - app/**/*.{rb,rake}
22
- # - lib/**/*.{rb,rake}
23
- # - test/**/*.{rb,rake}
24
- # - spec/**/*.{rb,rake}
25
- #
26
- GREP_PATTERN = <<~STRING.delete("\n").delete("\s")
27
- (
28
- (#{RUBY_DIRS.join('|')})\\/.+\\.(rb|rake)
29
- |^
30
- (Gemfile|Rakefile|.+\\.gemspec)
31
- \Z)
32
- STRING
18
+ # Pattern for matching autofix options
19
+ AUTO_REGEX = /\A-a\Z/i.freeze
33
20
 
34
21
  namespace :lint do
35
22
  desc "Runs rubocop against all files with committed changes different from base branch"
36
23
  task :rubocop do
37
- exec(<<~BASH)
38
- RUBOCOP_CHANGED_FILES=`#{diff_file_paths}`
39
- if [ -z "$RUBOCOP_CHANGED_FILES" ]; then
40
- echo "No matching Ruby files changed"
41
- else
42
- bundle exec rubocop $RUBOCOP_CHANGED_FILES
43
- fi
44
- BASH
24
+ Rake::Task[RUBOCOP_TASK_NAME].invoke
25
+ end
26
+
27
+ desc "Runs rubocop against all files using -a (soft autofix) option"
28
+ task :rubocop_a do
29
+ Rake::Task[RUBOCOP_TASK_NAME].invoke("-a")
45
30
  end
46
31
 
47
- private
32
+ desc "Runs rubocop against all files using -A (hard autofix) option"
33
+ task :rubocop_A do
34
+ Rake::Task[RUBOCOP_TASK_NAME].invoke("-A")
35
+ end
48
36
 
49
- # A list of the local file paths of Ruby files with committed changes.
50
- #
51
- # Run a git diff-tree command with the following otions:
52
- # -r recursive
53
- # --name-only Only return the name of the files
54
- # --diff-filter Filter out results that have been deleted on HEAD
55
- #
56
- # Pipe the output from this command through grep to match only Ruby files in the
57
- # desired directories
58
- #
59
- # Returns String
60
- def diff_file_paths
61
- base_branch = ENV.fetch("RUBOCOP_LINT_BASE", BASE_BRANCH)
62
- "git diff -r --name-only --diff-filter=d #{base_branch} | egrep '#{GREP_PATTERN}'"
37
+ task :execute_rubocop, [:auto_flag] do |_t, args|
38
+ if sanitized_file_paths.any?
39
+ # Sanitize args to make sure only a single "a" or "A" is accepted
40
+ auto_flag = AUTO_REGEX.match(args[:auto_flag])
41
+ exec("bundle exec rubocop $RUBOCOP_CHANGED_FILES #{auto_flag}".strip)
42
+ else
43
+ puts "No matching Ruby files changed"
44
+ end
63
45
  end
64
46
  end
47
+
48
+ private
49
+
50
+ # Returns Array
51
+ def sanitized_file_paths
52
+ base = ENV.fetch("RUBOCOP_LINT_BASE", BASE_BRANCH)
53
+ changed_files = BoxtRubyStyleGuide::GitDiff.new(base).all
54
+ BoxtRubyStyleGuide::FilepathMatcher.new(*changed_files).all_matches
55
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxt_ruby_style_guide
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.6.0
4
+ version: 7.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boxt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-12 00:00:00.000000000 Z
11
+ date: 2020-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rubocop
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +114,34 @@ dependencies:
100
114
  - - "~>"
101
115
  - !ruby/object:Gem::Version
102
116
  version: '2.1'
117
+ - !ruby/object:Gem::Dependency
118
+ name: bundler-audit
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.7.0.1
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 0.7.0.1
131
+ - !ruby/object:Gem::Dependency
132
+ name: byebug
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
103
145
  - !ruby/object:Gem::Dependency
104
146
  name: rake
105
147
  requirement: !ruby/object:Gem::Requirement
@@ -170,6 +212,8 @@ files:
170
212
  - VERSION
171
213
  - default.yml
172
214
  - lib/boxt_ruby_style_guide.rb
215
+ - lib/boxt_ruby_style_guide/filepath_matcher.rb
216
+ - lib/boxt_ruby_style_guide/git_diff.rb
173
217
  - lib/boxt_ruby_style_guide/railtie.rb
174
218
  - lib/boxt_ruby_style_guide/version.rb
175
219
  - lib/tasks/lint.rake