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
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, web_root = false) ⇒ 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.
- #reldir_from_web_root(in_path) ⇒ Object
Constructor Details
#initialize(src_root, dst_root, resource_dir = nil, web_root = false) ⇒ 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 |
# File 'lib/giblish/utils.rb', line 41 def initialize(src_root, dst_root, resource_dir = nil, web_root = false) # Make sure that the source root exists in the file system @src_root_abs = Pathname.new(src_root).realpath self.dst_root_abs = dst_root # Make sure that the resource dir exists if user gives a path to it resource_dir && (@resource_dir_abs = Pathname.new(resource_dir).realpath) # Set web root if given by user @web_root_abs = web_root ? Pathname.new(web_root) : nil end |
Instance Attribute Details
#dst_root_abs ⇒ Object
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
141 142 143 144 145 146 147 148 |
# File 'lib/giblish/utils.rb', line 141 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.
158 159 160 161 162 163 |
# File 'lib/giblish/utils.rb', line 158 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.
131 132 133 134 135 |
# File 'lib/giblish/utils.rb', line 131 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
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/giblish/utils.rb', line 106 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/giblish/utils.rb', line 81 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
64 65 66 67 68 69 70 71 72 |
# File 'lib/giblish/utils.rb', line 64 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 |
#reldir_from_web_root(in_path) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/giblish/utils.rb', line 74 def reldir_from_web_root(in_path) p = in_path.is_a?(Pathname) ? in_path : Pathname.new(in_path) return p if @web_root_abs.nil? p.relative_path_from(@web_root_abs) end |