Class: Buildr::Unzip
Overview
An object for unzipping/untarring a file into a target directory. You can tell it to include or exclude only specific files and directories, and also to map files from particular paths inside the zip file into the target directory. Once ready, call #extract.
Usually it is more convenient to create a file task for extracting the zip file (see #unzip) and pass this object as a prerequisite to other tasks.
See Buildr#unzip.
Defined Under Namespace
Classes: FromPath
Instance Attribute Summary collapse
-
#target ⇒ Object
The target directory to extract to.
-
#zip_file ⇒ Object
The zip file to extract.
Instance Method Summary collapse
-
#exclude(*files) ⇒ Object
:call-seq: exclude(*files) => self.
-
#extract ⇒ Object
:call-seq: extract.
-
#from_path(name) ⇒ Object
(also: #path)
:call-seq: from_path(name) => Path.
-
#include(*files) ⇒ Object
(also: #add)
:call-seq: include(*files) => self include(*files, :path=>name) => self.
-
#included?(entry_name) ⇒ Boolean
reads the includes/excludes and apply them to the entry_name.
-
#initialize(args) ⇒ Unzip
constructor
Initialize with hash argument of the form target=>zip_file.
-
#root ⇒ Object
:call-seq: root => Unzip.
-
#to_s ⇒ Object
Returns the path to the target directory.
Constructor Details
#initialize(args) ⇒ Unzip
Initialize with hash argument of the form target=>zip_file.
127 128 129 130 131 |
# File 'lib/buildr/packaging/ziptask.rb', line 127 def initialize(args) @target, arg_names, zip_file = Buildr.application.resolve_args([args]) @zip_file = zip_file.first @paths = {} end |
Instance Attribute Details
#target ⇒ Object
The target directory to extract to.
124 125 126 |
# File 'lib/buildr/packaging/ziptask.rb', line 124 def target @target end |
#zip_file ⇒ Object
The zip file to extract.
122 123 124 |
# File 'lib/buildr/packaging/ziptask.rb', line 122 def zip_file @zip_file end |
Instance Method Details
#exclude(*files) ⇒ Object
:call-seq:
exclude(*files) => self
Exclude all files that match the patterns and return self.
Use exclude to unzip all files except those that match the pattern. You can use #exclude in combination with #include.
234 235 236 237 238 239 240 241 |
# File 'lib/buildr/packaging/ziptask.rb', line 234 def exclude(*files) if Hash === files.last from_path(files.pop[:path]).exclude *files else from_path(nil).exclude *files end self end |
#extract ⇒ Object
:call-seq:
extract
Extract the zip/tgz file into the target directory.
You can call this method directly. However, if you are using the #unzip method, it creates a file task for the target directory: use that task instead as a prerequisite. For example:
build unzip(dir=>zip_file)
Or:
unzip(dir=>zip_file).target.invoke
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/buildr/packaging/ziptask.rb', line 144 def extract # If no paths specified, then no include/exclude patterns # specified. Nothing will happen unless we include all files. if @paths.empty? @paths[nil] = FromPath.new(self, nil) end # Otherwise, empty unzip creates target as a file when touching. mkpath target.to_s if zip_file.to_s.match /\.t?gz$/ #un-tar.gz Zlib::GzipReader.open(zip_file.to_s) { |tar| Archive::Tar::Minitar::Input.open(tar) do |inp| inp.each do |tar_entry| @paths.each do |path, patterns| patterns.map([tar_entry]).each do |dest, entry| next if entry.directory? dest = File.(dest, target.to_s) trace "Extracting #{dest}" mkpath File.dirname(dest) rescue nil #entry.restore_permissions = true File.open(dest, 'wb') {|f| f.write entry.read} end end end end } else Zip::ZipFile.open(zip_file.to_s) do |zip| entries = zip.collect @paths.each do |path, patterns| patterns.map(entries).each do |dest, entry| next if entry.directory? dest = File.(dest, target.to_s) trace "Extracting #{dest}" mkpath File.dirname(dest) rescue nil entry. = true entry.extract(dest) { true } end end end end # Let other tasks know we updated the target directory. touch target.to_s end |
#from_path(name) ⇒ Object Also known as: path
:call-seq:
from_path(name) => Path
Allows you to unzip from a path. Returns an object you can use to specify which files to include/exclude relative to that path. Expands the file relative to that path.
For example:
unzip(Dir.pwd=>'test.jar').from_path('etc').include('LICENSE')
will unzip etc/LICENSE into ./LICENSE.
This is different from:
unzip(Dir.pwd=>'test.jar').include('etc/LICENSE')
which unzips etc/LICENSE into ./etc/LICENSE.
257 258 259 |
# File 'lib/buildr/packaging/ziptask.rb', line 257 def from_path(name) @paths[name] ||= FromPath.new(self, name) end |
#include(*files) ⇒ Object Also known as: add
:call-seq:
include(*files) => self
include(*files, :path=>name) => self
Include all files that match the patterns and returns self.
Use include if you only want to unzip some of the files, by specifying them instead of using exclusion. You can use #include in combination with #exclude.
217 218 219 220 221 222 223 224 |
# File 'lib/buildr/packaging/ziptask.rb', line 217 def include(*files) if Hash === files.last from_path(files.pop[:path]).include *files else from_path(nil).include *files end self end |
#included?(entry_name) ⇒ Boolean
reads the includes/excludes and apply them to the entry_name
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/buildr/packaging/ziptask.rb', line 191 def included?(entry_name) @paths.each do |path, patterns| return true if path.nil? if entry_name =~ /^#{path}/ short = entry_name.sub(path, '') if patterns.include.any? { |pattern| File.fnmatch(pattern, entry_name) } && !patterns.exclude.any? { |pattern| File.fnmatch(pattern, entry_name) } # trace "tar_entry.full_name " + entry_name + " is included" return true end end end # trace "tar_entry.full_name " + entry_name + " is excluded" return false end |
#to_s ⇒ Object
Returns the path to the target directory.
272 273 274 |
# File 'lib/buildr/packaging/ziptask.rb', line 272 def to_s target.to_s end |