-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from zachfeldman/zfeldman/cbruckmayer/impleme…
…nt-file-cache [Feature] Implement file cache
- Loading branch information
Showing
15 changed files
with
499 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
module ERBLint | ||
class Cache | ||
CACHE_DIRECTORY = ".erb-lint-cache" | ||
|
||
def initialize(config, file_loader = nil) | ||
@config = config | ||
@file_loader = file_loader | ||
@hits = [] | ||
@new_results = [] | ||
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_hash| | ||
ERBLint::CachedOffense.new(offense_hash) | ||
end | ||
rescue Errno::ENOENT | ||
return false | ||
end | ||
@hits.push(file_checksum) | ||
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) | ||
|
||
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 | ||
end | ||
|
||
def prune_cache | ||
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) || new_results.include?(cache_file) | ||
|
||
File.delete(File.join(CACHE_DIRECTORY, cache_file)) | ||
end | ||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module ERBLint | ||
# A Cached version of an Offense with only essential information represented as strings | ||
class CachedOffense | ||
attr_reader( | ||
:message, | ||
:line_number, | ||
:severity, | ||
:column, | ||
:simple_name, | ||
:last_line, | ||
:last_column, | ||
:length, | ||
) | ||
|
||
def initialize(params) | ||
params = params.transform_keys(&:to_sym) | ||
|
||
@message = params[:message] | ||
@line_number = params[:line_number] | ||
@severity = params[:severity]&.to_sym | ||
@column = params[:column] | ||
@simple_name = params[:simple_name] | ||
@last_line = params[:last_line] | ||
@last_column = params[:last_column] | ||
@length = params[:length] | ||
end | ||
|
||
def self.new_from_offense(offense) | ||
new( | ||
{ | ||
message: offense.message, | ||
line_number: offense.line_number, | ||
severity: offense.severity, | ||
column: offense.column, | ||
simple_name: offense.simple_name, | ||
last_line: offense.last_line, | ||
last_column: offense.last_column, | ||
length: offense.length, | ||
} | ||
) | ||
end | ||
|
||
def to_h | ||
{ | ||
message: message, | ||
line_number: line_number, | ||
severity: severity, | ||
column: column, | ||
simple_name: simple_name, | ||
last_line: last_line, | ||
last_column: last_column, | ||
length: length, | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.