Class: Stove::Cookbook::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/stove/cookbook/metadata.rb

Overview

Borrowed and modified from: https://raw.github.com/chef/chef/11.4.0/lib/chef/cookbook/metadata.rb

Copyright

Copyright 2008-2019 Chef Software, Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Chef::Cookbook::Metadata

Chef::Cookbook::Metadata provides a convenient DSL for declaring metadata about Chef Cookbooks.

Constant Summary collapse

DEFAULT_VERSION =
'>= 0.0.0'.freeze
COMPARISON_FIELDS =
[
  :name, :description, :long_description, :maintainer,
  :maintainer_email, :license, :platforms, :dependencies,
  :recommendations, :suggestions, :conflicting, :providing,
  :replacing, :attributes, :groupings, :recipes, :version
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none') ⇒ Metadata

Returns a new instance of Metadata.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/stove/cookbook/metadata.rb', line 128

def initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none')
  @cookbook         = cookbook
  @name             = cookbook ? cookbook.name : ''
  @long_description = ''
  @source_url       = Stove::Mash.new
  @issues_url       = Stove::Mash.new
  @gems             = []
  @chef_version     = []
  @ohai_version     = []
  @platforms        = Stove::Mash.new
  @dependencies     = Stove::Mash.new
  @recommendations  = Stove::Mash.new
  @suggestions      = Stove::Mash.new
  @conflicting      = Stove::Mash.new
  @providing        = Stove::Mash.new
  @replacing        = Stove::Mash.new
  @attributes       = Stove::Mash.new
  @groupings        = Stove::Mash.new
  @recipes          = Stove::Mash.new

  self.maintainer(maintainer)
  self.maintainer_email(maintainer_email)
  self.license(license)
  self.description('A fabulous new cookbook')
  self.version('0.0.0')

  if cookbook
    @recipes = cookbook.fully_qualified_recipe_names.inject({}) do |r, e|
      e = self.name if e =~ /::default$/
      r[e] = ""
      self.provides e
      r
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



123
124
125
# File 'lib/stove/cookbook/metadata.rb', line 123

def attributes
  @attributes
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



120
121
122
# File 'lib/stove/cookbook/metadata.rb', line 120

def conflicting
  @conflicting
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



114
115
116
# File 'lib/stove/cookbook/metadata.rb', line 114

def cookbook
  @cookbook
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



116
117
118
# File 'lib/stove/cookbook/metadata.rb', line 116

def dependencies
  @dependencies
end

#gemsObject (readonly)

Returns the value of attribute gems.



118
119
120
# File 'lib/stove/cookbook/metadata.rb', line 118

def gems
  @gems
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



124
125
126
# File 'lib/stove/cookbook/metadata.rb', line 124

def groupings
  @groupings
end

#platformsObject (readonly)

Returns the value of attribute platforms.



115
116
117
# File 'lib/stove/cookbook/metadata.rb', line 115

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



121
122
123
# File 'lib/stove/cookbook/metadata.rb', line 121

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



125
126
127
# File 'lib/stove/cookbook/metadata.rb', line 125

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



117
118
119
# File 'lib/stove/cookbook/metadata.rb', line 117

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



122
123
124
# File 'lib/stove/cookbook/metadata.rb', line 122

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



119
120
121
# File 'lib/stove/cookbook/metadata.rb', line 119

def suggestions
  @suggestions
end

#version(arg = UNSET_VALUE) ⇒ Object (readonly)

Returns the value of attribute version.



126
127
128
# File 'lib/stove/cookbook/metadata.rb', line 126

def version
  @version
end

Class Method Details

.def_attribute(field) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/stove/cookbook/metadata.rb', line 31

def def_attribute(field)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(arg = nil)
      set_or_return(:#{field}, arg)
    end
  EOM
end

.def_meta_cookbook(field, instance_variable) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/stove/cookbook/metadata.rb', line 39

def def_meta_cookbook(field, instance_variable)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(thing, *args)
      version = args.first
      @#{instance_variable}[thing] = version || DEFAULT_VERSION
      @#{instance_variable}[thing]
    end
  EOM
end

.def_meta_gems(field, instance_variable) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/stove/cookbook/metadata.rb', line 58

def def_meta_gems(field, instance_variable)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(*args)
      @#{instance_variable} << args unless args.empty?
      @#{instance_variable}
    end
  EOM
end

.def_meta_setter(field, instance_variable) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/stove/cookbook/metadata.rb', line 49

def def_meta_setter(field, instance_variable)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(name, description)
      @#{instance_variable}[name] = description
      @#{instance_variable}
    end
  EOM
end

.def_meta_version(field) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/stove/cookbook/metadata.rb', line 67

def def_meta_version(field)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(*args)
      @#{field} << args unless args.empty?
      @#{field}
    end
  EOM
end

.from_file(path) ⇒ Object



27
28
29
# File 'lib/stove/cookbook/metadata.rb', line 27

def from_file(path)
  new.from_file(path)
end

Instance Method Details

#==(other) ⇒ Object



178
179
180
181
182
# File 'lib/stove/cookbook/metadata.rb', line 178

def ==(other)
  COMPARISON_FIELDS.inject(true) do |equal_so_far, field|
    equal_so_far && other.respond_to?(field) && (other.send(field) == send(field))
  end
end

#from_file(path) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/stove/cookbook/metadata.rb', line 164

def from_file(path)
  path = path.to_s
  path_json = File.join(File.dirname(path), 'metadata.json')

  if File.exist?(path) && File.readable?(path)
    self.instance_eval(IO.read(path), path, 1)
    self
  elsif File.exist?(path_json) && File.readable?(path_json)
    (path_json)
  else
    raise Error::MetadataNotFound.new(path: path)
  end
end

#to_hash(extended_metadata = false) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/stove/cookbook/metadata.rb', line 192

def to_hash( = false)
  hash = {
    'name'             => self.name,
    'version'          => self.version,
    'description'      => self.description,
    'long_description' => self.long_description,
    'maintainer'       => self.maintainer,
    'maintainer_email' => self.maintainer_email,
    'license'          => self.license,
    'platforms'        => self.platforms,
    'dependencies'     => self.dependencies,
    'recommendations'  => self.recommendations,
    'suggestions'      => self.suggestions,
    'conflicting'      => self.conflicting,
    'providing'        => self.providing,
    'replacing'        => self.replacing,
    'attributes'       => self.attributes,
    'groupings'        => self.groupings,
    'recipes'          => self.recipes,
  }

  if 
    hash['source_url']   = self.source_url unless self.source_url.empty?
    hash['issues_url']   = self.issues_url unless self.issues_url.empty?
    hash['gems']         = self.gems unless self.gems.empty?
    hash['chef_version'] = self.chef_version.map(&:sort)
    hash['ohai_version'] = self.ohai_version.map(&:sort)
  end

  return hash
end