Class: CookbookDevelopment::ReleaseTasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/cookbook/development/rake/release_tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ReleaseTasks

Returns a new instance of ReleaseTasks.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cookbook/development/rake/release_tasks.rb', line 11

def initialize
  @project_dir   = Dir.pwd
  @chef_dir      = File.join(project_dir, 'test', '.chef')
  @knife_cfg     = File.join(chef_dir, 'knife.rb')
  @vendor_dir    = File.join(project_dir, 'vendor')
  @cookbooks_dir = File.join(vendor_dir, 'cookbooks')
  @berks_file    = File.join(project_dir, 'Berksfile')

  yield(self) if block_given?
  define
end

Instance Attribute Details

#berks_fileObject (readonly)

Returns the value of attribute berks_file.



9
10
11
# File 'lib/cookbook/development/rake/release_tasks.rb', line 9

def berks_file
  @berks_file
end

#chef_dirObject (readonly)

Returns the value of attribute chef_dir.



5
6
7
# File 'lib/cookbook/development/rake/release_tasks.rb', line 5

def chef_dir
  @chef_dir
end

#cookbooks_dirObject (readonly)

Returns the value of attribute cookbooks_dir.



8
9
10
# File 'lib/cookbook/development/rake/release_tasks.rb', line 8

def cookbooks_dir
  @cookbooks_dir
end

#knife_cfgObject (readonly)

Returns the value of attribute knife_cfg.



6
7
8
# File 'lib/cookbook/development/rake/release_tasks.rb', line 6

def knife_cfg
  @knife_cfg
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



4
5
6
# File 'lib/cookbook/development/rake/release_tasks.rb', line 4

def project_dir
  @project_dir
end

#vendor_dirObject (readonly)

Returns the value of attribute vendor_dir.



7
8
9
# File 'lib/cookbook/development/rake/release_tasks.rb', line 7

def vendor_dir
  @vendor_dir
end

Instance Method Details

#already_tagged?(tag) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/cookbook/development/rake/release_tasks.rb', line 98

def already_tagged?(tag)
  sh('git tag').split(/\n/).include?(tag)
end

#berks_uploadObject



58
59
60
61
# File 'lib/cookbook/development/rake/release_tasks.rb', line 58

def berks_upload
  puts 'Running berks upload...'
  Rake::Task[:upload].invoke
end

#clean?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cookbook/development/rake/release_tasks.rb', line 81

def clean?
  sh_with_code('git diff --exit-code')[1] == 0
end

#committed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/cookbook/development/rake/release_tasks.rb', line 85

def committed?
  sh_with_code('git diff-index --quiet --cached HEAD')[1] == 0
end

#defineObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cookbook/development/rake/release_tasks.rb', line 23

def define
  desc 'Does a berks upload --except :test'
  task :upload do |task|
    Berkshelf::Berksfile.from_file(berks_file).upload(:path => cookbooks_dir, :except => :test)
  end

  desc 'Runs the full test suite and then does a berks upload'
  task :release do
    release_cookbook
  end

end

#git_pullObject



63
64
65
66
67
# File 'lib/cookbook/development/rake/release_tasks.rb', line 63

def git_pull
  cmd = 'git pull --rebase'
  out, code = sh_with_code(cmd)
  raise "Couldn't git pull. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
end

#git_pushObject



69
70
71
72
73
# File 'lib/cookbook/development/rake/release_tasks.rb', line 69

def git_push
  puts 'Pushing git changes...'
  perform_git_push 'origin --tags :'
  puts 'Pushed git commits and tags.'
end

#perform_git_push(options = '') ⇒ Object



75
76
77
78
79
# File 'lib/cookbook/development/rake/release_tasks.rb', line 75

def perform_git_push(options = '')
  cmd = "git push #{options}"
  out, code = sh_with_code(cmd)
  raise "Couldn't git push. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
end

#release_cookbook(skip_test = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cookbook/development/rake/release_tasks.rb', line 36

def release_cookbook(skip_test = false)
  start_time = Time.now
  release_version = version
  release_tag = version_tag(release_version)

  raise "Tag #{release_tag} has already been created." if already_tagged?(release_tag)
  raise 'You have uncommitted changes.' unless clean? && committed?
  raise 'You have unpushed commits.' if unpushed?

  Rake::Task[:test].invoke unless ENV['test'] == 'false'

  tag_version(release_version, release_tag) do
    berks_upload
    Rake::Task['version:bump:patch'].invoke
    git_pull
    git_push
  end

  elapsed = Time.now - start_time
  puts elapsed
end

#sh(cmd, &block) ⇒ Object



93
94
95
96
# File 'lib/cookbook/development/rake/release_tasks.rb', line 93

def sh(cmd, &block)
  out, code = sh_with_code(cmd, &block)
  code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
end

#sh_with_code(cmd, &block) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/cookbook/development/rake/release_tasks.rb', line 136

def sh_with_code(cmd, &block)
  cmd << " 2>&1"
  outbuf = `#{cmd}`
  if $? == 0
    block.call(outbuf) if block
  end
  [outbuf, $?]
end

#tag_version(release, tag) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/cookbook/development/rake/release_tasks.rb', line 126

def tag_version(release, tag)
  sh "git tag -a -m \"Version #{release}\" #{tag}"
  puts "Tagged #{tag}."
  yield if block_given?
rescue Exception => e
  puts "Untagging #{tag} due to error."
  sh_with_code "git tag -d #{tag}"
  raise e
end

#unpushed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/cookbook/development/rake/release_tasks.rb', line 89

def unpushed?
  sh_with_code('git cherry')[0] != ''
end

#versionObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cookbook/development/rake/release_tasks.rb', line 106

def version
  version_file = VersionFile::VERSION_FILE
  alt_version_file = VersionFile::ALT_VERSION_FILE

  if File.exist?(version_file)
    Version.current(version_file)
  elsif File.exist?(alt_version_file)
    Version.current(alt_version_file)
  else
    raise <<-MSG
    The versioning/release process relies on having a VERSION file in the root of
    your cookbook as well as the version attribute in metadata.rb reading
    from said VERSION file. Until https://github.com/opscode/test-kitchen/pull/212
    is resolved we need to put the cookbooks in a place that test-kitchen
    will copy to the VM. That place is in recipes/VERSION. Neither #{version_file}
    nor #{alt_version_file} were found in your cookbook.
    MSG
  end
end

#version_tag(version) ⇒ Object



102
103
104
# File 'lib/cookbook/development/rake/release_tasks.rb', line 102

def version_tag(version)
  "v#{version}"
end