Module: Halite::Dependencies

Defined in:
lib/halite/dependencies.rb

Overview

Methods to extract cookbook dependencies from a gem.

Since:

  • 1.0.0

Defined Under Namespace

Classes: Dependency

Class Method Summary collapse

Class Method Details

.clean(dep) ⇒ Object

Raises:

Since:

  • 1.0.0



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

Since:

  • 1.0.0



77
78
79
80
81
82
# 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) ⇒ Object

Since:

  • 1.0.0



102
103
104
105
106
# 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) ⇒ Object

Raises:

Since:

  • 1.0.0



108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Array<Halite::Dependencies::Dependency>

Extract the cookbook dependencies from a gem specification.

Parameters:

  • spec (Gem::Specification)

    Gem to extract from.

  • development (Boolean) (defaults to: false)

    If true, consider development depencies.

Returns:

Since:

  • 1.6.0 Added development parameter.



48
49
50
51
52
53
54
# 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((spec), :metadata)
  deps += clean_and_tag(extract_from_dependencies(spec, development: development), :dependencies)
  deps
end

.extract_from_dependencies(spec, development: false) ⇒ Object

Since:

  • 1.0.0



67
68
69
70
71
72
73
74
75
# 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) ⇒ Object

Since:

  • 1.0.0



61
62
63
64
65
# File 'lib/halite/dependencies.rb', line 61

def self.(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..fetch('halite_dependencies', '').split(/,/)
end

.extract_from_requirements(spec) ⇒ Object

Since:

  • 1.0.0



56
57
58
59
# File 'lib/halite/dependencies.rb', line 56

def self.extract_from_requirements(spec)
  # Simple dependencies in the requirements array.
  spec.requirements
end