Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created September 19, 2012 19:52
Show Gist options
  • Save mlafeldt/3751833 to your computer and use it in GitHub Desktop.
Save mlafeldt/3751833 to your computer and use it in GitHub Desktop.
Process metadata of Chef cookbooks
#!/usr/bin/env ruby
require 'chef/cookbook/metadata'
require 'chef/version_class'
# Add increment methods to Chef::Version
class Chef
class Version
def increment_patch
@patch += 1
self
end
def increment_minor
@minor += 1
self
end
def increment_major
@major += 1
self
end
end
end
metadata_file = ARGV.first || 'metadata.rb'
metadata = Chef::Cookbook::Metadata.new
metadata.from_file(metadata_file)
puts "#{metadata.name} #{metadata.version}"
new_version = Chef::Version.new(metadata.version).increment_patch
puts "#{metadata.name} #{new_version}"
#!/usr/bin/env ruby
require 'chef/cookbook/metadata'
metadata_file = ARGV.first || 'metadata.rb'
metadata = Chef::Cookbook::Metadata.new
metadata.from_file(metadata_file)
puts "#{metadata.name} #{metadata.version}"
metadata.dependencies.each do |cookbook, version|
puts "#{cookbook} #{version}"
end
#!/usr/bin/env ruby
class CookbookMetadata
class << self
def from_file(file)
content = File.read(file)
object = new(file)
object.load(content)
end
end
attr_reader :cb_name, :cb_version, :dependencies
def initialize(file)
@file = file
@cb_name = nil
@cb_version = nil
@dependencies = Hash.new
end
def name(value)
@cb_name = value
end
def version(value)
@cb_version = value
end
def depends(cb_name, version=nil)
@dependencies[cb_name] = version || ">= 0.0.0"
end
%w(maintainer maintainer_email license description
long_description recipe supports).each do |method|
define_method(method.to_sym) do |*args|
end
end
def load(content)
instance_eval(content)
self
end
end
metadata_file = ARGV.first || 'metadata.rb'
metadata = CookbookMetadata.from_file(metadata_file)
puts "#{metadata.cb_name} #{metadata.cb_version}"
metadata.dependencies.each do |cookbook, version|
puts "#{cookbook} #{version}"
end
@mlafeldt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment