forked from Shopify/erb_lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.rb
102 lines (82 loc) · 2.46 KB
/
cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
module ERBLint
class Cache
CACHE_DIRECTORY = ".erb-lint-cache"
def initialize(config, file_loader = nil, prune = false)
@config = config
@file_loader = file_loader
@hits = []
@new_results = []
@prune = prune
puts "Cache mode is on"
end
def get(filename, file_content)
file_checksum = checksum(filename, file_content)
begin
cache_file_contents_as_offenses = JSON.parse(
File.read(File.join(CACHE_DIRECTORY, file_checksum))
).map do |offense|
ERBLint::CachedOffense.from_json(offense)
end
rescue Errno::ENOENT
return false
end
@hits.push(file_checksum) if prune?
cache_file_contents_as_offenses
end
def set(filename, file_content, offenses_as_json)
file_checksum = checksum(filename, file_content)
@new_results.push(file_checksum) if prune?
FileUtils.mkdir_p(CACHE_DIRECTORY)
File.open(File.join(CACHE_DIRECTORY, file_checksum), "wb") do |f|
f.write(offenses_as_json)
end
end
def close
prune_cache if prune?
end
def prune_cache
puts "Prune cache mode is on - pruned file names will be logged"
if hits.empty?
puts "Cache being created for the first time, skipping prune"
return
end
cache_files = Dir.new(CACHE_DIRECTORY).children
cache_files.each do |cache_file|
next if hits.include?(cache_file)
if new_results.include?(cache_file)
puts "Skipping deletion of new cache result #{cache_file}"
next
end
puts "Cleaning deleted cached file with checksum #{cache_file}"
File.delete(File.join(CACHE_DIRECTORY, cache_file))
end
@hits = []
end
def cache_dir_exists?
File.directory?(CACHE_DIRECTORY)
end
def clear
return unless cache_dir_exists?
puts "Clearing cache by deleting cache directory"
FileUtils.rm_r(CACHE_DIRECTORY)
end
private
attr_reader :config, :hits, :new_results
def checksum(filename, file_content)
digester = Digest::SHA1.new
mode = File.stat(filename).mode
digester.update(
"#{mode}#{config.to_hash}#{ERBLint::VERSION}#{file_content}"
)
digester.hexdigest
rescue Errno::ENOENT
# Spurious files that come and go should not cause a crash, at least not
# here.
"_"
end
def prune?
@prune
end
end
end