Sha256: 12bf6f387eaafa3275f86461f3f422a8266c829e34bb75ff3584123f35f06b84

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require 'pathname'
require 'chrysalis/errors'


module Chrysalis
  
  class Archive
    @@archives = []
    
    def self.inherited(archive)
      @@archives << archive
    end
    
    # Extracts the archive, located at +path+, to the directory +to+ (which
    # defaults to the current directory).  An optional set of parameters is
    # accepted as +params+.
    #
    # Returns the directory the archive was extracted into.
    #
    # The return value assumes standard conventions are followed by the archive,
    # namely that the directory is the name of the archive file, without the
    # extension.
    #
    # For example, given an archive file libfoo-1.4.2.tar.gz, the directory
    # returned would be libfoo-1.4.2.
    #
    # For archives that don't follow this practice, parameters are available
    # to override default behavior.
    def self.extract(path, to = '.', params = {})
      pn = Pathname.new(path)
      return pn.cleanpath.to_s if pn.directory?
      return pn.cleanpath.to_s if params[:noop]
      
      @@archives.reverse.each { |archive|
        return archive.new(path, params).extract(to) if archive.extracts?(path)
      }
      raise ArchiveError, "Unknown archive format. (ext: #{pn.extname})"
    end
    
    
    def self.extracts?(path)
      false
    end
    
    def initialize(path, params = {})
      @path = path
    end
    
    def extract(to = '.')
      raise UnimplementedError, "Archive#extract not implemented"
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chrysalis-0.1.1 lib/chrysalis/archive.rb
chrysalis-0.1.0 lib/chrysalis/archive.rb