Class: Stove::Action::Changelog

Inherits:
Base
  • Object
show all
Defined in:
lib/stove/actions/changelog.rb

Instance Attribute Summary

Attributes inherited from Base

#cookbook, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Mixin::Optionable

extended, included

Methods included from Mixin::Validatable

#validate

Constructor Details

This class inherits a constructor from Stove::Action::Base

Instance Method Details

#default_changesetObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stove/actions/changelog.rb', line 56

def default_changeset
  return @default_changeset if @default_changeset

  header = "v#{cookbook.version} (#{Time.now.to_date})"

  contents = []
  contents << header
  contents << '-'*header.length
  contents << cookbook.changeset || 'Enter CHANGELOG entries here'
  contents << ''

  @default_changeset = contents.join("\n")
  @default_changeset
end

#prompt_for_changesetObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stove/actions/changelog.rb', line 40

def prompt_for_changeset
  tempfile = Tempfile.new(["#{cookbook.name}-changeset-#{Time.now}", '.md'])
  tempfile.write(default_changeset)
  tempfile.rewind

  # Shell out to the default editor
  system %Q|$EDITOR "#{tempfile.path}"|

  # Save the resulting changes back to the cookbook object
  cookbook.changeset = File.read(tempfile.path).strip

  # Cleanup
  tempfile.close
  tempfile.unlink
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stove/actions/changelog.rb', line 19

def run
  log.info('Generating new Changelog')
  log.debug("Generated changeset:\n#{default_changeset}")

  # Open a file prompt for changes
  prompt_for_changeset

  log.debug("New changeset:\n#{cookbook.changeset}")

  # Write the new changelog to disk
  path     = File.join(cookbook.path, 'CHANGELOG.md')
  contents = File.readlines(path)
  index    = contents.find_index { |line| line =~ /^(--)+/ }

  log.debug("Writing changelog at `#{path}', index #{index}")

  contents.insert(index - 2, "\n" + cookbook.changeset + "\n\n")

  File.open(path, 'w') { |file| file.write(contents.join('')) }
end