Class: Buildr::UnzipTask

Inherits:
Rake::Task
  • Object
show all
Defined in:
lib/tasks/zip.rb

Overview

The UnzipTask expands the contents of a ZIP file into a target directory. You can include any number of files and directories, use exclusion patterns, and expand files from relative paths.

The file(s) to unzip is the first prerequisite.

Defined Under Namespace

Classes: FromPath

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ UnzipTask

Returns a new instance of UnzipTask.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/tasks/zip.rb', line 271

def initialize(*args)
  super
  @paths = {}
  enhance do |task|
    fail "Where do you want the file unzipped" unless target

    # 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(nil)
      @paths[nil].include "*"
    end

    # Otherwise, empty unzip creates target as a file when touching.
    mkpath target, :verbose=>false
    prerequisites.each do |file|
      Zip::ZipFile.open(file) do |zip|
        entries = zip.collect
        @paths.each do |path, patterns|
          patterns.map(entries).each do |dest, entry|
            next if entry.directory?
            dest = File.expand_path(dest, target)
            puts "Extracting #{dest}" if Rake.application.options.trace
            mkpath File.dirname(dest), :verbose=>false rescue nil
            entry.extract(dest) { true }
          end
        end
      end
    end
    # Let other tasks know we updated the target directory.
    touch target, :verbose=>false
  end
end

Instance Attribute Details

#targetObject

The target directory.



269
270
271
# File 'lib/tasks/zip.rb', line 269

def target
  @target
end

Instance Method Details

#exclude(*files) ⇒ Object

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.



330
331
332
333
334
335
336
337
# File 'lib/tasks/zip.rb', line 330

def exclude(*files)
  if Hash === files.last
    from_path(files.pop[:path]).exclude *files
  else
    from_path(nil).exclude *files
  end
  self
end

#from_path(path) ⇒ Object

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("test.jar").into(Dir.pwd).from_path("etc").include("LICENSE")

will unzip etc/LICENSE into ./LICENSE.

This is different from:

unzip("test.jar").into(Dir.pwd).include("etc/LICENSE")

which unzips etc/LICENSE into ./etc/LICENSE.



350
351
352
# File 'lib/tasks/zip.rb', line 350

def from_path(path)
  @paths[path] ||= FromPath.new(path)
end

#include(*files) ⇒ Object Also known as: add

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.



316
317
318
319
320
321
322
323
# File 'lib/tasks/zip.rb', line 316

def include(*files)
  if Hash === files.last
    from_path(files.pop[:path]).include *files
  else
    from_path(nil).include *files
  end
  self
end

#into(target) ⇒ Object

Specifies directory to unzip to and return self.



306
307
308
309
# File 'lib/tasks/zip.rb', line 306

def into(target)
  self.target = target
  self
end

#needed?Boolean

Returns:

  • (Boolean)


354
355
356
357
358
359
# File 'lib/tasks/zip.rb', line 354

def needed?()
  #return true unless target && File.exist?(target)
  #return true if prerequisites.any? { |prereq| File.stat(prereq).mtime > File.stat(target).mtime }
  #false
  true
end