lib/build/files/path.rb in build-files-1.0.3 vs lib/build/files/path.rb in build-files-1.0.4

- old
+ new

@@ -34,11 +34,11 @@ else path.split(File::SEPARATOR) end end - # Return the shortest relative path to get to path from root: + # Return the shortest relative path to get to path from root. Root should be a directory with which you are computing the relative path. def self.shortest_path(path, root) path_components = Path.components(path) root_components = Path.components(root) # Find the common prefix: @@ -93,14 +93,18 @@ def basename self.parts.last end + def start_with?(*args) + @full_path.start_with?(*args) + end + alias parts components def relative_path - @relative_path ||= Path.relative_path(@root.to_s, @full_path) + @relative_path ||= Path.relative_path(@root.to_s, @full_path).freeze end def relative_parts dirname, _, basename = self.relative_path.rpartition(File::SEPARATOR) @@ -131,9 +135,18 @@ self.class.new(File.join(root, relative_path), root, relative_path) end def self.join(root, relative_path) self.new(File.join(root, relative_path), root) + end + + # Expand a subpath within a given root, similar to `File.expand_path` + def self.expand(subpath, root = Dir.getwd) + if subpath.start_with? File::SEPARATOR + self.new(subpath) + else + self.join(root, subpath) + end end def shortest_path(root) self.class.shortest_path(self, root) end