Class: Buildr::ArchiveTask::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/buildr/packaging/archive.rb

Overview

Which files go where. All the rules for including, excluding and merging files are handled by this object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ Path

Returns a new instance of Path.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/buildr/packaging/archive.rb', line 28

def initialize(root, path)
  @root = root
  @path = path.empty? ? path : "#{path}/"
  @includes = FileList[]
  @excludes = []
  # Expand source files added to this path.
  expand_src = proc { @includes.map{ |file| file.to_s }.uniq }
  @sources = [ expand_src ]
  # Add files and directories added to this path.
  @actions = [] << proc do |file_map|
    expand_src.call.each do |path|
      unless excluded?(path)
        if File.directory?(path)
          in_directory path do |file, rel_path|
            dest = "#{@path}#{rel_path}"
            unless excluded?(dest)
              trace "Adding #{dest}"
              file_map[dest] = file
            end
          end
        end
        unless File.basename(path) == "."
          trace "Adding #{@path}#{File.basename(path)}"
          file_map["#{@path}#{File.basename(path)}"] = path
        end
      end
    end
  end
end

Instance Attribute Details

#rootObject (readonly)

Returns the archive from this path.



26
27
28
# File 'lib/buildr/packaging/archive.rb', line 26

def root
  @root
end

Instance Method Details

#add_files(file_map, transform_map) ⇒ Object

:nodoc:



136
137
138
# File 'lib/buildr/packaging/archive.rb', line 136

def add_files(file_map, transform_map) #:nodoc:
  @actions.each { |action| action.call(file_map, transform_map) }
end

#contain?(*files) ⇒ Boolean

:call-seq:

contain(file*) => boolean

Returns true if this ZIP file path contains all the specified files. You can use relative file names and glob patterns (using *, **, etc).

Returns:

  • (Boolean)


162
163
164
# File 'lib/buildr/packaging/archive.rb', line 162

def contain?(*files)
  files.all? { |file| entries.detect { |entry| File.fnmatch(file, entry.to_s) } }
end

#empty?Boolean

:call-seq:

empty? => boolean

Returns true if this path is empty (has no other entries inside).

Returns:

  • (Boolean)


153
154
155
# File 'lib/buildr/packaging/archive.rb', line 153

def empty?
  entries.all? { |entry| entry.empty? }
end

#entry(name) ⇒ Object

:call-seq:

entry(name) => ZipEntry

Returns a ZIP file entry. You can use this to check if the entry exists and its contents, for example:

package(:jar).path("META-INF").entry("LICENSE").should contain(/Apache Software License/)


172
173
174
# File 'lib/buildr/packaging/archive.rb', line 172

def entry(name)
  root.entry("#{@path}#{name}")
end

#exclude(*files) ⇒ Object

:call-seq:

exclude(*files) => self


96
97
98
99
100
101
# File 'lib/buildr/packaging/archive.rb', line 96

def exclude(*files)
  files = to_artifacts(files)
  @excludes |= files
  @excludes |= files.reject { |f| f =~ /\*$/ }.map { |f| "#{f}/*" }
  self
end

#exist?Boolean

:call-seq:

exist => boolean

Returns true if this path exists. This only works if the path has any entries in it, so exist on path happens to be the opposite of empty.

Returns:

  • (Boolean)


145
146
147
# File 'lib/buildr/packaging/archive.rb', line 145

def exist?
  !entries.empty?
end

#include(*args) ⇒ Object Also known as: add, <<

:call-seq:

include(*files) => self
include(*files, :path=>path) => self
include(file, :as=>name) => self
include(:from=>path) => self
include(*files, :merge=>true) => self


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/buildr/packaging/archive.rb', line 64

def include(*args)
  options = Hash === args.last ? args.pop : nil
  files = to_artifacts(args)
  raise 'AchiveTask.include() values should not include nil' if files.include? nil

  if options.nil? || options.empty?
    @includes.include *files.flatten
  elsif options[:path]
    sans_path = options.reject { |k,v| k == :path }
    path(options[:path]).include *files + [sans_path]
  elsif options[:as]
    raise 'You can only use the :as option in combination with the :path option' unless options.size == 1
    raise 'You can only use one file with the :as option' unless files.size == 1
    include_as files.first.to_s, options[:as]
  elsif options[:from]
    raise 'You can only use the :from option in combination with the :path option' unless options.size == 1
    raise 'You cannot use the :from option with file names' unless files.empty?
    fail 'AchiveTask.include() :from value should not be nil' if [options[:from]].flatten.include? nil
    [options[:from]].flatten.each { |path| include_as path.to_s, '.' }
  elsif options[:merge]
    raise 'You can only use the :merge option in combination with the :path option' unless options.size == 1
    files.each { |file| merge file }
  else
    raise "Unrecognized option #{options.keys.join(', ')}"
  end
  self
end

#merge(*args) ⇒ Object

:call-seq:

merge(*files) => Merge
merge(*files, :path=>name) => Merge

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/buildr/packaging/archive.rb', line 106

def merge(*args)
  options = Hash === args.last ? args.pop : {}
  files = to_artifacts(args)
  rake_check_options options, :path
  raise ArgumentError, "Expected at least one file to merge" if files.empty?
  path = options[:path] || @path
  expanders = files.collect do |file|
    @sources << proc { file.to_s }
    expander = ZipExpander.new(file)
    @actions << proc do |file_map, transform_map|
      file.invoke() if file.is_a?(Rake::Task)
      expander.expand(file_map, transform_map, path)
    end
    expander
  end
  Merge.new(expanders)
end

#path(path) ⇒ Object

Returns a Path relative to this one.



125
126
127
128
129
# File 'lib/buildr/packaging/archive.rb', line 125

def path(path)
  return self if path.nil?
  return root.path(path[1..-1]) if path[0] == ?/
  root.path("#{@path}#{path}")
end

#sourcesObject

Returns all the source files.



132
133
134
# File 'lib/buildr/packaging/archive.rb', line 132

def sources #:nodoc:
  @sources.map{ |source| source.call }.flatten
end

#to_sObject



176
177
178
# File 'lib/buildr/packaging/archive.rb', line 176

def to_s
  @path
end