Class: RuboCop::Server::Cache Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/server/cache.rb |
Overview
Caches the states of server process.
Constant Summary
-
GEMFILE_NAMES =
# File 'lib/rubocop/server/cache.rb', line 21%w[Gemfile gems.rb].freeze
Class Attribute Summary
- .cache_root_path rw Internal use only
- .pid_running? ⇒ Boolean readonly Internal use only
Class Method Summary
- .acquire_lock Internal use only
- .cache_path Internal use only
- .cache_root_dir_from_config Internal use only
- .dir Internal use only
- .lock_path Internal use only
- .pid_path Internal use only
- .port_path Internal use only
-
.project_dir
Internal use only
Searches for Gemfile or gems.rb in the current dir or any parent dirs.
- .project_dir_cache_key Internal use only
- .status_path Internal use only
- .stderr_path Internal use only
- .token_path Internal use only
- .version_path Internal use only
- .write_pid_file Internal use only
- .write_port_and_token_files(port:, token:) Internal use only
- .write_status_file(status) Internal use only
- .write_version_file(version) Internal use only
Class Attribute Details
.cache_root_path (rw)
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 24
attr_accessor :cache_root_path
.pid_running? ⇒ Boolean
(readonly)
# File 'lib/rubocop/server/cache.rb', line 117
def pid_running? Process.kill(0, pid_path.read.to_i) == 1 rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES, Errno::EROFS false end
Class Method Details
.acquire_lock
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 123
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_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 50
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. (File.join(cache_root_dir, 'server')) end
.cache_root_dir_from_config
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 61
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
.dir
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 44
def dir Pathname.new(File.join(cache_path, project_dir_cache_key)).tap do |d| d.mkpath unless d.exist? end end
.lock_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 101
def lock_path dir.join('lock') end
.pid_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 97
def pid_path dir.join('pid') end
.port_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 89
def port_path dir.join('port') end
.project_dir
Searches for Gemfile or gems.rb in the current dir or any parent dirs
# File 'lib/rubocop/server/cache.rb', line 27
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. ('..', current_dir) end # If we can't find a Gemfile, just use the current directory Dir.pwd end
.project_dir_cache_key
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 40
def project_dir_cache_key @project_dir_cache_key ||= project_dir[1..].tr('/', '+') end
.status_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 105
def status_path dir.join('status') end
.stderr_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 113
def stderr_path dir.join('stderr') end
.token_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 93
def token_path dir.join('token') end
.version_path
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 109
def version_path dir.join('version') end
.write_pid_file
[ GitHub ].write_port_and_token_files(port:, token:)
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 133
def write_port_and_token_files(port:, token:) port_path.write(port) token_path.write(token) end
.write_status_file(status)
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 145
def write_status_file(status) status_path.write(status) end
.write_version_file(version)
[ GitHub ]# File 'lib/rubocop/server/cache.rb', line 149
def write_version_file(version) version_path.write(version) end