lib/buildr/packaging/tar.rb in assaf-buildr-1.3.3 vs lib/buildr/packaging/tar.rb in assaf-buildr-1.3.4

- old
+ new

@@ -12,12 +12,12 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. -require 'buildr/packaging/zip' -require 'archive/tar/minitar' +require 'buildr/packaging/archive' +gem 'archive-tar-minitar' ; autoload :Archive, 'archive/tar/minitar' module Buildr # The TarTask creates a new Tar file. You can include any number of files and and directories, @@ -43,10 +43,39 @@ super self.gzip = name =~ /\.[t?]gz$/ self.mode = '0755' end + # :call-seq: + # entry(name) => Entry + # + # Returns a Tar file entry. You can use this to check if the entry exists and its contents, + # for example: + # package(:tar).entry("src/LICENSE").should contain(/Apache Software License/) + def entry(entry_name) + Buildr::TarEntry.new(self, entry_name) + end + + def entries() #:nodoc: + tar_entries = nil + with_uncompressed_tar { |tar| tar_entries = tar.entries } + tar_entries + end + + # :call-seq: + # with_uncompressed_tar { |tar_entries| ... } + # + # Yields an Archive::Tar::Minitar::Input object to the provided block. + # Opening, closing and Gzip-decompressing is automatically taken care of. + def with_uncompressed_tar &block + if gzip + Zlib::GzipReader.open(name) { |tar| Archive::Tar::Minitar.open(tar, &block) } + else + Archive::Tar::Minitar.open(name, &block) + end + end + private def create_from(file_map) if gzip StringIO.new.tap do |io| @@ -79,10 +108,63 @@ end end end end - + + + class TarEntry #:nodoc: + + def initialize(tar_task, entry_name) + @tar_task = tar_task + @entry_name = entry_name + end + + # :call-seq: + # contain?(*patterns) => boolean + # + # Returns true if this Tar file entry matches against all the arguments. An argument may be + # a string or regular expression. + def contain?(*patterns) + content = read_content_from_tar + patterns.map { |pattern| Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern.to_s)) }. + all? { |pattern| content =~ pattern } + end + + # :call-seq: + # empty?() => boolean + # + # Returns true if this entry is empty. + def empty?() + read_content_from_tar.nil? + end + + # :call-seq: + # exist() => boolean + # + # Returns true if this entry exists. + def exist?() + exist = false + @tar_task.with_uncompressed_tar { |tar| exist = tar.any? { |entry| entry.name == @entry_name } } + exist + end + + def to_s #:nodoc: + @entry_name + end + + private + + def read_content_from_tar + content = Errno::ENOENT.new("No such file or directory - #{@entry_name}") + @tar_task.with_uncompressed_tar do |tar| + content = tar.inject(content) { |content, entry| entry.name == @entry_name ? entry.read : content } + end + raise content if Exception === content + content + end + end + end # :call-seq: # tar(file) => TarTask