Class: RuboCop::Service::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/service/installer.rb

Instance Method Summary collapse

Instance Method Details

#find_rubocop_pathObject



39
40
41
42
43
44
45
# File 'lib/rubocop/service/installer.rb', line 39

def find_rubocop_path
  feature_path = $LOAD_PATH.resolve_feature_path("rubocop")
  feature_path.nil? ? nil : feature_path[1]
rescue LoadError => e
  dputs "$LOAD_PATH.resolve_feature_path raised error:", e.message
  nil
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/service/installer.rb', line 8

def run
  puts "Finding RuboCop installation..."
  rubocop_path = find_rubocop_path
  unless rubocop_path
    warn "RuboCop not found! Please install it first."
    exit 1
  end
  dputs "rubocop.rb path:", rubocop_path
  rubocop_dir = Pathname.new File.dirname(rubocop_path)
  dputs "rubocop dir:", rubocop_dir
  if (rubocop_dir / ".rubocop-service_patched").exist?
    warn "Already patched! Use `rubocop-service uninstall` to restore."
    exit 1
  end
  puts "RuboCop found, patching it..."
  patch_libs = Dir.glob("#{__dir__}/patch/**/*.rb")
  patch_libs.each do |libpath|
    path =
      (Pathname.new libpath).relative_path_from(__dir__).sub("patch/", "")
    rubocop_file = rubocop_dir / "rubocop" / path
    rubocop_file.open("a") do |file|
      file.puts "# !!! Patched by rubocop-service !!!"
      file.puts "require 'rubocop/service/patch/#{path.sub(".rb", "")}' if RuboCop::Platform.windows?"
      file.puts "# !!! End of patch !!!"
    end
    dputs "Patched:", rubocop_file
  end
  (rubocop_dir / ".rubocop-service_patched").open("w").close
  puts "Patched #{patch_libs.size} files! Run `rubocop-service uninstall` to restore."
end