Class: Rpmchange::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/rpmchange/spec.rb

Constant Summary collapse

SECTIONS =
%i{prep build install clean check files changelog}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Spec

Returns a new instance of Spec.



22
23
24
25
# File 'lib/rpmchange/spec.rb', line 22

def initialize(str)
  @str = str
  _load!
end

Instance Attribute Details

#strObject (readonly)

<< self



20
21
22
# File 'lib/rpmchange/spec.rb', line 20

def str
  @str
end

Class Method Details

.diff(a, b, **kwargs) ⇒ Object



12
13
14
# 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) ⇒ Object



4
5
6
# File 'lib/rpmchange/spec.rb', line 4

def load(str)
  Spec.new(str)
end

.loadfile(path) ⇒ Object



8
9
10
# File 'lib/rpmchange/spec.rb', line 8

def loadfile(path)
  Spec.new(Pathname.new(path).read)
end

.without_macroObject



16
17
# File 'lib/rpmchange/spec.rb', line 16

def without_macro
end

Instance Method Details

#append_changelog(name:, email:, message:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rpmchange/spec.rb', line 98

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_line(lines: changelog_lines, new_line: line)}
end

#append_patch(value) ⇒ Object



129
130
131
# File 'lib/rpmchange/spec.rb', line 129

def append_patch(value)
  append_patch_macro append_patch_tag(value)
end

#append_patch_macro(num) ⇒ Object



155
156
157
158
# File 'lib/rpmchange/spec.rb', line 155

def append_patch_macro(num)
  _append_line(lines: prep_lines, new_line: "%patch#{num} -p1", after: _patch_macro_line_regex)
  nil
end

#append_patch_tag(value) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rpmchange/spec.rb', line 140

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_line(lines: preamble_lines, new_line: make_line, after: _patch_tag_line_regex)
  patch_num
end

#append_tag(tag, value) ⇒ Object



125
126
127
# File 'lib/rpmchange/spec.rb', line 125

def append_tag(tag, value)
  _append_line(lines: preamble_lines, new_line: "#{to_tag_name(tag)}: #{value}")
end

#diff(with: nil, **kwargs) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rpmchange/spec.rb', line 51

def diff(with: nil, **kwargs)
  if with
    self.class.diff(dump, with, **kwargs)
  else
    self.class.diff(str, dump, **kwargs)
  end
end

#dumpObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rpmchange/spec.rb', line 31

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

#epochObject



83
84
85
# File 'lib/rpmchange/spec.rb', line 83

def epoch
  tag(:epoch)
end

#epoch=(value) ⇒ Object



87
88
89
# File 'lib/rpmchange/spec.rb', line 87

def epoch=(value)
  set_tag(:epoch, value)
end

#full_versionObject



91
92
93
94
95
96
# File 'lib/rpmchange/spec.rb', line 91

def full_version
  [epoch, [version, release].compact.join('-')]
    .compact
      .join(':')
        .gsub(/%{.*}/, '')
end

#inspectObject



47
48
49
# File 'lib/rpmchange/spec.rb', line 47

def inspect
  "#<#{self.class}: #{name}/#{full_version}>"
end

#nameObject



59
60
61
# File 'lib/rpmchange/spec.rb', line 59

def name
  tag(:name)
end

#name=(value) ⇒ Object



63
64
65
# File 'lib/rpmchange/spec.rb', line 63

def name=(value)
  set_tag(:name, value)
end

#patch_tagsObject



133
134
135
136
137
138
# File 'lib/rpmchange/spec.rb', line 133

def patch_tags
  preamble_lines
    .grep(_patch_tag_line_regex)
    .map(&method(:_patch_tag_line_parse))
    .to_h
end

#preamble_linesObject



164
165
166
# File 'lib/rpmchange/spec.rb', line 164

def preamble_lines
  @preamble_lines ||= []
end

#releaseObject



75
76
77
# File 'lib/rpmchange/spec.rb', line 75

def release
  tag(:release)
end

#release=(value) ⇒ Object



79
80
81
# File 'lib/rpmchange/spec.rb', line 79

def release=(value)
  set_tag(:release, value)
end

#reloadObject



27
28
29
# File 'lib/rpmchange/spec.rb', line 27

def reload
  @sections = nil
end

#sectionsObject



160
161
162
# File 'lib/rpmchange/spec.rb', line 160

def sections
  @sections ||= {}
end

#set_tag(tag, value) ⇒ Object



121
122
123
# File 'lib/rpmchange/spec.rb', line 121

def set_tag(tag, value)
  _replace_line(lines: preamble_lines, new_line: "#{to_tag_name(tag)}: #{value.strip}", match: /#{to_tag_name(tag)}:/)
end

#tag(tag) ⇒ Object



115
116
117
118
119
# File 'lib/rpmchange/spec.rb', line 115

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_sObject



43
44
45
# File 'lib/rpmchange/spec.rb', line 43

def to_s
  dump
end

#to_tag_name(tag) ⇒ Object



111
112
113
# File 'lib/rpmchange/spec.rb', line 111

def to_tag_name(tag)
  tag.to_s.capitalize
end

#versionObject



67
68
69
# File 'lib/rpmchange/spec.rb', line 67

def version
  tag(:version)
end

#version=(value) ⇒ Object



71
72
73
# File 'lib/rpmchange/spec.rb', line 71

def version=(value)
  set_tag(:version, value)
end