lib/tap/root.rb in bahuvrihi-tap-0.10.1 vs lib/tap/root.rb in bahuvrihi-tap-0.10.2

- old
+ new

@@ -76,10 +76,23 @@ # use dir.length + 1 to remove a leading '/'. If dir.length + 1 >= expanded.length # as in: relative_filepath('/path', '/path') then the first arg returns nil, and an # empty string is returned expanded_path[( expanded_dir.chomp("/").length + 1)..-1] || "" + end + + # Generates a target filepath translated from the source_dir to + # the target_dir. Raises an error if the filepath is not relative + # to the source_dir. + # + # Root.translate("/path/to/file.txt", "/path", "/another/path") # => '/another/path/to/file.txt' + # + def translate(path, source_dir, target_dir) + unless relative_path = relative_filepath(source_dir, path) + raise ArgumentError, "\n#{path}\nis not relative to:\n#{source_dir}" + end + File.join(target_dir, relative_path) end # Lists all unique paths matching the input glob patterns. def glob(*patterns) patterns.collect do |pattern| @@ -107,29 +120,23 @@ base = File.expand_path(base) Dir.glob(File.join(base, suffix_pattern)) end.flatten.uniq end - # Executes the block in the specified directory. Makes the directory, if - # necessary when mkdir is specified. Otherwise, indir raises an error - # for non-existant directories, as well as non-directory inputs. - def indir(dir, mkdir=false) + # Like Dir.chdir but makes the directory, if necessary, when + # mkdir is specified. chdir raises an error for non-existant + # directories, as well as non-directory inputs. + def chdir(dir, mkdir=false, &block) unless File.directory?(dir) if !File.exists?(dir) && mkdir FileUtils.mkdir_p(dir) else raise "not a directory: #{dir}" end end - - pwd = Dir.pwd - begin - Dir.chdir(dir) - yield - ensure - Dir.chdir(pwd) - end + + Dir.chdir(dir, &block) end # The path root type indicating windows, *nix, or some unknown # style of filepaths (:win, :nix, :unknown). def path_root_type @@ -525,21 +532,18 @@ # Retrieves the filepath relative to the path of the specified alias. def relative_filepath(dir, filepath) Root.relative_filepath(self[dir], filepath) end - # Generates a target filepath translated from the aliased input dir to - # the aliased output dir. Raises an error if the filepath is not relative - # to the aliased input dir. + # Generates a target filepath translated from the aliased source_dir to + # the aliased target_dir. Raises an error if the filepath is not relative + # to the aliased source_dir. # # fp = r.filepath(:in, 'path/to/file.txt') # => '/root_dir/in/path/to/file.txt' # r.translate(fp, :in, :out) # => '/root_dir/out/path/to/file.txt' - def translate(filepath, input_dir, output_dir) - unless relative_path = relative_filepath(input_dir, filepath) - raise "\n#{filepath}\nis not relative to:\n#{input_dir}" - end - filepath(output_dir, relative_path) + def translate(filepath, source_dir, target_dir) + Root.translate(filepath, self[source_dir], self[target_dir]) end # Lists all files in the aliased dir matching the input patterns. Patterns # should be valid inputs for +Dir.glob+. If no patterns are specified, lists # all files/folders matching '**/*'. @@ -553,12 +557,12 @@ # If no patterns are specified, then all versions of filename will be returned. def vglob(dir, filename, *vpatterns) Root.vglob(filepath(dir, filename), *vpatterns) end - # Executes the provided block in the specified directory using Root.indir. - def indir(dir, mkdir=false) - Root.indir(self[dir], mkdir) { yield } + # chdirs to the specified directory using Root.chdir. + def chdir(dir, mkdir=false, &block) + Root.chdir(self[dir], mkdir, &block) end private # reassigns all paths with the input root, directories, and absolute_paths \ No newline at end of file