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 tar. 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