class Vagrant::Bundler::SolutionFile
Attributes
@return [Array<Gem::Resolver::DependencyRequest>] list of required dependencies
@return [Pathname] path to plugin file
@return [Pathname] path to solution file
Public Class Methods
@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 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
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
@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 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
@return [Boolean] contained solution is valid
# File lib/vagrant/bundler.rb, line 62 def valid? @valid end
Protected Instance Methods
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
@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 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
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