Class: RuboCop::Daemon::Cache
- Inherits:
-
Object
- Object
- RuboCop::Daemon::Cache
- Defined in:
- lib/rubocop/daemon/cache.rb
Class Method Summary collapse
- .acquire_lock ⇒ Object
- .dir ⇒ Object
- .lock_path ⇒ Object
- .pid_path ⇒ Object
- .pid_running? ⇒ Boolean
- .port_path ⇒ Object
-
.project_dir ⇒ Object
Searches for Gemfile or gems.rb in the current dir or any parent dirs.
- .project_dir_cache_key ⇒ Object
- .status_path ⇒ Object
- .token_path ⇒ Object
- .write_pid_file ⇒ Object
- .write_port_and_token_files(port:, token:) ⇒ Object
- .write_status_file(status) ⇒ Object
Class Method Details
.acquire_lock ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rubocop/daemon/cache.rb', line 60 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 |
.dir ⇒ Object
27 28 29 30 31 32 |
# File 'lib/rubocop/daemon/cache.rb', line 27 def dir cache_path = File.('~/.cache/rubocop-daemon') Pathname.new(File.join(cache_path, project_dir_cache_key)).tap do |d| d.mkpath unless d.exist? end end |
.lock_path ⇒ Object
46 47 48 |
# File 'lib/rubocop/daemon/cache.rb', line 46 def lock_path dir.join('lock') end |
.pid_path ⇒ Object
42 43 44 |
# File 'lib/rubocop/daemon/cache.rb', line 42 def pid_path dir.join('pid') end |
.pid_running? ⇒ Boolean
54 55 56 57 58 |
# File 'lib/rubocop/daemon/cache.rb', line 54 def pid_running? Process.kill(0, pid_path.read.to_i) == 1 rescue Errno::ESRCH false end |
.port_path ⇒ Object
34 35 36 |
# File 'lib/rubocop/daemon/cache.rb', line 34 def port_path dir.join('port') end |
.project_dir ⇒ Object
Searches for Gemfile or gems.rb in the current dir or any parent dirs
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rubocop/daemon/cache.rb', line 10 def project_dir current_dir = Dir.pwd while current_dir != '/' return current_dir if %w[Gemfile gems.rb].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 ⇒ Object
23 24 25 |
# File 'lib/rubocop/daemon/cache.rb', line 23 def project_dir_cache_key @project_dir_cache_key ||= project_dir[1..-1].tr('/', '+') end |
.status_path ⇒ Object
50 51 52 |
# File 'lib/rubocop/daemon/cache.rb', line 50 def status_path dir.join('status') end |
.token_path ⇒ Object
38 39 40 |
# File 'lib/rubocop/daemon/cache.rb', line 38 def token_path dir.join('token') end |
.write_pid_file ⇒ Object
75 76 77 78 79 80 |
# File 'lib/rubocop/daemon/cache.rb', line 75 def write_pid_file pid_path.write(Process.pid) yield ensure dir.rmtree end |
.write_port_and_token_files(port:, token:) ⇒ Object
70 71 72 73 |
# File 'lib/rubocop/daemon/cache.rb', line 70 def write_port_and_token_files(port:, token:) port_path.write(port) token_path.write(token) end |
.write_status_file(status) ⇒ Object
82 83 84 |
# File 'lib/rubocop/daemon/cache.rb', line 82 def write_status_file(status) status_path.write(status) end |