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
LOCKFILE_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.lock gems.locked].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

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



30
31
32
# File 'lib/rubocop/server/cache.rb', line 30

def cache_root_path
  @cache_root_path
end

Class Method Details

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



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

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

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



77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/server/cache.rb', line 77

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

  File.expand_path(File.join(cache_root_dir, CacheConfig::SERVER_DIR))
end

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



67
68
69
70
71
# File 'lib/rubocop/server/cache.rb', line 67

def dir
  dir_path.tap do |d|
    d.mkpath unless d.exist?
  end
end

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



73
74
75
# File 'lib/rubocop/server/cache.rb', line 73

def dir_path
  Pathname.new(File.join(cache_path, project_dir_cache_key))
end

.inherit_from_data(yaml) ⇒ 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.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rubocop/server/cache.rb', line 159

def inherit_from_data(yaml)
  return '' unless (inherit_from_paths = yaml['inherit_from'])

  Array(inherit_from_paths).map do |path|
    next if PathUtil.remote_file?(path)

    path = Pathname(path)

    path.exist? ? path.read : ''
  end.join
end

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



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

def lock_path
  dir.join('lock')
end

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



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

def pid
  pid = pid_path(create_dir: false).read.to_i
  pid if pid.positive?
rescue Errno::ENOENT, Errno::EACCES, Errno::EPERM, Errno::EROFS, Errno::ENAMETOOLONG,
       Errno::ENOTDIR
  nil
end

.pid_path(create_dir: true) ⇒ 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.



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

def pid_path(create_dir: true)
  (create_dir ? dir : dir_path).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)


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

def pid_running?
  return false unless (pid = self.pid)

  Process.kill(0, pid) == 1
rescue Errno::ESRCH, Errno::EPERM
  false
end

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



87
88
89
# File 'lib/rubocop/server/cache.rb', line 87

def port_path
  dir.join('port')
end

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

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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/server/cache.rb', line 33

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



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

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

.require_data(yaml) ⇒ 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.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rubocop/server/cache.rb', line 171

def require_data(yaml)
  return '' unless (require_paths = yaml['require'])

  Array(require_paths).map do |path|
    # NOTE: This targets only relative or absolute path specifications.
    # For example, specifications like `require: rubocop-performance`,
    # which can be loaded from `$LOAD_PATH`, are ignored.
    next unless path.start_with?('.', '/')

    # NOTE: `.so` files are not typically specified, so only `.rb` files are targeted.
    path = "#{path}.rb" unless path.end_with?('.rb')
    path = Pathname(path)

    path.exist? ? path.read : ''
  end.join
end

.restart_key(args_config_file_path: 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.

rubocop:disable-next Metrics/AbcSize



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/server/cache.rb', line 51

def restart_key(args_config_file_path: nil)
  lockfile_path = LOCKFILE_NAMES.map do |lockfile_name|
    Pathname(project_dir).join(lockfile_name)
  end.find(&:exist?)
  version_data = lockfile_path&.read || RuboCop::Version::STRING
  config_path = Pathname(args_config_file_path || ConfigFinder.find_config_path(Dir.pwd))
  # NOTE: A missing config file is reported by `RuboCop::CLI` with a friendly message.
  config_data = config_path.exist? ? config_path.read : ''
  yaml = load_erb_templated_yaml(config_data)

  inherit_from_data = inherit_from_data(yaml)
  require_data = require_data(yaml)

  Digest::SHA1.hexdigest(version_data + config_data + inherit_from_data + require_data)
end

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



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

def status_path
  dir.join('status')
end

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



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

def stderr_path
  dir.join('stderr')
end

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



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

def token_path
  dir.join('token')
end

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



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

def version_path
  dir.join('version')
end

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



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

def write_pid_file
  pid_path.write(Process.pid)
  yield
ensure
  remove_dir
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.



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

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.



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

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.



155
156
157
# File 'lib/rubocop/server/cache.rb', line 155

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