Class: Sylvia::RuboCop

Inherits:
Object
  • Object
show all
Defined in:
lib/sylvia/rubocop.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.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.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
# File 'lib/sylvia/rubocop.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-performance

    AllCops:
      Include:
        - "**/*.rb"
        - "**/*.rake"
        - "."
      Exclude:
        - "vendor/**/*"
        - "db/schema.rb"
      NewCops: enable

    Performance:
      Enabled: true

    Layout/LineLength:
      Max: 120
      Exclude:
        - "spec/**/*"

    Style/BlockDelimiters:
      Exclude:
        - "spec/**/*"

    Lint/AmbiguousBlockAssociation:
      Exclude:
        - "spec/**/*"

    Metrics/BlockLength:
      Exclude:
        - "spec/**/*"

    Layout/HeredocIndentation:
      Enabled: false

    Metrics/ClassLength:
      Max: 175

    Metrics/MethodLength:
      Max: 25

    Metrics/ParameterLists:
      Max: 20

    Metrics/AbcSize:
      Enabled: false

    Metrics/PerceivedComplexity:
      Enabled: false

    Metrics/CyclomaticComplexity:
      Enabled: false

    Style/HashEachMethods:
      Enabled: true

    Style/HashTransformKeys:
      Enabled: false

    Style/HashTransformValues:
      Enabled: false

    Style/FrozenStringLiteralComment:
      Enabled: false

    Style/Documentation:
      Enabled: false
  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



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
# File 'lib/sylvia/rubocop.rb', line 119

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'
  }

  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