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.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/java_tools/jar.rb', line 19

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

Instance Attribute Details

#base_dirObject

string



13
14
15
# File 'lib/java_tools/jar.rb', line 13

def base_dir
  @base_dir
end

#compressionObject

string



13
14
15
# File 'lib/java_tools/jar.rb', line 13

def compression
  @compression
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#verboseObject

string



13
14
15
# File 'lib/java_tools/jar.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#add_blob(str, archive_path) ⇒ Object



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

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

#add_file(file_path, archive_path = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/java_tools/jar.rb', line 74

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



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

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



70
71
72
# File 'lib/java_tools/jar.rb', line 70

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

#create_zipfileObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/java_tools/jar.rb', line 115

def create_zipfile
  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



57
58
59
60
61
62
# File 'lib/java_tools/jar.rb', line 57

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

#entriesObject



100
101
102
# File 'lib/java_tools/jar.rb', line 100

def entries
  @entries.keys
end

#execute(io = $stderr) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/java_tools/jar.rb', line 104

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



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

def find_archive_path( path, base_dir )
  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



49
50
51
52
53
54
55
# File 'lib/java_tools/jar.rb', line 49

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



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

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



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

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

#remove_entry(archive_path) ⇒ Object



96
97
98
# File 'lib/java_tools/jar.rb', line 96

def remove_entry( archive_path )
  @entries.delete(archive_path)
end

#remove_manifest_attribute(name) ⇒ Object



43
44
45
46
47
# File 'lib/java_tools/jar.rb', line 43

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