class Rpmchange::Spec

Constants

SECTIONS

Attributes

str[R]

Public Class Methods

diff(a, b, **kwargs) click to toggle source
# File lib/rpmchange/spec.rb, line 12
def diff(a, b, **kwargs)
  Diffy::Diff.new(a.to_s, b.to_s, {diff: "-U 3"}.merge(kwargs))
end
load(str) click to toggle source
# File lib/rpmchange/spec.rb, line 4
def load(str)
  Spec.new(str)
end
loadfile(path) click to toggle source
# File lib/rpmchange/spec.rb, line 8
def loadfile(path)
  Spec.new(Pathname.new(path).read)
end
new(str) click to toggle source
# File lib/rpmchange/spec.rb, line 19
def initialize(str)
  @str = str
  _load!
end

Public Instance Methods

append(section:, value:, after: nil) click to toggle source
# File lib/rpmchange/spec.rb, line 157
def append(section:, value:, after: nil)
  _append_value(lines: section_lines(section), value: value, after: after)
end
append_changelog(name:, email:, message:) click to toggle source
# File lib/rpmchange/spec.rb, line 95
def append_changelog(name:, email:, message:)
  message_lines = message.to_s.split("\n")
  message_lines[0] = "- #{message_lines[0]}"
  1.upto(message_lines.size - 1).each do |i|
    message_lines[i] = "  #{message_lines[i]}"
  end

  ["* #{Time.now.strftime("%a %b %e %Y")} #{name} <#{email}> - #{full_version}",
    message_lines.join("\n"),
    "",
  ].reverse_each {|line| _prepend_value(lines: changelog_lines, value: line)}
end
append_patch(value) click to toggle source
# File lib/rpmchange/spec.rb, line 126
def append_patch(value)
  append_patch_macro append_patch_tag(value)
end
append_patch_macro(num) click to toggle source
# File lib/rpmchange/spec.rb, line 152
def append_patch_macro(num)
  _append_value(lines: prep_lines, value: "%patch#{num} -p1", after: _patch_macro_line_regex)
  nil
end
append_patch_tag(value) click to toggle source
# File lib/rpmchange/spec.rb, line 137
def append_patch_tag(value)
  patch_num = nil
  make_line = proc do |insert_ind|
    if insert_ind > 0
      last_patch_num = _patch_tag_line_parse(preamble_lines[insert_ind - 1]).first
      patch_num = last_patch_num + 1
    else
      patch_num = 0
    end
    "Patch#{patch_num}: #{value}"
  end
  _append_value(lines: preamble_lines, value: make_line, after: _patch_tag_line_regex)
  patch_num
end
append_tag(tag, value) click to toggle source
# File lib/rpmchange/spec.rb, line 122
def append_tag(tag, value)
  _append_value(lines: preamble_lines, value: "#{to_tag_name(tag)}: #{value}")
end
delete(section:, match:) click to toggle source
# File lib/rpmchange/spec.rb, line 169
def delete(section:, match:)
  _delete_all_lines(lines: section_lines(section), match: match)
end
diff(with: nil, **kwargs) click to toggle source
# File lib/rpmchange/spec.rb, line 48
def diff(with: nil, **kwargs)
  if with
    self.class.diff(dump, with, **kwargs)
  else
    self.class.diff(str, dump, **kwargs)
  end
end
dump() click to toggle source
# File lib/rpmchange/spec.rb, line 28
def dump
  [].tap do |res|
    res.push *preamble_lines
    sections.each do |section, by_params|
      by_params.each {|params, lines|
        res.push ["%#{section}", *params].join(' ')
        res.push *lines
      }
    end
  end.map {|line| line + "\n"}.join
end
epoch() click to toggle source
# File lib/rpmchange/spec.rb, line 80
def epoch
  tag(:epoch)
end
epoch=(value) click to toggle source
# File lib/rpmchange/spec.rb, line 84
def epoch=(value)
  set_tag(:epoch, value)
end
full_version() click to toggle source
# File lib/rpmchange/spec.rb, line 88
def full_version
  [epoch, [version, release].compact.join('-')]
    .compact
      .join(':')
        .gsub(/%{.*}/, '')
end
inspect() click to toggle source
# File lib/rpmchange/spec.rb, line 44
def inspect
  "#<#{self.class}: #{name}/#{full_version}>"
end
name() click to toggle source
# File lib/rpmchange/spec.rb, line 56
def name
  tag(:name)
end
name=(value) click to toggle source
# File lib/rpmchange/spec.rb, line 60
def name=(value)
  set_tag(:name, value)
end
patch_tags() click to toggle source
# File lib/rpmchange/spec.rb, line 130
def patch_tags
  preamble_lines
    .grep(_patch_tag_line_regex)
    .map(&method(:_patch_tag_line_parse))
    .to_h
end
preamble_lines() click to toggle source
# File lib/rpmchange/spec.rb, line 177
def preamble_lines
  @preamble_lines ||= []
end
prepend(section:, value:, before: nil) click to toggle source
# File lib/rpmchange/spec.rb, line 161
def prepend(section:, value:, before: nil)
  _prepend_value(lines: section_lines(section), value: value, before: before)
end
release() click to toggle source
# File lib/rpmchange/spec.rb, line 72
def release
  tag(:release)
end
release=(value) click to toggle source
# File lib/rpmchange/spec.rb, line 76
def release=(value)
  set_tag(:release, value)
end
reload() click to toggle source
# File lib/rpmchange/spec.rb, line 24
def reload
  @sections = nil
end
replace(section:, value:, match:) click to toggle source
# File lib/rpmchange/spec.rb, line 165
def replace(section:, value:, match:)
  _replace_line(lines: section_lines(section), value: value, match: match)
end
sections() click to toggle source
# File lib/rpmchange/spec.rb, line 173
def sections
  @sections ||= {}
end
set_tag(tag, value) click to toggle source
# File lib/rpmchange/spec.rb, line 118
def set_tag(tag, value)
  _replace_line(lines: preamble_lines, value: "#{to_tag_name(tag)}: #{value.strip}", match: /#{to_tag_name(tag)}:/)
end
tag(tag) click to toggle source
# File lib/rpmchange/spec.rb, line 112
def tag(tag)
  match = preamble_lines.find {|line| line.start_with? "#{to_tag_name(tag)}:"}
  res = match.split(':', 2).last if match
  res.strip if res
end
to_s() click to toggle source
# File lib/rpmchange/spec.rb, line 40
def to_s
  dump
end
to_tag_name(tag) click to toggle source
# File lib/rpmchange/spec.rb, line 108
def to_tag_name(tag)
  tag.to_s.capitalize
end
version() click to toggle source
# File lib/rpmchange/spec.rb, line 64
def version
  tag(:version)
end
version=(value) click to toggle source
# File lib/rpmchange/spec.rb, line 68
def version=(value)
  set_tag(:version, value)
end

Protected Instance Methods

_append_value(lines:, value:, after: nil) click to toggle source
# File lib/rpmchange/spec.rb, line 194
def _append_value(lines:, value:, after: nil)
  find_index = proc do |lines|
    if after
      ind = lines.rindex {|line| line.to_s =~ after}
      ind ? ind + 1 : -1
    else
      -1
    end
  end
  _insert_value(lines: lines, value: value, find_index: find_index)
end
_delete_all_lines(lines:, match:) click to toggle source
# File lib/rpmchange/spec.rb, line 235
def _delete_all_lines(lines:, match:)
  lines.delete_if {|line| line.to_s =~ match}
end
_insert_value(lines:, value:, find_index:) click to toggle source
# File lib/rpmchange/spec.rb, line 222
def _insert_value(lines:, value:, find_index:)
  if ind = find_index.call(lines)
    lines.insert(ind, *_value_to_lines(value, ind))
  end
end
_load!() click to toggle source
# File lib/rpmchange/spec.rb, line 248
def _load!
  current_section = preamble_lines
  str.lines.each do |line|
    line.chomp!
    if section = SECTIONS.find {|s| line.start_with? "%#{s}"}
      section_params = line.split(' ')[1..-1]
      section_key = section_params.empty? ? nil : section_params
      sections[section] ||= {}
      sections[section][section_key] ||= []
      current_section = sections[section][section_key]
    else
      current_section << line
    end
  end
end
_patch_macro_line_regex() click to toggle source
# File lib/rpmchange/spec.rb, line 190
def _patch_macro_line_regex
  @_patch_macro_line_regex ||= /^%patch[0-9]*/
end
_patch_tag_line_parse(line) click to toggle source
# File lib/rpmchange/spec.rb, line 239
def _patch_tag_line_parse(line)
  tag, value = line.split(':', 2)
  [tag.split('Patch').last.to_i, value.strip] if tag
end
_patch_tag_line_regex() click to toggle source
# File lib/rpmchange/spec.rb, line 244
def _patch_tag_line_regex
  @_patch_tag_line_regex ||= /^Patch[0-9]*:/
end
_prepend_value(lines:, value:, before: nil) click to toggle source
# File lib/rpmchange/spec.rb, line 206
def _prepend_value(lines:, value:, before: nil)
  find_index = proc do |lines|
    if before
      lines.index {|line| line.to_s =~ before} || 0
    else
      0
    end
  end
  _insert_value(lines: lines, value: value, find_index: find_index)
end
_replace_line(lines:, value:, match:) click to toggle source
# File lib/rpmchange/spec.rb, line 228
def _replace_line(lines:, value:, match:)
  if ind = lines.index {|line| line.to_s =~ match}
    lines.delete_at(ind)
    lines.insert(ind, *_value_to_lines(value, ind))
  end
end
_value_to_lines(value, *args) click to toggle source
# File lib/rpmchange/spec.rb, line 217
def _value_to_lines(value, *args)
  res = (value.respond_to?(:call) ? value.call(*args) : value).to_s
  res.empty? ? [res] : res.split("\n")
end