Class: Fastlane::Actions::UpdateRubocopAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb

Class Method Summary collapse

Class Method Details

.add_failing_cop(line) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 152

def add_failing_cop(line)
  matches = %r{^.+: [A-Z]: (\w+/\w+):}.match line
  return unless matches

  @failing_cops ||= Set.new
  @failing_cops << matches[1]
end

.adjust_namespace(line) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 134

def adjust_namespace(line)
  matches = %r{^(.*): (\w+)/(\w+) has the wrong namespace - should be (\w+)}.match line
  UI.user_error! "Failed to scan #{line.inspect}" unless matches

  path, old_namespace, cop, new_namespace = *matches[1..4]

  UI.important "#{path}: #{old_namespace}/#{cop} -> #{new_namespace}/#{cop}"

  PatternPatch::Patch.new(
    regexp: %r{#{old_namespace}/#{cop}},
    text: "#{new_namespace}/#{cop}",
    mode: :replace,
    global: true
  ).apply path

  true
end

.adjust_target_ruby_versionObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 181

def adjust_target_ruby_version
  rubocop_config = YAML.load_file ".rubocop.yml"
  versions = rubocop_config.map { |k, v| v['TargetRubyVersion'] }.compact.map(&:to_f).uniq
  return unless versions.any? { |v| v < 2.1 }

  version = [versions.max, 2.1].max

  UI.important "Updating TargetRubyVersion to #{version}"

  PatternPatch::Patch.new(
    regexp: /(TargetRubyVersion\s*:\s*)\d\.\d(.*)$/,
    text: "\\1#{version}\\2",
    mode: :replace,
    global: true
  ).apply ".rubocop.yml"
end

.authorsObject



41
42
43
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 41

def authors
  ["Jimmy Dee"]
end

.descriptionObject



34
35
36
37
38
39
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 34

def description
  "Updates rubocop to the latest version. Pins your gemspec to a new version of rubocop if " \
  "necessary. Runs rubocop -a to auto-correct many offenses. Adjusts TargetRubyVersion. " \
  "Automatically corrects namespace changes. Disables any remaining failing Cops in your " \
  ".rubocop.yml with a TODO note. Run from the command line with no arguments."
end

.disable_failing_copsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 160

def disable_failing_cops
  return unless @failing_cops

  UI.message ""
  UI.important "Temporarily disabling the following cops in .rubocop.yml"
  @failing_cops.each do |cop|
    UI.important " #{cop}"
  end

  insertion = "# --- update_rubocop ---\n"
  insertion << "# TODO: Review these failing cops, adjust code and re-enable as necessary.\n\n"
  insertion << @failing_cops.map { |c| "#{c}:\n  Enabled: false\n" }.join("\n")
  insertion << "# --- update_rubocop ---\n\n"

  PatternPatch::Patch.new(
    regexp: /\A/,
    text: insertion,
    mode: :prepend
  ).apply ".rubocop.yml"
end

.example_codeObject



45
46
47
48
49
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 45

def example_code
  [
    "bundle exec fastlane run update_rubocop"
  ]
end

.gemspecObject



65
66
67
68
69
70
71
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 65

def gemspec
  return @gemspec if @gemspec
  # rubocop: disable Security/Eval
  @gemspec = eval(File.read(gemspec_path))
  # rubocop: enable Security/Eval
  @gemspec
end

.gemspec_pathObject



61
62
63
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 61

def gemspec_path
  Dir[File.expand_path("*.gemspec")].first
end

.latest_rubocop_gemspecObject



51
52
53
54
55
56
57
58
59
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 51

def latest_rubocop_gemspec
  spec_tuple = Gem::SpecFetcher.fetcher.detect :latest do |name_tuple|
    name_tuple.name == 'rubocop'
  end.first

  name_tuple = spec_tuple.first
  source = spec_tuple.second
  source.fetch_spec name_tuple
end

.rubocop_dependencyObject



73
74
75
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 73

def rubocop_dependency
  gemspec.development_dependencies.find { |d| d.name == "rubocop" }
end

.rubocop_requirement_from_repoObject



77
78
79
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 77

def rubocop_requirement_from_repo
  rubocop_dependency.requirement
end

.run(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 14

def run(params)
  unless Gem::Version.new(Fastlane::VERSION) >= Gem::Version.new("2.69.0")
    UI.important "This action requires fastlane >= 2.69.0."
    return false
  end

  spec = latest_rubocop_gemspec
  UI.message "Latest: #{spec.name} #{spec.version}"
  requirement = rubocop_requirement_from_repo
  UI.message "requirement from gemspec: #{requirement}"
  unless requirement.satisfied_by? spec.version
    UI.important "updating gemspec requirement to #{spec.version}"
    update_rubocop_requirement spec.version
  end

  sh "bundle update --quiet rubocop"

  run_rubocop
end

.run_rubocopObject



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
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 89

def run_rubocop
  adjust_target_ruby_version

  UI.message "1. Running rubocop --auto-correct. This may fail."
  sh "bundle exec rubocop --auto-correct", print_command_output: false do |status, output|
    output.split("\n").each do |line|
      adjust_namespace(line) if line =~ /has the wrong namespace/
    end

    if status.success?
      UI.success "Done ✅"
      return
    end
  end

  UI.message "2. Running rubocop --display-cop-names to disable failing cops. This may fail."
  sh "bundle exec rubocop --display-cop-names", print_command_output: false do |status, output, command|
    if status.success?
      UI.success "Done ✅"
      return
    end

    output.split("\n").each do |line|
      add_failing_cop line
    end

    disable_failing_cops
  end

  UI.message "3. Running rubocop --auto-correct to verify changes. This should pass."
  sh "bundle exec rubocop --auto-correct", print_command_output: false do |status, output, command|
    if status.success?
      UI.success "Done ✅"
      return
    end

    UI.important "rubocop still detects offenses. These have to be fixed manually."
    output.split("\n").each do |line|
      UI.message line
    end
  end

  false
end

.update_rubocop_requirement(version) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/maintenance/actions/update_rubocop_action.rb', line 81

def update_rubocop_requirement(version)
  PatternPatch::Patch.new(
    regexp: /(\.add_development_dependency.*rubocop['")\]}]).*$/,
    text: %(\\1, '#{version}'),
    mode: :replace
  ).apply gemspec_path
end