class Revision::Info
Constants
- CHANGELOG_END
- CHANGELOG_END_TAG
- CHANGELOG_START
- CHANGELOG_START_TAG
- DEFAULT_COMMENT_PREFIX
- DEFAULT_REGEX
Attributes
comment_prefix[RW]
major[RW]
minor[RW]
patch[RW]
regex[RW]
src[RW]
Public Class Methods
new(file, regex: nil, comment_prefix: nil, embed_changelog: true)
click to toggle source
# File lib/revision/info.rb, line 18 def initialize(file, regex: nil, comment_prefix: nil, embed_changelog: true) @src=file @regex = regex.nil? ? DEFAULT_REGEX : /#{regex}/ @comment_prefix = comment_prefix || DEFAULT_COMMENT_PREFIX @embed_changelog = embed_changelog matched = false File.open(@src).each_line do |line| if line =~ @regex @major = Regexp.last_match[:major].to_i @minor = Regexp.last_match[:minor].to_i @patch = Regexp.last_match[:patch].to_i matched = true break end end raise "Failed to match against #{@regex}" unless matched end
Public Instance Methods
changelog() { |strip_comment_prefix(chomp)| ... }
click to toggle source
# File lib/revision/info.rb, line 95 def changelog in_changelog = false File.open(@src).each_line do |line| if in_changelog break if line =~ CHANGELOG_END yield strip_comment_prefix(line.chomp) else in_changelog = line =~ CHANGELOG_START end end end
format_changelog_entry(entry_lines)
click to toggle source
Prefixes the entry with an empty line, then prefixes each line with comment chars and converts the line entries to a single string
# File lib/revision/info.rb, line 123 def format_changelog_entry(entry_lines) entry_lines.unshift('').map { |line| "#{@comment_prefix} #{line}".rstrip()}.join("\n") end
get_changelog_entry()
click to toggle source
# File lib/revision/info.rb, line 127 def get_changelog_entry entry_lines = [] entry_lines << "Version #{self} (#{Time.now.strftime("%d %b %Y")})" puts('Changelog entry (one item per line / empty line to end)') puts('N.B. Git commit entry will use revision ID and first line of entry') while line = $stdin.readline.strip break if line.length == 0 entry_lines << "- #{line}" end entry_lines end
last_changelog_entry()
click to toggle source
# File lib/revision/info.rb, line 107 def last_changelog_entry in_entry = false lines = [] changelog do |line| if line.length > 0 in_entry = true lines << line else break if in_entry end end lines end
major_increment!()
click to toggle source
# File lib/revision/info.rb, line 47 def major_increment! @major += 1 @minor = 0 @patch = 0 self end
minor_increment!()
click to toggle source
# File lib/revision/info.rb, line 41 def minor_increment! @minor += 1 @patch = 0 self end
new_changelog_placeholder()
click to toggle source
# File lib/revision/info.rb, line 54 def new_changelog_placeholder placeholder = [@comment_prefix,CHANGELOG_START_TAG,CHANGELOG_END_TAG].join("\n#{@comment_prefix} ") # Handle C block comments as a special case for legacy project support # Could generalise the comment definition, but feels like unnecessary complexity... if @src =~ /\.(c|cpp|h|hpp)$/ && !@comment_prefix =~ /#{Regexp.escape('//')}/ placeholder = "/**\n${placeholder}\n*/" end placeholder + "\n" end
patch_increment!()
click to toggle source
# File lib/revision/info.rb, line 36 def patch_increment! @patch += 1 self end
strip_comment_prefix(line)
click to toggle source
# File lib/revision/info.rb, line 91 def strip_comment_prefix(line) line.gsub(/^\s*#{Regexp.escape(@comment_prefix)}\s?/,'') end
to_s()
click to toggle source
# File lib/revision/info.rb, line 87 def to_s "#{@major}.#{@minor}.#{@patch}" end
write(output_file_name)
click to toggle source
# File lib/revision/info.rb, line 64 def write(output_file_name) ref_info = self.class.new(@src, regex: @regex) raise 'No revision identifiers incremented' if ref_info.to_s == self.to_s text = File.read(@src) text.gsub!(@regex) { |match| "#{$~[:prefix]}#{@major}#{$~[:sep1]}#{@minor}#{$~[:sep2]}#{@patch}#{$~[:postfix]}" } if @embed_changelog entry = get_changelog_entry #Insert start/end tags if not present text += new_changelog_placeholder unless text.match(CHANGELOG_START) text.gsub!(CHANGELOG_START) { |match| [match, format_changelog_entry(entry)].join("\n") } end File.open(output_file_name, 'w') { |f| f.write(text) } end
write!()
click to toggle source
# File lib/revision/info.rb, line 83 def write! write(@src) end