class Buildr::Packaging::Java::Manifest

Constants

STANDARD_HEADER

Attributes

sections[R]

The sections of this manifest.

Public Class Methods

from_zip(file) → manifest click to toggle source

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::ZipInputStream::open(file.to_s) do |zip|
    while (entry = zip.get_next_entry)
      if entry.name == 'META-INF/MANIFEST.MF'
        return Manifest.parse zip.read
      end
    end
  end
  Manifest.new
end
new(arg = nil) click to toggle source

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 91
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(str) → manifest click to toggle source

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
update_manifest(file) { |manifest| ... } click to toggle source

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 71
def update_manifest(file)
  manifest = from_zip(file)
  result = yield manifest
  Zip::ZipFile.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

each(&block) click to toggle source

Iterate over each section and yield to block.

# File lib/buildr/java/packaging.rb, line 117
def each(&block)
  @sections.each(&block)
end
main() click to toggle source

The main (first) section of this manifest.

# File lib/buildr/java/packaging.rb, line 110
def main
  sections.first
end
to_s() click to toggle source

Convert to MANIFEST.MF format.

# File lib/buildr/java/packaging.rb, line 122
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