Sha256: eda5ede7285b288cc6831558d5e82c6b69b7b85c89c3502ef66a1a95b48eb0ca

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'chrysalis/archive'
require 'chrysalis/core_ext/string'


module Chrysalis
module Ar

  # Implements support for extracting tar archives.
  #
  # Archives of this type are identified by the file extension <em>tar</em>.
  class TarArchive < Archive
    
    EXTENSION = ".tar".freeze
    
    def self.extracts?(path)
      path.end? EXTENSION
    end
    
    def initialize(path, params = {})
      super
      @extract_to = params[:extract_to]
      @extracts_into = params[:extracts_into]
      @noop = params[:noop]
    end
    
    def extract(to = '.')
      dir = Pathname.new(to)
      dir = dir + @extract_to if @extract_to
      
      unless @noop
        FileUtils.mkpath dir
        
        bin = (RUBY_PLATFORM.match(/mswin/) ? "bsdtar" : "tar")
      
        puts "Extracting #{@path} ..."
        sh "#{bin} #{self.flags} #{@path} -C #{dir.cleanpath}"
      end
      
      ex_path = Pathname.new(dir).join(self.extracts_into)
      ex_path.cleanpath.to_s
    end
    
    protected
      def flags
        "xvf"
      end
    
      def extracts_into
        return @extracts_into if @extracts_into
        Pathname.new(@path).basename(EXTENSION)
      end
    
  end

end 
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chrysalis-0.1.0 lib/chrysalis/ar/tar.rb
chrysalis-0.1.1 lib/chrysalis/ar/tar.rb