Class: RuboCop::CacheConfig Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class represents the cache config of the caching RuboCop runs.

Class Method Summary collapse

Class Method Details

.root_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/cache_config.rb', line 7

def self.root_dir
  root = ENV.fetch('RUBOCOP_CACHE_ROOT', nil)
  root ||= yield
  root ||= if ENV.key?('XDG_CACHE_HOME')
             # Include user ID in the path to make sure the user has write
             # access.
             File.join(ENV.fetch('XDG_CACHE_HOME'), Process.uid.to_s)
           else
             # On FreeBSD, the /home path is a symbolic link to /usr/home
             # and the $HOME environment variable returns the /home path.
             #
             # As $HOME is a built-in environment variable, FreeBSD users
             # always get a warning message.
             #
             # To avoid raising warn log messages on FreeBSD, we retrieve
             # the real path of the home folder.
             File.join(File.realpath(Dir.home), '.cache')
           end

  File.join(root, 'rubocop_cache')
end

.root_dir_from_toplevel_config(cache_root_override = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lightweight cache root computation that reads CacheRootDirectory from the toplevel config file and environment variables without going through the full ConfigStore/ConfigLoader. This method can be used, for example, before loading configuration files. Please note that this method doesn’t take into account any inherit_from dependencies.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cache_config.rb', line 34

def self.root_dir_from_toplevel_config(cache_root_override = nil)
  root_dir do
    next cache_root_override if cache_root_override

    config_path = ConfigFinder.find_config_path(Dir.pwd)
    file_contents = File.read(config_path)

    # Returns early if `CacheRootDirectory` is not used before requiring `erb` or `yaml`.
    next unless file_contents.include?('CacheRootDirectory')

    require 'erb'
    require 'yaml'
    yaml_code = ERB.new(file_contents).result
    config_yaml = YAML.safe_load(yaml_code, permitted_classes: [Regexp, Symbol], aliases: true)

    # For compatibility with Ruby 3.0 or lower.
    if Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0.0')
      config_yaml == false ? nil : config_yaml
    end

    config_yaml&.dig('AllCops', 'CacheRootDirectory')
  end
end