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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/buildr/java/packaging.rb', line 95 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.
111 112 113 |
# File 'lib/buildr/java/packaging.rb', line 111 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 64 65 66 67 |
# File 'lib/buildr/java/packaging.rb', line 58 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 |
.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.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/buildr/java/packaging.rb', line 75 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.
121 122 123 |
# File 'lib/buildr/java/packaging.rb', line 121 def each(&block) @sections.each(&block) end |
#main ⇒ Object
The main (first) section of this manifest.
114 115 116 |
# File 'lib/buildr/java/packaging.rb', line 114 def main sections.first end |
#to_s ⇒ Object
Convert to MANIFEST.MF format.
126 127 128 129 130 131 132 133 |
# File 'lib/buildr/java/packaging.rb', line 126 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 |