Class: JavaTools::Jar

Inherits:
Object
  • Object
show all
Defined in:
lib/java_tools/jar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, files = nil, base_dir = nil) ⇒ Jar

Returns a new instance of Jar.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/java_tools/jar.rb', line 29

def initialize( output, files = nil, base_dir = nil )
  @output      = output
  @base_dir    = base_dir
  @entries     = { } # archive_path => IO
  @verbose     = false
  @compression = 1
  
  self.manifest = { }
  
  add_files(files) unless files.nil? || files.empty?
end

Instance Attribute Details

#base_dirObject

The path to the directory which should be considered the root of the archive.

If you set base_dir to “build” and add the file “build/Main.class”, that file will be put in the archive as “Main.class”.



17
18
19
# File 'lib/java_tools/jar.rb', line 17

def base_dir
  @base_dir
end

#compressionObject

The ZIP compression rate from 0 (no compression) to 9 (maximal compression)



20
21
22
# File 'lib/java_tools/jar.rb', line 20

def compression
  @compression
end

#outputObject (readonly)

The path to the JAR file that will be generated



26
27
28
# File 'lib/java_tools/jar.rb', line 26

def output
  @output
end

#verboseObject

Whether or not to print the equivalent command string to the output (see #execute)



23
24
25
# File 'lib/java_tools/jar.rb', line 23

def verbose
  @verbose
end

Instance Method Details

#add_blob(str, archive_path) ⇒ Object

Adds a string to the archive at archive_path.



119
120
121
# File 'lib/java_tools/jar.rb', line 119

def add_blob( str, archive_path )
  @entries[archive_path] = StringIO.new(str)
end

#add_file(file_path, archive_path = nil) ⇒ Object

Adds the file at file_path to the archive and put it at archive_path (the same as file_path by default) inside the archive.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/java_tools/jar.rb', line 98

def add_file( file_path, archive_path = nil )
  archive_path = find_archive_path(file_path, @base_dir) unless archive_path
  
  if File.directory?(file_path)
    raise ArgumentError, "\"#{file_path}\" is a directory"
  elsif ! File.exists?(file_path)
    raise ArgumentError, "\"#{file_path}\" does not exist"
  end
  
  @entries[archive_path || file_path] = File.new(file_path)
end

#add_files(files, base_dir = nil) ⇒ Object

Adds a list of files to the archive, at paths relative to base_dir (defaults to #base_dir) inside the archive.



112
113
114
115
116
# File 'lib/java_tools/jar.rb', line 112

def add_files( files, base_dir = nil )
  files.each do |file|
    add_file(file, find_archive_path(file, base_dir || @base_dir))
  end
end

#commit_manifestObject

:nodoc:



92
93
94
# File 'lib/java_tools/jar.rb', line 92

def commit_manifest # :nodoc:
  add_blob(manifest_string, 'META-INF/MANIFEST.MF')
end

#create_zipfileObject

:nodoc:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/java_tools/jar.rb', line 145

def create_zipfile # :nodoc:
  buffer_size = 65536
  
  zipstream = ZipOutputStream.new(FileOutputStream.new(@output))
  
  @entries.each do |path, io|
    while buffer = io.read(buffer_size)
      zipstream.put_next_entry(ZipEntry.new(path))
      zipstream.write(buffer.to_java_bytes, 0, buffer.length)
      zipstream.close_entry
    end
    
    io.close
  end
        
  zipstream.close
end

#default_manifestObject

:nodoc:



79
80
81
82
83
84
# File 'lib/java_tools/jar.rb', line 79

def default_manifest # :nodoc:
  {
    'Built-By' => "JavaTools v#{JavaTools::version}",
    'Manifest-Version' => '1.0'
  }
end

#entriesObject

:nodoc:



127
128
129
# File 'lib/java_tools/jar.rb', line 127

def entries # :nodoc:
  @entries.keys
end

#execute(io = $stderr) ⇒ Object

Creates the archive. If #verbose is true the equivalent command string for the jar command will be printed to the stream passed as io (or $stdout by default)



134
135
136
137
138
139
140
141
142
143
# File 'lib/java_tools/jar.rb', line 134

def execute( io = $stderr )
  raise "Output not set" unless @output
  
  compression_flag = @compression == 0 ? '0' : ''
  
  io.puts "jar -c#{compression_flag}f #{@output}" if @verbose
  
  commit_manifest
  create_zipfile
end

#find_archive_path(path, base_dir) ⇒ Object

:nodoc:



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/java_tools/jar.rb', line 163

def find_archive_path( path, base_dir ) # :nodoc:
  if base_dir
    prefix = base_dir + (base_dir =~ /\/$/ ? '' : '/')
  
    if 0 == path.index(prefix)
      return path.slice(prefix.length, path.length - prefix.length)
    end
  end
  
  path
end

#main_class=(class_name) ⇒ Object

Convenience method for setting the Main-Class manifest attribute



71
72
73
74
75
76
77
# File 'lib/java_tools/jar.rb', line 71

def main_class=( class_name )
  if class_name
    @manifest['Main-Class'] = class_name
  else
    remove_manifest_attribute('Main-Class')
  end
end

#manifest=(manifest_hash) ⇒ Object

Sets the attributes that will end up in the JAR manifest.

Attribute names are treated case insensitively, so setting both Main-Class and main-class will result in only one being used.

Will raise an error on malformed attribute names (must start with a letter or digit and contain only letter, digits, dashes and underscores).

The manifest you set will be merged with a set of default attributes (but yours will override).



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/java_tools/jar.rb', line 51

def manifest=( manifest_hash )
  @manifest = default_manifest
  
  manifest_hash.each do |key, value|
    raise ArgumentError, "Malformed attribute name #{key}" if key !~ /^[\w\d][\w\d\-_]*$/
    
    remove_manifest_attribute(key)
    
    @manifest[key] = value
  end
end

#manifest_stringObject

:nodoc:



86
87
88
89
90
# File 'lib/java_tools/jar.rb', line 86

def manifest_string # :nodoc:
  @manifest.keys.inject("") do |str, key|
    str + "#{key}: #{@manifest[key]}\n"
  end
end

#remove_entry(archive_path) ⇒ Object

:nodoc:



123
124
125
# File 'lib/java_tools/jar.rb', line 123

def remove_entry( archive_path ) # :nodoc:
  @entries.delete(archive_path)
end

#remove_manifest_attribute(name) ⇒ Object

Removes a manifest attribute, the comparison is case insensitive.



64
65
66
67
68
# File 'lib/java_tools/jar.rb', line 64

def remove_manifest_attribute( name ) # :nodoc:
  @manifest.delete_if do |key, value|
    key.downcase == name.downcase
  end      
end