Class: Sylvia::RuboCopRails

Inherits:
Object
  • Object
show all
Defined in:
lib/sylvia/rubocop-rails.rb

Constant Summary collapse

CONFIG_FILE =
'.rubocop.yml'
TODO_FILE =
'.rubocop_todo.yml'
GEMFILE =
'Gemfile'

Class Method Summary collapse

Class Method Details

.generate_todoObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sylvia/rubocop-rails.rb', line 16

def self.generate_todo
  if File.exist?(TODO_FILE)
    puts "#{TODO_FILE} already exists. Skipping generation."
    return
  end

  puts 'Generating RuboCop TODO...'
  success = system('bundle exec rubocop --auto-gen-config --no-exclude-limit')

  if success
    puts "RuboCop TODO generated (see #{TODO_FILE})"
  else
    warn 'Failed to generate RuboCop TODO. Check if rubocop is installed.'
  end
rescue StandardError => e
  warn "Error while generating TODO: #{e.message}"
  exit(1)
end

.setupObject



7
8
9
10
11
12
13
14
# File 'lib/sylvia/rubocop-rails.rb', line 7

def self.setup
  setup_config
  setup_gems
rescue StandardError => e
  warn "Error during RuboCop setup: #{e.message}"
  warn e.backtrace.first
  exit(1)
end

.setup_configObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/sylvia/rubocop-rails.rb', line 35

def self.setup_config
  if File.exist?(CONFIG_FILE)
    puts "#{CONFIG_FILE} already exists. Skipping."
    return
  end

  config_content = <<~YAML
require:
  - rubocop-rails
  - rubocop-performance
  - rubocop-rspec

inherit_mode:
  merge:
- Exclude

AllCops:
  TargetRubyVersion: 3.3
  TargetRailsVersion: 8.0
  NewCops: enable
  SuggestExtensions: false
  DisplayCopNames: true
  Exclude:
- "db/schema.rb"
- "db/migrate/**/*"
- "bin/**/*"
- "vendor/**/*"
- "node_modules/**/*"
- "tmp/**/*"
- "config/puma.rb"
- "config/environments/**/*"

# =========================================
# Layout
# =========================================

Layout/LineLength:
  Max: 120
  Exclude:
- "spec/**/*"
- "test/**/*"
- "config/routes.rb"

Layout/HeredocIndentation:
  Enabled: true

# =========================================
# Metrics
# =========================================

Metrics/BlockLength:
  Exclude:
- "spec/**/*"
- "test/**/*"
- "config/routes.rb"
- "db/migrate/**/*"

Metrics/ClassLength:
  Max: 175

Metrics/MethodLength:
  Max: 25

Metrics/ParameterLists:
  Max: 6

Metrics/AbcSize:
  Max: 30

Metrics/CyclomaticComplexity:
  Max: 8

Metrics/PerceivedComplexity:
  Max: 8

# =========================================
# Style Rules
# =========================================

Style/FrozenStringLiteralComment:
  Enabled: false

Style/Documentation:
  Enabled: false

Style/StringLiterals:
  EnforcedStyle: single_quotes

Style/SymbolArray:
  EnforcedStyle: brackets

Style/TrailingCommaInArrayLiteral:
  EnforcedStyleForMultiline: comma

Style/TrailingCommaInHashLiteral:
  EnforcedStyleForMultiline: comma

Style/HashEachMethods:
  Enabled: true

Style/HashTransformKeys:
  Enabled: true

Style/HashTransformValues:
  Enabled: true

# =========================================
# Rails Rules
# =========================================

Rails:
  Enabled: true

Rails/FindBy:
  Enabled: true
Rails/SaveBang:
  Enabled: true
Rails/TimeZone:
  EnforcedStyle: strict
Rails/Date:
  Enabled: true
Rails/PluralizationGrammar:
  Enabled: true
Rails/SkipsModelValidations:
  Enabled: true
Rails/OutputSafety:
  Enabled: true
Rails/HttpPositionalArguments:
  Enabled: true
Rails/NegateInclude:
  Enabled: true
Rails/WhereEquals:
  Enabled: true
Rails/CompactBlank:
  Enabled: true
Rails/Pluck:
  Enabled: false

# =========================================
# Performance Rules
# =========================================

Performance/Count:
  Enabled: true
Performance/Detect:
  Enabled: true
Performance/RedundantSortBlock:
  Enabled: true
Performance/ReverseEach:
  Enabled: true
Performance/Sum:
  Enabled: true
Performance/TimesMap:
  Enabled: true

# =========================================
# RSpec Rules
# =========================================

RSpec:
  Enabled: true
  AllCops:
Exclude:
  - "db/**/*"
  - "config/**/*"
  ExampleLength:
Max: 10
  EmptyExampleGroup:
Enabled: false
  MultipleExpectations:
Max: 10
  YAML

  File.write(CONFIG_FILE, config_content)
  puts "Created #{CONFIG_FILE}"
rescue Errno::EACCES
  warn "Permission denied while creating #{CONFIG_FILE}"
  exit(1)
end

.setup_gemsObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sylvia/rubocop-rails.rb', line 215

def self.setup_gems
  unless File.exist?(GEMFILE)
    puts 'No Gemfile found. Please create one and add these gems manually:'
    puts "  gem 'rubocop', '1.80.0', require: false"
    puts "  gem 'rubocop-performance', '1.26.0', require: false"
    return
  end

  content = File.read(GEMFILE)
  gems_to_add = {
    'rubocop' => '1.80.0',
    'rubocop-performance' => '1.26.0',
    'rubocop-rails' => '2.33.4',
    'rubocop-rspec' => '3.7.0'
  }

  gems_to_add.each do |name, version|
    if content.match?(/gem ['"]#{name}['"]/)
      puts "Gemfile already contains #{name}"
    else
      File.open(GEMFILE, 'a') do |f|
        f.puts %(\ngem "#{name}", "#{version}", require: false)
      end
      puts "Added gem '#{name}' (version #{version}) to Gemfile"
    end
  end

  puts 'Installing specified gems...'
  success = system('bundle install')

  warn 'bundle install failed. Please run it manually.' unless success
rescue Errno::EACCES
  warn 'Permission denied while modifying Gemfile.'
  exit(1)
rescue StandardError => e
  warn "Error while setting up gems: #{e.message}"
  exit(1)
end