Class: Buildr::Packaging::Java::Manifest

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/buildr/java/packaging.rb

Constant Summary collapse

STANDARD_HEADER =
{ 'Manifest-Version'=>'1.0', 'Created-By'=>'Buildr' }
LINE_SEPARATOR =

:nodoc:

/\r\n|\n|\r[^\n]/
SECTION_SEPARATOR =

:nodoc:

/(#{LINE_SEPARATOR}){2}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ Manifest

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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

Instance Attribute Details

#sectionsObject (readonly)

The sections of this manifest.



107
108
109
# File 'lib/buildr/java/packaging.rb', line 107

def sections
  @sections
end

Class Method Details

.from_zip(file) ⇒ Object

:call-seq:

from_zip(file) => manifest

Parse the MANIFEST.MF entry of a ZIP (or JAR) file and return a new Manifest.



58
59
60
61
62
63
# File 'lib/buildr/java/packaging.rb', line 58

def from_zip(file)
  Zip::ZipFile.open(file.to_s) do |zip|
    return Manifest.new unless zip.get_entry('META-INF/MANIFEST.MF')
    Manifest.parse zip.read('META-INF/MANIFEST.MF')
  end
end

.parse(str) ⇒ Object

:call-seq:

parse(str) => manifest

Parse a string in MANIFEST.MF format and return a new Manifest.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/buildr/java/packaging.rb', line 38

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[0] == 32
        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) ⇒ Object

:call-seq:

update_manifest(file) { |manifest| ... }

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.



71
72
73
74
75
76
77
78
79
80
81
# 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

Instance Method Details

#each(&block) ⇒ Object

Iterate over each section and yield to block.



117
118
119
# File 'lib/buildr/java/packaging.rb', line 117

def each(&block)
  @sections.each(&block)
end

#mainObject

The main (first) section of this manifest.



110
111
112
# File 'lib/buildr/java/packaging.rb', line 110

def main
  sections.first
end

#to_sObject

Convert to MANIFEST.MF format.



122
123
124
125
126
127
128
129
# 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