lib/build/files.rb in build-graph-0.0.1 vs lib/build/files.rb in build-graph-0.1.0
- old
+ new
@@ -23,31 +23,19 @@
module Build
module Files
class List
include Enumerable
-
+
def +(list)
Composite.new([self, list])
end
-
+
def intersects? other
other.any?{|path| include?(path)}
end
-
- def rebase(root)
- raise NotImplementedError
- end
-
- def to_paths
- relative_paths = self.each do |path|
- path.relative_path
- end
- return Paths.new(@root, relative_paths)
- end
-
def match(pattern)
all? {|path| path.match(pattern)}
end
end
@@ -66,33 +54,64 @@
def relative_path
self.slice(@root.length..-1)
end
end
- class Directory < List
- def initialize(root, path = "")
+ # A list which has a single root directory.
+ class DirectoryList < List
+ def initialize(root)
@root = root.to_s
+ end
+
+ attr :root
+
+ def roots
+ [@root]
+ end
+
+ def rebase(root)
+ raise NotImplementedError
+ end
+
+ def to_paths(root=@root)
+ relative_paths = self.each do |path|
+ path.relative_path
+ end
+
+ return Paths.new(root, relative_paths)
+ end
+
+ def process(root=@root)
+ self.collect do |path|
+ basename, _, filename = path.relative_path.rpartition(File::SEPARATOR)
+
+ File.join(basename, yield(filename))
+ end
+
+ Paths.new(root, self.collect)
+ end
+ end
+
+ class Directory < DirectoryList
+ def initialize(root, path = "")
+ super(root)
+
@path = path
end
- attr :root
attr :path
def full_path
File.join(@root, @path)
end
def each(&block)
- Dir.glob(full_path + "**/*").each do |path|
+ Dir.glob(full_path + "**/*") do |path|
yield RelativePath.new(path, @root)
end
end
- def roots
- [full_path]
- end
-
def eql?(other)
other.kind_of?(self.class) and @root.eql?(other.root) and @path.eql?(other.path)
end
def hash
@@ -107,13 +126,14 @@
def rebase(root)
self.class.new(root, @path)
end
end
- class Glob < List
+ class Glob < DirectoryList
def initialize(root, pattern)
- @root = root.to_s
+ super(root)
+
@pattern = pattern
end
attr :root
attr :pattern
@@ -122,19 +142,15 @@
File.join(@root, @pattern)
end
# Enumerate all paths matching the pattern.
def each(&block)
- Dir.glob(full_pattern).each do |path|
+ Dir.glob(full_pattern) do |path|
yield RelativePath.new(path, @root)
end
end
-
- def roots
- [@root]
- end
-
+
def eql?(other)
other.kind_of?(self.class) and @root.eql?(other.root) and @pattern.eql?(other.pattern)
end
def hash
@@ -148,13 +164,14 @@
def rebase(root)
self.class.new(root, @pattern)
end
end
- class Paths < List
+ class Paths < DirectoryList
def initialize(root, paths)
- @root = root.to_s
+ super(root)
+
@paths = Array(paths)
end
attr :paths
@@ -163,13 +180,9 @@
full_path = File.join(@root, path)
yield RelativePath.new(full_path, @root)
end
end
- def roots
- [@root]
- end
-
def eql? other
other.kind_of?(self.class) and @paths.eql?(other.paths)
end
def hash