Class: CookbookBump::Bump

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/cookbook-bump/bump.rb

Constant Summary collapse

TYPE_INDEX =
{ "major" => 0, "minor" => 1, "patch" => 2 }

Instance Method Summary collapse

Instance Method Details

#find_git_repo(cookbook_path, cookbook) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cookbook-bump/bump.rb', line 89

def find_git_repo(cookbook_path, cookbook)
  loader = ::Chef::CookbookLoader.new(cookbook_path)
  cookbook_dir = loader[cookbook].root_dir
  full_path = cookbook_dir.split(File::SEPARATOR)
  (full_path.length - 1).downto(0) do |search_path_index|
    git_config = File.join(full_path[0..search_path_index] + [".git", "config"])
    if File.exist?(git_config)
      return File.join(full_path[0..search_path_index])
    end
  end
  ui.fatal("Unable to find a git repo for this cookbook.")
end

#get_tags(cookbook_path, cookbook) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/cookbook-bump/bump.rb', line 75

def get_tags(cookbook_path, cookbook)
  git_repo = find_git_repo(cookbook_path, cookbook)
  g = Grit::Repo.new(git_repo)
  if g.config["remote.origin.url"].split(File::SEPARATOR).last.scan(cookbook).size > 0
    ui.confirm("I found a repo at #{git_repo} - do you want to tag it?")
  else
    ui.confirm("I didn't find a repo with a name like #{cookbook}.  I did find #{git_repo} - are you sure you want to tag it?")
  end
  g.tags.map { |t| t.name }
end

#get_version(cookbook_path, cookbook) ⇒ Object



70
71
72
73
# File 'lib/cookbook-bump/bump.rb', line 70

def get_version(cookbook_path, cookbook)
  loader = ::Chef::CookbookLoader.new(cookbook_path)
  return loader[cookbook].version
end

#patch(cookbook_path, cookbook, type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cookbook-bump/bump.rb', line 50

def patch(cookbook_path, cookbook, type)
  t = TYPE_INDEX[type]
  current_version = get_version(cookbook_path, cookbook).split(".").map{|i| i.to_i}
  bumped_version = current_version.clone
  bumped_version[t] = bumped_version[t] + 1
   = File.join(cookbook_path, cookbook, "metadata.rb")
  old_version = current_version.join('.')
  new_version = bumped_version.join('.') 
  (old_version, new_version, )
  ui.msg("Bumping #{type} level of the #{cookbook} cookbook from #{old_version} to #{new_version}")
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cookbook-bump/bump.rb', line 14

def run
  
  self.config = Chef::Config.merge!(config)
  if config.has_key?(:cookbook_path)
    cookbook_path = config["cookbook_path"]
  else
    ui.fatal "No default cookbook_path; Specify with -o or fix your knife.rb."
    show_usage
    exit 1
  end
  
  if name_args.size == 0
    show_usage
    exit 0
  end

  unless name_args.size == 2
    ui.fatal "Please specify the cookbook whose version you which to bump, and the type of bump you wish to apply."
    show_usage
    exit 1
  end

  unless TYPE_INDEX.has_key?(name_args.last.downcase)
    ui.fatal "Sorry, '#{name_args.last}' isn't a valid bump type.  Specify one of 'major', 'minor' or 'patch'"
    show_usage
    exit 1
  end
  cookbook = name_args.first
  patch_type = name_args.last
  cookbook_path = Array(config[:cookbook_path]).first

  patch(cookbook_path, cookbook, patch_type)

end

#tagObject



86
87
# File 'lib/cookbook-bump/bump.rb', line 86

def tag
end

#update_metadata(old_version, new_version, metadata_file) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cookbook-bump/bump.rb', line 62

def (old_version, new_version, )
  open_file = File.open(, "r")
  body_of_file = open_file.read
  open_file.close
  body_of_file.gsub!(old_version, new_version)
  File.open(, "w") { |file| file << body_of_file }
end