forked from rubocop/rubocop
/
config_finder.rb
68 lines (52 loc) · 1.81 KB
/
config_finder.rb
1
2
3
4
5
6
7
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
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
# frozen_string_literal: true
require_relative 'file_finder'
module RuboCop
# This class has methods related to finding configuration path.
# @api private
class ConfigFinder
DOTFILE = '.rubocop.yml'
XDG_CONFIG = 'config.yml'
RUBOCOP_HOME = File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
DEFAULT_FILE = File.join(RUBOCOP_HOME, 'config', 'default.yml')
class << self
include FileFinder
attr_writer :project_root
def find_config_path(target_dir)
find_project_dotfile(target_dir) || find_user_dotfile || find_user_xdg_config ||
DEFAULT_FILE
end
# Returns the path RuboCop inferred as the root of the project. No file
# searches will go past this directory.
def project_root
@project_root ||= find_project_root
end
private
def find_project_root
pwd = Dir.pwd
gems_file = find_last_file_upwards('Gemfile', pwd) || find_last_file_upwards('gems.rb', pwd)
return unless gems_file
File.dirname(gems_file)
end
def find_project_dotfile(target_dir)
find_file_upwards(DOTFILE, target_dir, project_root)
end
def find_user_dotfile
return unless ENV.key?('HOME')
file = File.join(Dir.home, DOTFILE)
return file if File.exist?(file)
end
def find_user_xdg_config
xdg_config_home = expand_path(ENV.fetch('XDG_CONFIG_HOME', '~/.config'))
xdg_config = File.join(xdg_config_home, 'rubocop', XDG_CONFIG)
return xdg_config if File.exist?(xdg_config)
end
def expand_path(path)
File.expand_path(path)
rescue ArgumentError
# Could happen because HOME or ID could not be determined. Fall back on
# using the path literally in that case.
path
end
end
end
end