Class: Berkshelf::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/dependency.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(berksfile, name, options = {}) ⇒ Dependency

Returns a new instance of Dependency.

Parameters:

  • berksfile (Berkshelf::Berksfile)

    the berksfile this dependency belongs to

  • name (String)

    the name of dependency

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :constraint (String, Semverse::Constraint)

    version constraint for this dependency

  • :git (String)

    the Git URL to clone

  • :path (String)

    a filepath to the cookbook on your local disk

  • :metadata (String)

    use the metadata at the given pat

  • :group (Symbol, Array)

    the group or groups that the cookbook belongs to

  • :ref (String)

    the commit hash or an alias to a commit hash to clone

  • :branch (String)

    same as ref

  • :tag (String)

    same as tag

  • :locked_version (String)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/berkshelf/dependency.rb', line 57

def initialize(berksfile, name, options = {})
  @options            = options
  @berksfile          = berksfile
  @name               = name
  @metadata           = options[:metadata]
  @location           = Location.init(self, options)

  if options[:locked_version]
    @locked_version = Semverse::Version.coerce(options[:locked_version])
  end

  # The coerce method automatically gives us a default constraint of
  # >= 0.0.0 if the constraint is not set.
  @version_constraint = Semverse::Constraint.coerce(options[:constraint])

  add_group(options[:group]) if options[:group]
  add_group(:default) if groups.empty?
end

Instance Attribute Details

#berksfileBerkshelf::Berksfile (readonly)



21
22
23
# File 'lib/berkshelf/dependency.rb', line 21

def berksfile
  @berksfile
end

#groupsArray<Symbol>

The list of groups this dependency belongs to.

Returns:

  • (Array<Symbol>)


157
158
159
# File 'lib/berkshelf/dependency.rb', line 157

def groups
  @groups ||= []
end

#locationBerkshelf::Location (readonly)

Returns:



27
28
29
# File 'lib/berkshelf/dependency.rb', line 27

def location
  @location
end

#locked_versionSemverse::Version

Returns:

  • (Semverse::Version)


29
30
31
# File 'lib/berkshelf/dependency.rb', line 29

def locked_version
  @locked_version
end

#nameString (readonly)

Returns:



23
24
25
# File 'lib/berkshelf/dependency.rb', line 23

def name
  @name
end

#sourceSource

Returns:



33
34
35
# File 'lib/berkshelf/dependency.rb', line 33

def source
  @source
end

#version_constraintSemverse::Constraint

Returns:

  • (Semverse::Constraint)


31
32
33
# File 'lib/berkshelf/dependency.rb', line 31

def version_constraint
  @version_constraint
end

Class Method Details

.name(dependency) ⇒ String

Returns the name of this cookbook (because it’s the key in hash tables).

Parameters:

  • dependency (Dependency, #to_s)

    the dependency to find the name from

Returns:

  • (String)

    the name of the cookbook



11
12
13
14
15
16
17
# File 'lib/berkshelf/dependency.rb', line 11

def name(dependency)
  if dependency.is_a?(Dependency)
    dependency.name.to_s
  else
    dependency.to_s
  end
end

Instance Method Details

#<=>(other) ⇒ Object



161
162
163
# File 'lib/berkshelf/dependency.rb', line 161

def <=>(other)
  [name, version_constraint] <=> [other.name, other.version_constraint]
end

#add_group(*local_groups) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/berkshelf/dependency.rb', line 99

def add_group(*local_groups)
  local_groups = local_groups.first if local_groups.first.is_a?(Array)

  local_groups.each do |group|
    group = group.to_sym
    groups << group unless groups.include?(group)
  end
end

#cached_cookbookCachedCookbook?

Attempt to load the cached_cookbook for this dependency. For SCM/path locations, this method delegates to BaseLocation#cached_cookbook. For generic dependencies, this method tries attemps to load a matching cookbook from the CookbookStore.

Returns:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/berkshelf/dependency.rb', line 122

def cached_cookbook
  return @cached_cookbook if @cached_cookbook

  @cached_cookbook =
    if location
      cookbook = location.cached_cookbook

      # If we have a cached cookbook, tighten our constraints
      if cookbook
        self.locked_version     = cookbook.version
        self.version_constraint = cookbook.version
      end

      cookbook
    else
      if locked_version
        CookbookStore.instance.cookbook(name, locked_version)
      else
        CookbookStore.instance.satisfy(name, version_constraint)
      end
    end

  @cached_cookbook
end

#has_group?(group) ⇒ Boolean

Returns true if this dependency has the given group.

Returns:

  • (Boolean)


150
151
152
# File 'lib/berkshelf/dependency.rb', line 150

def has_group?(group)
  groups.include?(group.to_sym)
end

#inspectObject



169
170
171
172
173
174
175
176
# File 'lib/berkshelf/dependency.rb', line 169

def inspect
  "#<Berkshelf::Dependency: " << [
    "#{name} (#{version_constraint})",
    "locked_version: #{locked_version.inspect}",
    "groups: #{groups}",
    "location: #{location || "default"}>",
  ].join(", ")
end

#installed?Boolean

Determine if this dependency is installed. A dependency is “installed” if the associated CachedCookbook exists on disk.

Returns:

  • (Boolean)


112
113
114
# File 'lib/berkshelf/dependency.rb', line 112

def installed?
  !cached_cookbook.nil?
end

#metadata?Boolean

Return true if this is a metadata location.

Returns:

  • (Boolean)


79
80
81
# File 'lib/berkshelf/dependency.rb', line 79

def metadata?
  !!@metadata
end

#to_lockObject



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/berkshelf/dependency.rb', line 178

def to_lock
  out =
    if location || version_constraint.to_s == ">= 0.0.0"
      "  #{name}\n"
    else
      "  #{name} (#{version_constraint})\n"
    end

  out << location.to_lock if location
  out
end

#to_sObject



165
166
167
# File 'lib/berkshelf/dependency.rb', line 165

def to_s
  "#{name} (#{locked_version || version_constraint})"
end