class LambdaRubyBundler::CLI::CacheRunner

Runs the executor in Cache Mode.

Constants

MD5_EXTRACT_REGEX

Attributes

cache_dir[R]

Public Class Methods

new(root_path, app_path, cache_dir) click to toggle source

Creates new instance of cache runner.

@param [String] root_path

Path to the root of application (containing Gemfile.lock)

@param [String] app_path

Path (relative to root_path) containing application code

@param [String]

cache_dir Directory containing cached builds
Calls superclass method LambdaRubyBundler::CLI::BaseRunner::new
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 19
def initialize(root_path, app_path, cache_dir)
  super(root_path, app_path)
  @cache_dir = cache_dir
end

Public Instance Methods

run() click to toggle source

Runs the executor, if necessary. Returns hash with two keys:

:application_bundle => path to the application code bundle
:dependency_layer => path to dependency bundle

@return [Hash] Paths to the builds

# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 29
def run
  build_dependencies = !dependencies_builds.key?(dependencies_hash)
  build_application = !application_builds.key?(application_hash)

  if build_application || build_dependencies
    clear_cache
    bundle(build_dependencies)
  end

  paths
end

Private Instance Methods

application_builds() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 79
def application_builds
  @application_builds ||= fetch_builds('build-*.zip')
end
application_hash() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 58
def application_hash
  @application_hash ||= begin
    files = Dir[File.join(root_path, app_path, '**', '*')]
    digest = Digest::MD5.new

    files.each do |file|
      digest << File.read(file) if File.file?(file)
    end

    digest.hexdigest
  end
end
clear_cache() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 43
def clear_cache
  (application_builds.values + dependencies_builds.values).each do |file|
    FileUtils.rm(file)
  end
end
dependencies_builds() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 83
def dependencies_builds
  @dependencies_builds ||= fetch_builds('builddep-*.zip')
end
dependencies_hash() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 71
def dependencies_hash
  @dependencies_hash ||= begin
    path = File.join(root_path, 'Gemfile.lock')
    content = File.read(path)
    Digest::MD5.hexdigest(content)
  end
end
extract_md5(path) click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 94
def extract_md5(path)
  MD5_EXTRACT_REGEX.match(path)&.captures&.at(0)
end
fetch_builds(name_glob) click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 87
def fetch_builds(name_glob)
  Dir[File.join(cache_dir, name_glob)]
    .group_by(&method(:extract_md5))
    .transform_values(&:first)
    .tap { |result| result.delete(nil) }
end
paths() click to toggle source
# File lib/lambda_ruby_bundler/cli/cache_runner.rb, line 49
def paths
  @paths ||= {
    application_bundle:
      File.join(cache_dir, "build-#{application_hash}.zip"),
    dependency_layer:
      File.join(cache_dir, "builddep-#{dependencies_hash}.zip")
  }
end