Class: Buildr::ArchiveTask::Path
- Defined in:
- lib/buildr/core/checks.rb,
lib/buildr/packaging/zip.rb
Overview
Which files go where. All the rules for including, excluding and merging files are handled by this object.
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the archive from this path.
Instance Method Summary collapse
-
#add_files(file_map) ⇒ Object
:nodoc:.
-
#contain?(*files) ⇒ Boolean
:call-seq: contain(file*) => boolean.
-
#empty? ⇒ Boolean
:call-seq: empty?() => boolean.
-
#entry(name) ⇒ Object
:call-seq: entry(name) => ZipEntry.
-
#exclude(*files) ⇒ Object
:call-seq: exclude(*files) => self.
-
#exist? ⇒ Boolean
:call-seq: exist() => boolean.
-
#include(*args) ⇒ Object
(also: #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.
-
#initialize(root, path) ⇒ Path
constructor
A new instance of Path.
-
#merge(*args) ⇒ Object
:call-seq: merge(*files) => Merge merge(*files, :path=>name) => Merge.
-
#path(path) ⇒ Object
Returns a Path relative to this one.
-
#sources ⇒ Object
Returns all the source files.
- #to_s ⇒ Object
Constructor Details
#initialize(root, path) ⇒ Path
Returns a new instance of Path.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/buildr/packaging/zip.rb', line 34 def initialize(root, path) @root = root @path = path.empty? ? path : "#{path}/" @includes = FileList[] @excludes = [] # Expand source files added to this path. = proc { @includes.map{ |file| file.to_s }.uniq } @sources = [ ] # Add files and directories added to this path. @actions = [] << proc do |file_map| .call.each do |path| unless excluded?(path) if File.directory?(path) in_directory path do |file, rel_path| dest = "#{@path}#{rel_path}" trace "Adding #{dest}" file_map[dest] = file end else trace "Adding #{@path}#{File.basename(path)}" file_map["#{@path}#{File.basename(path)}"] = path end end end end end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the archive from this path.
32 33 34 |
# File 'lib/buildr/packaging/zip.rb', line 32 def root @root end |
Instance Method Details
#add_files(file_map) ⇒ Object
:nodoc:
134 135 136 |
# File 'lib/buildr/packaging/zip.rb', line 134 def add_files(file_map) #:nodoc: @actions.each { |action| action.call(file_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).
315 316 317 |
# File 'lib/buildr/core/checks.rb', line 315 def contain?(*files) files.all? { |file| entries.detect { |entry| File.fnmatch(file, entry.to_s, File::FNM_PATHNAME) } } end |
#empty? ⇒ Boolean
:call-seq:
empty?() => boolean
Returns true if this path is empty (has no other entries inside).
306 307 308 |
# File 'lib/buildr/core/checks.rb', line 306 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/)
325 326 327 |
# File 'lib/buildr/core/checks.rb', line 325 def entry(name) root.entry("#{@path}#{name}") end |
#exclude(*files) ⇒ Object
:call-seq:
exclude(*files) => self
97 98 99 100 101 102 |
# File 'lib/buildr/packaging/zip.rb', line 97 def exclude(*files) files = files.flatten.map(&:to_s) @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.
298 299 300 |
# File 'lib/buildr/core/checks.rb', line 298 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/buildr/packaging/zip.rb', line 67 def include(*args) = args.pop if Hash === args.last files = args.flatten if .nil? || .empty? @includes.include *files.flatten elsif [:path] sans_path = .reject { |k,v| k == :path } path([:path]).include *files + [sans_path] elsif [:as] raise 'You can only use the :as option in combination with the :path option' unless .size == 1 raise 'You can only use one file with the :as option' unless files.size == 1 include_as files.first.to_s, [:as] elsif [:from] raise 'You can only use the :from option in combination with the :path option' unless .size == 1 raise 'You canont use the :from option with file names' unless files.empty? [[:from]].flatten.each { |path| include_as path.to_s, '.' } elsif [:merge] raise 'You can only use the :merge option in combination with the :path option' unless .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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/buildr/packaging/zip.rb', line 107 def merge(*args) = Hash === args.last ? args.pop : {} files = args.flatten , :path raise ArgumentError, "Expected at least one file to merge" if files.empty? path = [:path] || @path = files.collect do |file| @sources << proc { file.to_s } = ZipExpander.new(file) @actions << proc { |file_map| .(file_map, path) } end Merge.new() end |
#path(path) ⇒ Object
Returns a Path relative to this one.
123 124 125 126 127 |
# File 'lib/buildr/packaging/zip.rb', line 123 def path(path) return self if path.nil? return root.path(path[1..-1]) if path[0] == ?/ root.path("#{@path}#{path}") end |
#sources ⇒ Object
Returns all the source files.
130 131 132 |
# File 'lib/buildr/packaging/zip.rb', line 130 def sources() #:nodoc: @sources.map{ |source| source.call }.flatten end |
#to_s ⇒ Object
138 139 140 |
# File 'lib/buildr/packaging/zip.rb', line 138 def to_s() @path end |