class Buildr::Packaging::Java::Manifest
Constants
- STANDARD_HEADER
Attributes
The sections of this manifest.
Public Class Methods
Parse the MANIFEST.MF entry of a ZIP (or JAR) file and return a new Manifest.
# File lib/buildr/java/packaging.rb, line 54 def from_zip(file) Zip::File.open(file.to_s) do |zip| return Manifest.parse zip.read('META-INF/MANIFEST.MF') if zip.find_entry('META-INF/MANIFEST.MF') end Manifest.new end
Returns a new Manifest object based on the argument:
-
nil – Empty Manifest.
-
Hash – Manifest with main section using the hash name/value pairs.
-
Array – Manifest with one section from each entry (must be hashes).
-
String – Parse (see Manifest#parse).
-
Proc/Method – New Manifest from result of calling proc/method.
# File lib/buildr/java/packaging.rb, line 87 def initialize(arg = nil) case arg when nil, Hash then @sections = [arg || {}] when Array then @sections = arg when String then @sections = Manifest.parse(arg).sections when Proc, Method then @sections = Manifest.new(arg.call).sections else fail 'Invalid manifest, expecting Hash, Array, file name/task or proc/method.' end # Add Manifest-Version and Created-By, if not specified. STANDARD_HEADER.each do |name, value| sections.first[name] ||= value end end
Parse a string in MANIFEST.MF format and return a new Manifest.
# File lib/buildr/java/packaging.rb, line 34 def parse(str) sections = str.split(SECTION_SEPARATOR).reject { |s| s.strip.empty? } new sections.map { |section| lines = section.split(LINE_SEPARATOR).inject([]) { |merged, line| if line[/^ /] == ' ' merged.last << line[1..-1] else merged << line end merged } lines.map { |line| line.scan(/(.*?):\s*(.*)/).first }. inject({}) { |map, (key, value)| map.merge(key=>value) } } end
Updates the MANIFEST.MF entry of a ZIP (or JAR) file. Reads the MANIFEST.MF, yields to the block with the Manifest object, and writes the modified object back to the file.
# File lib/buildr/java/packaging.rb, line 67 def update_manifest(file) manifest = from_zip(file) result = yield manifest Zip::File.open(file.to_s) do |zip| zip.get_output_stream('META-INF/MANIFEST.MF') do |out| out.write manifest.to_s out.write "\n" end end result end
Public Instance Methods
Iterate over each section and yield to block.
# File lib/buildr/java/packaging.rb, line 113 def each(&block) @sections.each(&block) end
The main (first) section of this manifest.
# File lib/buildr/java/packaging.rb, line 106 def main sections.first end
Convert to MANIFEST.MF format.
# File lib/buildr/java/packaging.rb, line 118 def to_s @sections.map { |section| keys = section.keys keys.unshift('Name') if keys.delete('Name') lines = keys.map { |key| manifest_wrap_at_72("#{key}: #{section[key]}") } lines + [''] }.flatten.join("\n") end