class Vagrant::Bundler::SolutionFile

Attributes

dependency_list[R]

@return [Array<Gem::Resolver::DependencyRequest>] list of required dependencies

plugin_file[R]

@return [Pathname] path to plugin file

solution_file[R]

@return [Pathname] path to solution file

Public Class Methods

new(plugin_file:, solution_file: nil) click to toggle source

@param [Pathname] plugin_file Path to plugin file @param [Pathname] solution_file Custom path to solution file

# File lib/vagrant/bundler.rb, line 31
def initialize(plugin_file:, solution_file: nil)
  @logger = Log4r::Logger.new("vagrant::bundler::solution_file")
  @plugin_file = Pathname.new(plugin_file.to_s)
  if solution_file
    @solution_file = Pathname.new(solution_file.to_s)
  else
    @solution_file = Pathname.new(@plugin_file.to_s + ".sol")
  end
  @valid = false
  @dependency_list = [].freeze
  @logger.debug("new solution file instance plugin_file=#{plugin_file} " \
    "solution_file=#{solution_file}")
  load
end

Public Instance Methods

delete!() click to toggle source

Delete the solution file

@return [Boolean] true if file was deleted

# File lib/vagrant/bundler.rb, line 76
def delete!
  if !solution_file.exist?
    @logger.debug("solution file does not exist. nothing to delete.")
    return false
  end
  @logger.debug("deleting solution file - #{solution_file}")
  solution_file.delete
  true
end
dependency_list=(dependency_list) click to toggle source

Set the list of dependencies for this solution

@param [Array<Gem::Dependency>] dependency_list List of dependencies for the solution @return [Array<Gem::Resolver::DependencyRequest>]

# File lib/vagrant/bundler.rb, line 50
def dependency_list=(dependency_list)
  Array(dependency_list).each do |d|
    if !d.is_a?(Gem::Dependency)
      raise TypeError, "Expected `Gem::Dependency` but received `#{d.class}`"
    end
  end
  @dependency_list = dependency_list.map do |d|
    Gem::Resolver::DependencyRequest.new(d, nil).freeze
  end.freeze
end
invalidate!() click to toggle source

@return [FalseClass] invalidate this solution file

# File lib/vagrant/bundler.rb, line 67
def invalidate!
  @valid = false
  @logger.debug("manually invalidating solution file #{self}")
  @valid
end
store!() click to toggle source

Store the solution file

# File lib/vagrant/bundler.rb, line 87
def store!
  if !plugin_file.exist?
    @logger.debug("plugin file does not exist, not storing solution")
    return
  end
  if !solution_file.dirname.exist?
    @logger.debug("creating directory for solution file: #{solution_file.dirname}")
    solution_file.dirname.mkpath
  end
  @logger.debug("writing solution file contents to disk")
  solution_file.write({
    dependencies: dependency_list.map { |d|
      [d.dependency.name, d.dependency.requirements_list]
    },
    checksum: plugin_file_checksum,
    vagrant_version: Vagrant::VERSION
  }.to_json)
  @valid = true
end
valid?() click to toggle source

@return [Boolean] contained solution is valid

# File lib/vagrant/bundler.rb, line 62
def valid?
  @valid
end

Protected Instance Methods

load() click to toggle source

Load the solution file for the plugin path provided if it exists. Validate solution is still applicable before injecting dependencies.

# File lib/vagrant/bundler.rb, line 117
def load
  if !plugin_file.exist? || !solution_file.exist?
    @logger.debug("missing file so skipping loading")
    return
  end
  solution = read_solution || return
  return if !valid_solution?(
    checksum: solution[:checksum],
    version: solution[:vagrant_version]
  )
  @logger.debug("loading solution dependency list")
  @dependency_list = Array(solution[:dependencies]).map do |name, requirements|
    gd = Gem::Dependency.new(name, requirements)
    Gem::Resolver::DependencyRequest.new(gd, nil).freeze
  end.freeze
  @logger.debug("solution dependency list: #{dependency_list}")
  @valid = true
end
plugin_file_checksum() click to toggle source

@return [String] checksum of plugin file

# File lib/vagrant/bundler.rb, line 150
def plugin_file_checksum
  digest = Digest::SHA256.new
  digest.file(plugin_file.to_s)
  digest.hexdigest
end
read_solution() click to toggle source

Read contents of solution file and parse

@return [Hash]

# File lib/vagrant/bundler.rb, line 159
def read_solution
  @logger.debug("reading solution file - #{solution_file}")
  begin
    hash = JSON.load(solution_file.read)
    Vagrant::Util::HashWithIndifferentAccess.new(hash)
  rescue => err
    @logger.warn("failed to load solution file, ignoring (error: #{err})")
    nil
  end
end
valid_solution?(checksum:, version:) click to toggle source

Validate the given checksum matches the plugin file checksum

@param [String] checksum Checksum value to validate @return [Boolean]

# File lib/vagrant/bundler.rb, line 141
def valid_solution?(checksum:, version:)
  file_checksum = plugin_file_checksum
  @logger.debug("solution validation check CHECKSUM #{file_checksum} <-> #{checksum}" \
    " VERSION #{Vagrant::VERSION} <-> #{version}")
  plugin_file_checksum == checksum &&
    Vagrant::VERSION == version
end