Class: RuboCop::Server::Cache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/server/cache.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.

Caches the states of server process.

Constant Summary collapse

GEMFILE_NAMES =

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

%w[Gemfile gems.rb].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_root_pathObject

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.



25
26
27
# File 'lib/rubocop/server/cache.rb', line 25

def cache_root_path
  @cache_root_path
end

Class Method Details

.acquire_lockObject

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.



124
125
126
127
128
129
130
131
132
# File 'lib/rubocop/server/cache.rb', line 124

def acquire_lock
  lock_file = File.open(lock_path, File::CREAT)
  # flock returns 0 if successful, and false if not.
  flock_result = lock_file.flock(File::LOCK_EX | File::LOCK_NB)
  yield flock_result != false
ensure
  lock_file.flock(File::LOCK_UN)
  lock_file.close
end

.cache_pathObject

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.



51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/server/cache.rb', line 51

def cache_path
  cache_root_dir = if cache_root_path
                     File.join(cache_root_path, 'rubocop_cache')
                   else
                     cache_root_dir_from_config
                   end

  File.expand_path(File.join(cache_root_dir, 'server'))
end

.cache_root_dir_from_configObject

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.

rubocop:disable Metrics/MethodLength



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
# File 'lib/rubocop/server/cache.rb', line 62

def cache_root_dir_from_config
  CacheConfig.root_dir do
    # `RuboCop::ConfigStore` has heavy dependencies, this is a lightweight implementation
    # so that only the necessary `CacheRootDirectory` can be obtained.
    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'
    yaml_code = ERB.new(file_contents).result

    require 'yaml'
    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

.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.



45
46
47
48
49
# File 'lib/rubocop/server/cache.rb', line 45

def dir
  Pathname.new(File.join(cache_path, project_dir_cache_key)).tap do |d|
    d.mkpath unless d.exist?
  end
end

.lock_pathObject

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.



102
103
104
# File 'lib/rubocop/server/cache.rb', line 102

def lock_path
  dir.join('lock')
end

.pid_pathObject

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.



98
99
100
# File 'lib/rubocop/server/cache.rb', line 98

def pid_path
  dir.join('pid')
end

.pid_running?Boolean

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.

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/rubocop/server/cache.rb', line 118

def pid_running?
  Process.kill(0, pid_path.read.to_i) == 1
rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES
  false
end

.port_pathObject

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.

rubocop:enable Metrics/MethodLength



90
91
92
# File 'lib/rubocop/server/cache.rb', line 90

def port_path
  dir.join('port')
end

.project_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.

Searches for Gemfile or gems.rb in the current dir or any parent dirs



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/server/cache.rb', line 28

def project_dir
  current_dir = Dir.pwd
  while current_dir != '/'
    return current_dir if GEMFILE_NAMES.any? do |gemfile|
      File.exist?(File.join(current_dir, gemfile))
    end

    current_dir = File.expand_path('..', current_dir)
  end
  # If we can't find a Gemfile, just use the current directory
  Dir.pwd
end

.project_dir_cache_keyObject

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.



41
42
43
# File 'lib/rubocop/server/cache.rb', line 41

def project_dir_cache_key
  @project_dir_cache_key ||= project_dir[1..].tr('/', '+')
end

.status_pathObject

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.



106
107
108
# File 'lib/rubocop/server/cache.rb', line 106

def status_path
  dir.join('status')
end

.stderr_pathObject

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.



114
115
116
# File 'lib/rubocop/server/cache.rb', line 114

def stderr_path
  dir.join('stderr')
end

.token_pathObject

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.



94
95
96
# File 'lib/rubocop/server/cache.rb', line 94

def token_path
  dir.join('token')
end

.version_pathObject

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.



110
111
112
# File 'lib/rubocop/server/cache.rb', line 110

def version_path
  dir.join('version')
end

.write_pid_fileObject

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.



139
140
141
142
143
144
# File 'lib/rubocop/server/cache.rb', line 139

def write_pid_file
  pid_path.write(Process.pid)
  yield
ensure
  dir.rmtree
end

.write_port_and_token_files(port:, token:) ⇒ 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.



134
135
136
137
# File 'lib/rubocop/server/cache.rb', line 134

def write_port_and_token_files(port:, token:)
  port_path.write(port)
  token_path.write(token)
end

.write_status_file(status) ⇒ 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.



146
147
148
# File 'lib/rubocop/server/cache.rb', line 146

def write_status_file(status)
  status_path.write(status)
end

.write_version_file(version) ⇒ 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.



150
151
152
# File 'lib/rubocop/server/cache.rb', line 150

def write_version_file(version)
  version_path.write(version)
end