Class: Giblish::PathManager
- Inherits:
-
Object
- Object
- Giblish::PathManager
- Defined in:
- lib/giblish/utils.rb
Overview
Helper class to ease construction of different paths for input and output files and directories
Instance Attribute Summary collapse
-
#dst_root_abs ⇒ Object
readonly
Returns the value of attribute dst_root_abs.
-
#resource_dir_abs ⇒ Object
readonly
Returns the value of attribute resource_dir_abs.
-
#src_root_abs ⇒ Object
readonly
Returns the value of attribute src_root_abs.
Class Method Summary collapse
-
.closest_dir(in_path) ⇒ Object
Public: Return the absolute path to the closest directory (defined as - the parent dir when called with an existing file - the directory itself when called with an existing directory - the parent dir when called with a non-existing file/directory.
-
.find_gitrepo_root(dirpath) ⇒ Object
Public: Find the root directory of the git repo in which the given dirpath resides.
-
.get_new_basename(src_filepath, file_ext) ⇒ Object
Public: Get the basename for a file by replacing the file extention of the source file with the supplied one.
Instance Method Summary collapse
-
#adoc_output_dir(infile_path) ⇒ Object
Public: Get the path to the directory where to generate the given file.
- #adoc_output_file(infile_path, extension) ⇒ Object
-
#initialize(src_root, dst_root, resource_dir = nil) ⇒ PathManager
constructor
Public:.
-
#reldir_from_src_root(in_path) ⇒ Object
Public: Get the relative path from the source root dir to the source file dir.
Constructor Details
#initialize(src_root, dst_root, resource_dir = nil) ⇒ PathManager
Public:
src_root - a string or pathname with the top directory of the input file
tree
dst_root - a string or pathname with the top directory of the output file
tree
resource_dir - a string or pathname with the directory containing
resources
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/giblish/utils.rb', line 41 def initialize(src_root, dst_root, resource_dir = nil) # Make sure that the source root exists in the file system @src_root_abs = Pathname.new(src_root).realpath # Make sure that the destination root exists and expand it to an # absolute path Pathname.new(dst_root).mkpath @dst_root_abs = Pathname.new(dst_root).realpath # Make sure that the resource dir exists if user gives a path to it resource_dir && (@resource_dir_abs = Pathname.new(resource_dir).realpath) end |
Instance Attribute Details
#dst_root_abs ⇒ Object (readonly)
Returns the value of attribute dst_root_abs.
30 31 32 |
# File 'lib/giblish/utils.rb', line 30 def dst_root_abs @dst_root_abs end |
#resource_dir_abs ⇒ Object (readonly)
Returns the value of attribute resource_dir_abs.
31 32 33 |
# File 'lib/giblish/utils.rb', line 31 def resource_dir_abs @resource_dir_abs end |
#src_root_abs ⇒ Object (readonly)
Returns the value of attribute src_root_abs.
29 30 31 |
# File 'lib/giblish/utils.rb', line 29 def src_root_abs @src_root_abs end |
Class Method Details
.closest_dir(in_path) ⇒ Object
Public: Return the absolute path to the closest directory (defined as
- the parent dir when called with an existing file
- the directory itself when called with an existing directory
- the parent dir when called with a non-existing file/directory
128 129 130 131 132 133 134 135 |
# File 'lib/giblish/utils.rb', line 128 def self.closest_dir(in_path) sr = in_path.is_a?(Pathname) ? in_path : Pathname.new(in_path) if sr.exist? sr.directory? ? sr.realpath : sr.dirname.realpath else sr.parent. end end |
.find_gitrepo_root(dirpath) ⇒ Object
Public: Find the root directory of the git repo in which the
given dirpath resides.
dirpath - a relative or absolute path to a directory that resides
within a git repo.
Returns: the root direcotry of the git repo or nil if the input path
does not reside within a git repo.
145 146 147 148 149 150 |
# File 'lib/giblish/utils.rb', line 145 def self.find_gitrepo_root(dirpath) Pathname.new(dirpath)..ascend do |p| git_dir = p.join(".git") return p if git_dir.directory? end end |
.get_new_basename(src_filepath, file_ext) ⇒ Object
Public: Get the basename for a file by replacing the file
extention of the source file with the supplied one.
src_filepath - the full path of the source file file_ext - the file extention of the resulting file name
Example
Giblish::PathManager.get_new_basename(
"/my/old/file.txt","pdf") => "file.pdf"
Returns: the basename of a file that uses the supplied file extention.
118 119 120 121 122 |
# File 'lib/giblish/utils.rb', line 118 def self.get_new_basename(src_filepath, file_ext) p = Pathname.new src_filepath newname = p.basename.to_s.reverse.sub(p.extname.reverse, ".").reverse newname << file_ext end |
Instance Method Details
#adoc_output_dir(infile_path) ⇒ Object
Public: Get the path to the directory where to generate the given
file. The path is given as the relative path from the source adoc
file to the desired output directory (required by the Asciidoctor
API).
infile_path - a string or Pathname containing the absolute path of the
source adoc file
Returns: a Pathname with the relative path from the source file to the
output directory
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/giblish/utils.rb', line 93 def adoc_output_dir(infile_path) # Get absolute source dir path src_abs = self.class.closest_dir infile_path # Get relative path from source root dir src_rel = src_abs.relative_path_from(@src_root_abs) # Get the destination path relative the absolute source # root dst_abs = @dst_root_abs.realpath.join(src_rel) dst_abs.relative_path_from(src_abs) end |
#adoc_output_file(infile_path, extension) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/giblish/utils.rb', line 68 def adoc_output_file(infile_path, extension) # Get absolute source dir path src_dir_abs = self.class.closest_dir infile_path # Get relative path from source root dir src_dir_rel = src_dir_abs.relative_path_from(@src_root_abs) # Get the destination path relative the absolute source # root dst_dir_abs = @dst_root_abs.realpath.join(src_dir_rel) # return full file path with correct extension dst_dir_abs + get_new_basename(infile_path, extension) end |
#reldir_from_src_root(in_path) ⇒ Object
Public: Get the relative path from the source root dir to the
source file dir.
in_path - an absolute or relative path
58 59 60 61 62 63 64 65 66 |
# File 'lib/giblish/utils.rb', line 58 def reldir_from_src_root(in_path) p = in_path.is_a?(Pathname) ? in_path : Pathname.new(in_path) # Get absolute source dir path src_abs = p.directory? ? p.realpath : p.dirname.realpath # Get relative path from source root dir src_abs.relative_path_from(@src_root_abs) end |