Class: Buildr::Packaging::Java::Manifest
- 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
-
#sections ⇒ Object
readonly
The sections of this manifest.
Class Method Summary collapse
-
.from_zip(file) ⇒ Object
:call-seq: from_zip(file) => manifest.
-
.parse(str) ⇒ Object
:call-seq: parse(str) => manifest.
-
.update_manifest(file) ⇒ Object
:call-seq: update_manifest(file) { |manifest| … }.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over each section and yield to block.
-
#initialize(arg = nil) ⇒ Manifest
constructor
Returns a new Manifest object based on the argument: * nil – Empty Manifest.
-
#main ⇒ Object
The main (first) section of this manifest.
-
#to_s ⇒ Object
Convert to MANIFEST.MF format.
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/buildr/java/packaging.rb', line 90 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
#sections ⇒ Object (readonly)
The sections of this manifest.
106 107 108 |
# File 'lib/buildr/java/packaging.rb', line 106 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 |
# File 'lib/buildr/java/packaging.rb', line 58 def from_zip(file) Zip::ZipFile.open(file.to_s) do |zip| Manifest.parse zip.read('META-INF/MANIFEST.MF') rescue Manifest.new 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[/^ /] == ' ' 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.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/buildr/java/packaging.rb', line 70 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.
116 117 118 |
# File 'lib/buildr/java/packaging.rb', line 116 def each(&block) @sections.each(&block) end |
#main ⇒ Object
The main (first) section of this manifest.
109 110 111 |
# File 'lib/buildr/java/packaging.rb', line 109 def main sections.first end |
#to_s ⇒ Object
Convert to MANIFEST.MF format.
121 122 123 124 125 126 127 128 |
# File 'lib/buildr/java/packaging.rb', line 121 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 |