module Halite::Dependencies

Methods to extract cookbook dependencies from a gem.

@since 1.0.0

Constants

Dependency

Public Class Methods

clean(dep) click to toggle source
# File lib/halite/dependencies.rb, line 84
def self.clean(dep)
  # Convert to an array of strings, remove the spec to be re-added later.
  dep = Array(dep)
  spec = if dep.last.is_a?(::Gem::Specification)
    dep.pop
  end
  dep = Array(dep).map {|obj| obj.is_a?(::Gem::Specification) ? obj : obj.to_s.strip }
  # Unpack single strings like 'foo >= 1.0'
  dep = dep.first.split(/\s+/, 2) if dep.length == 1
  # Default version constraint to match rubygems behavior when sourcing from simple strings
  dep << '>= 0' if dep.length == 1
  raise InvalidDependencyError.new("Chef only supports a single version constraint on each dependency: #{dep}") if dep.length > 2 # ಠ_ಠ
  dep[1] = clean_requirement(dep[1])
  # Re-add the spec
  dep << spec if spec
  dep
end
clean_and_tag(deps, tag) click to toggle source
# File lib/halite/dependencies.rb, line 77
def self.clean_and_tag(deps, tag)
  deps.map do |dep|
    dep = clean(dep)
    Dependency.new(dep[0], dep[1], tag, dep[2])
  end
end
clean_requirement(req) click to toggle source
# File lib/halite/dependencies.rb, line 102
def self.clean_requirement(req)
  req = ::Gem::Requirement.create(req)
  req.requirements[0][1] = clean_version(req.requirements[0][1])
  req.to_s
end
clean_version(ver) click to toggle source
# File lib/halite/dependencies.rb, line 108
def self.clean_version(ver)
  segments = ver.segments.dup
  # Various ways Chef differs from Rubygems.
  # Strip any pre-release tags in the version.
  segments = segments.take_while {|s| s.to_s =~ /^\d+$/ }
  # Must be x or x.y or x.y.z.
  raise InvalidDependencyError.new("Chef only supports two or three version segments: #{ver}") if segments.length < 1 || segments.length > 3
  # If x, convert to x.0 because Chef requires two segments.
  segments << 0 if segments.length == 1
  # Convert 0.0 or 0.0.0 to just 0.
  segments = [0] if segments.all? {|s| s == 0 }
  ::Gem::Version.new(segments.join('.'))
end
extract(spec, development: false) click to toggle source

Extract the cookbook dependencies from a gem specification.

@since 1.6.0 Added development parameter. @param spec [Gem::Specification] Gem to extract from. @param development [Boolean] If true, consider development depencies. @return [Array<Halite::Dependencies::Dependency>]

# File lib/halite/dependencies.rb, line 48
def self.extract(spec, development: false)
  deps = []
  deps += clean_and_tag(extract_from_requirements(spec), :requirements)
  deps += clean_and_tag(extract_from_metadata(spec), :metadata)
  deps += clean_and_tag(extract_from_dependencies(spec, development: development), :dependencies)
  deps
end
extract_from_dependencies(spec, development: false) click to toggle source
# File lib/halite/dependencies.rb, line 67
def self.extract_from_dependencies(spec, development: false)
  # Find any gem dependencies that are cookbooks in disguise.
  spec.dependencies.select do |dep|
    (development || dep.type == :runtime) && Gem.new(dep).is_halite_cookbook?
  end.map do |dep|
    gem = Gem.new(dep)
    [gem.cookbook_name] + dep.requirements_list + [gem.spec]
  end
end
extract_from_metadata(spec) click to toggle source
# File lib/halite/dependencies.rb, line 61
def self.extract_from_metadata(spec)
  # This will only work on Rubygems 2.0 or higher I think, gee thats just too bad.
  # The metadata can only be a single string, so split on comma.
  spec.metadata.fetch('halite_dependencies', '').split(/,/)
end
extract_from_requirements(spec) click to toggle source
# File lib/halite/dependencies.rb, line 56
def self.extract_from_requirements(spec)
  # Simple dependencies in the requirements array.
  spec.requirements
end