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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

Instance Attribute Details

#sectionsObject (readonly)

The sections of this manifest.



103
104
105
# File 'lib/buildr/java/packaging.rb', line 103

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.



54
55
56
57
58
59
# 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

.parse(str) ⇒ Object

:call-seq:

parse(str) => manifest

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ 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.



67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Method Details

#each(&block) ⇒ Object

Iterate over each section and yield to block.



113
114
115
# File 'lib/buildr/java/packaging.rb', line 113

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

#mainObject

The main (first) section of this manifest.



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

def main
  sections.first
end

#to_sObject

Convert to MANIFEST.MF format.



118
119
120
121
122
123
124
125
# 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