Class: Lono::Md5

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/lono/md5.rb

Class Method Summary collapse

Class Method Details

.name(path) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/lono/md5.rb', line 32

def name(path)
  relative_path = name_without_md5(path)
  relative_path = relative_path.sub(/\.(\w+)$/,'') # strip extension
  ext = File.directory?(path) ? "zip" : $1
  md5 = sum(path)
  "#{relative_path}-#{md5}.#{ext}"
end

.name_without_md5(path) ⇒ Object



40
41
42
43
# File 'lib/lono/md5.rb', line 40

def name_without_md5(path)
  regexp = %r{.*/output/[\w_-]+/files/}
  path.sub(regexp, '') # relative_path w/o md5
end

.sum(path) ⇒ Object

This checksum within the file name is to mainly make sure that Lambda::Function resources “change” and an update is triggered. There’s another checksum in the upload code that makes sure we don’t upload the code again to speed up things.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lono/md5.rb', line 17

def sum(path)
  files = Dir["#{path}/**/*"]
  files = files.reject { |f| File.directory?(f) }
               .reject { |f| File.symlink?(f) }
  files.sort!
  content = files.map do |f|
    Digest::MD5.file(f).to_s[0..7]
  end.join

  md5 = Digest::MD5.new
  md5.update(content)
  md5.hexdigest.to_s[0..7]
end