Class: ReaPack::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/reapack/index/gem_version.rb,
lib/reapack/index/named_node.rb,
lib/reapack/index/cdetector.rb,
lib/reapack/index/provides.rb,
lib/reapack/index/metadata.rb,
lib/reapack/index/version.rb,
lib/reapack/index/scanner.rb,
lib/reapack/index/package.rb,
lib/reapack/index/source.rb,
lib/reapack/index/git.rb,
lib/reapack/index.rb

Defined Under Namespace

Classes: CLI, Category, Changelog, ConflictDetector, Git, Link, Metadata, NamedNode, Package, Provides, Scanner, Source, Version

Constant Summary collapse

VERSION =
'1.2.4'
Error =
Class.new RuntimeError
PKG_TYPES =
{
  script: %w{lua eel py},
  extension: %w{ext},
  effect: %w{jsfx},
  data: %w{data},
  theme: %w{theme},
  langpack: %w{reaperlangpack},
  webinterface: %w{www},
  projecttpl: %w{rpp},
  tracktpl: %w{rtracktemplate},
  midinotenames: %w{txt},
  autoitem: %w{reaperautoitem},
}.freeze
FS_ROOT =
File.expand_path('/').freeze
NAME_REGEX =
/\A[^*\\:<>?\/|"[:cntrl:]]+\Z/.freeze
NAME_INVALID =
/\A(:?[\.\x20].*|.+[\.\x20]|CLOCK\$|COM\d|LPT\d)\Z/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Index

Returns a new instance of Index.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/reapack/index.rb', line 92

def initialize(path)
  @amend = false
  @changes = {}
  @changed_nodes = []
  @files = []
  @path = path
  @auto_bump_commit = true

  @cdetector = ConflictDetector.new

  if File.exist? path
    begin
      # noblanks: don't preserve the original white spaces
      # so we always output a neat document
      @doc = File.open(path) {|file| Nokogiri::XML file, &:noblanks }
    rescue Nokogiri::XML::SyntaxError
    end

    unless @doc&.root&.name == 'index'
      raise Error, "'#{path}' is not a ReaPack index file"
    end

    @cdetector.load_xml @doc.root
  else
    @dirty = true
    @is_new = true

    @doc = Nokogiri::XML::Document.new
    @doc.root = Nokogiri::XML::Node.new 'index', @doc
  end

  @doc.root[:version] = 1
  @doc.encoding = 'utf-8'

  @metadata = Metadata.new @doc.root
end

Instance Attribute Details

#amendObject

Returns the value of attribute amend.



56
57
58
# File 'lib/reapack/index.rb', line 56

def amend
  @amend
end

#auto_bump_commitObject

Returns the value of attribute auto_bump_commit.



57
58
59
# File 'lib/reapack/index.rb', line 57

def auto_bump_commit
  @auto_bump_commit
end

#cdetectorObject (readonly)

Returns the value of attribute cdetector.



55
56
57
# File 'lib/reapack/index.rb', line 55

def cdetector
  @cdetector
end

#commitObject

Returns the value of attribute commit.



56
57
58
# File 'lib/reapack/index.rb', line 56

def commit
  @commit
end

#filesObject

Returns the value of attribute files.



56
57
58
# File 'lib/reapack/index.rb', line 56

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



55
56
57
# File 'lib/reapack/index.rb', line 55

def path
  @path
end

#strictObject

Returns the value of attribute strict.



56
57
58
# File 'lib/reapack/index.rb', line 56

def strict
  @strict
end

#timeObject

Returns the value of attribute time.



56
57
58
# File 'lib/reapack/index.rb', line 56

def time
  @time
end

#url_templateObject

Returns the value of attribute url_template.



55
56
57
# File 'lib/reapack/index.rb', line 55

def url_template
  @url_template
end

Class Method Details

.expand(filepath, basedir) ⇒ Object



295
296
297
298
299
# File 'lib/reapack/index.rb', line 295

def self.expand(filepath, basedir)
  expanded = File.expand_path filepath, FS_ROOT + basedir
  expanded[0...FS_ROOT.size] = ''
  expanded
end

.is_type?(input) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/reapack/index.rb', line 60

def is_type?(input)
  PKG_TYPES.has_key? input&.to_sym
end

.parse(contents) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/reapack/index.rb', line 82

def parse(contents)
  mh = MetaHeader.parse contents
  unless mh.has? :changelog
    wp = WordpressChangelog.new mh
    wp.parse contents
  end
  mh
end

.resolve_type(input) ⇒ Object



76
77
78
79
80
# File 'lib/reapack/index.rb', line 76

def resolve_type(input)
  PKG_TYPES
    .find {|name, exts| input.to_sym == name || exts.include?(input.to_s) }
    &.first
end

.type_of(path) ⇒ Object Also known as: is_package?



64
65
66
67
68
69
70
71
72
# File 'lib/reapack/index.rb', line 64

def type_of(path)
  # don't treat files in the root directory as packages
  # because they don't have a category

  if File.dirname(path) != '.' && (ext = File.extname(path)[1..-1])
    ext.downcase!
    PKG_TYPES.find {|_, v| v.include? ext }&.first
  end
end

Instance Method Details

#aboutObject



207
208
209
# File 'lib/reapack/index.rb', line 207

def about
  @metadata.about
end

#about=(content) ⇒ Object



211
212
213
214
215
216
# File 'lib/reapack/index.rb', line 211

def about=(content)
  old = @metadata.about
  @metadata.about = content

  log_change 'modified metadata' if old != @metadata.about
end

#changelogObject



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/reapack/index.rb', line 277

def changelog
  list = []

  @changes.each_pair {|type, data|
    count, plural = data
    list << "#{count} #{count != 1 ? plural : type}"
  }

  list << 'empty index' if @is_new && Category.find_all(@doc.root).empty?

  list.join ', '
end

#clearObject



290
291
292
293
# File 'lib/reapack/index.rb', line 290

def clear
  Category.find_all(@doc.root).each {|cat| cat.remove }
  @cdetector.clear
end


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/reapack/index.rb', line 191

def eval_link(type, input)
  if input.index('-') == 0
    @metadata.remove_link type, input[1..-1]
    log_change "removed #{type} link"
    return
  end

  link = @metadata.push_link type, *Link.split(input)

  if link.is_new?
    log_change "new #{type} link"
  elsif link.modified?
    log_change "modified #{type} link"
  end
end

#last_commitObject



255
256
257
# File 'lib/reapack/index.rb', line 255

def last_commit
  @doc.root[:commit]
end


187
188
189
# File 'lib/reapack/index.rb', line 187

def links(type)
  @metadata.links type
end

#modified?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/reapack/index.rb', line 273

def modified?
  !!@dirty
end

#nameObject



241
242
243
# File 'lib/reapack/index.rb', line 241

def name
  @doc.root[:name].to_s
end

#name=(newName) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/reapack/index.rb', line 245

def name=(newName)
  if !NAME_REGEX.match(newName) || NAME_INVALID.match(newName)
    raise Error, "invalid name '#{newName}'"
  end

  oldName = name
  @doc.root['name'] = newName
  log_change 'modified metadata' if oldName != newName
end

#remove(path) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/reapack/index.rb', line 174

def remove(path)
  cat, pkg = package_for path, false
  return unless pkg

  @cdetector[path].clear

  pkg.remove
  cat.remove if cat.empty?

  bump_commit if @auto_bump_commit
  log_change 'removed package'
end

#scan(path, contents) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/reapack/index.rb', line 129

def scan(path, contents)
  type = self.class.type_of path
  return unless type

  mh = contents.is_a?(MetaHeader) ? contents : self.class.parse(contents)

  if mh[:noindex]
    remove path
    return
  end

  # variables to restore if an error occur
  backups = Hash[[:@doc, :@cdetector].map {|var|
    [var, instance_variable_get(var).clone]
  }]

  cat, pkg = package_for path
  pkg.type = type

  scanner = Scanner.new cat, pkg, mh, self

  begin
    scanner.run @strict
  rescue Error
    backups.each {|var, value| instance_variable_set var, value }
    raise
  end

  log_change 'new category', 'new categories' if cat.is_new?

  if pkg.modified? && !@changed_nodes.include?(pkg.node)
    log_change "#{pkg.is_new? ? 'new' : 'modified'} package"
    @changed_nodes << pkg.node
  end

  pkg.versions.each {|ver|
    if ver.modified? && !@changed_nodes.include?(ver.node)
      log_change "#{ver.is_new? ? 'new' : 'modified'} version"
      @changed_nodes << ver.node
    end
  }

  bump_commit if @auto_bump_commit
end

#versionObject



237
238
239
# File 'lib/reapack/index.rb', line 237

def version
  @doc.root[:version].to_i
end

#write(path) ⇒ Object



259
260
261
262
263
264
# File 'lib/reapack/index.rb', line 259

def write(path)
  sort @doc.root

  FileUtils.mkdir_p File.dirname(path)
  File.binwrite path, @doc.to_xml
end

#write!Object



266
267
268
269
270
271
# File 'lib/reapack/index.rb', line 266

def write!
  write @path

  @is_new = @dirty = false
  @changes.clear
end