Class: Giblish::PathManager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_root, dst_root, resource_dir = nil, create_search_asset_dir = 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

create_search_asset_dir - true if this instance shall create a dir for storing

search artefacts, false otherwise

133
134
135
136
137
138
139
140
141
142
# File 'lib/giblish/utils.rb', line 133

def initialize(src_root, dst_root, resource_dir = nil, create_search_asset_dir = 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

  self.search_assets_abs = (@dst_root_abs.join("search_assets") if create_search_asset_dir)

  # 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_absObject

Returns the value of attribute dst_root_abs.


121
122
123
# File 'lib/giblish/utils.rb', line 121

def dst_root_abs
  @dst_root_abs
end

#resource_dir_absObject (readonly)

Returns the value of attribute resource_dir_abs.


121
122
123
# File 'lib/giblish/utils.rb', line 121

def resource_dir_abs
  @resource_dir_abs
end

#search_assets_absObject

Returns the value of attribute search_assets_abs.


121
122
123
# File 'lib/giblish/utils.rb', line 121

def search_assets_abs
  @search_assets_abs
end

#src_root_absObject (readonly)

Returns the value of attribute src_root_abs.


121
122
123
# File 'lib/giblish/utils.rb', line 121

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

284
285
286
287
288
289
290
291
# File 'lib/giblish/utils.rb', line 284

def self.closest_dir(in_path)
  sr = to_pathname(in_path)
  if sr.exist?
    sr.directory? ? sr.realpath : sr.dirname.realpath
  else
    sr.parent.expand_path
  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.

301
302
303
304
305
306
# File 'lib/giblish/utils.rb', line 301

def self.find_gitrepo_root(dirpath)
  Pathname.new(dirpath).realpath.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.


274
275
276
277
278
# File 'lib/giblish/utils.rb', line 274

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

.to_pathname(path) ⇒ Object

return a pathname, regardless if the given path is a Pathname or a string


258
259
260
# File 'lib/giblish/utils.rb', line 258

def self.to_pathname(path)
  path.is_a?(Pathname) ? path : Pathname.new(path)
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

243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/giblish/utils.rb', line 243

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


218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/giblish/utils.rb', line 218

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

#dst_abs_from_src_abs(src_path) ⇒ Object

return the destination dir corresponding to the given src path the src path must exist in the file system


203
204
205
206
207
# File 'lib/giblish/utils.rb', line 203

def dst_abs_from_src_abs(src_path)
  src_abs = (self.class.to_pathname src_path).realpath
  src_rel = reldir_from_src_root src_abs
  @dst_root_abs.join(src_rel)
end

#reldir_from_dst_root(path) ⇒ Object

Public: Get the relative path from the dst root dir to the

directory where the supplied path points.

path - an absolute or relative path to a file or dir


186
187
188
189
# File 'lib/giblish/utils.rb', line 186

def reldir_from_dst_root(path)
  dst = self.class.closest_dir path
  dst.relative_path_from(@dst_root_abs)
end

#reldir_from_src_root(in_path) ⇒ Object

Public: Get the relative path from the source root dir to the

directory where the supplied path points.

in_path - an absolute or relative path to a file or dir


167
168
169
170
# File 'lib/giblish/utils.rb', line 167

def reldir_from_src_root(in_path)
  p = self.class.closest_dir in_path
  p.relative_path_from(@src_root_abs)
end

#reldir_to_dst_root(path) ⇒ Object

Public: Get the relative path from the

directory where the supplied path points to
the dst root dir

path - an absolute or relative path to a file or dir


196
197
198
199
# File 'lib/giblish/utils.rb', line 196

def reldir_to_dst_root(path)
  dst = self.class.closest_dir path
  @dst_root_abs.relative_path_from(dst)
end

#reldir_to_src_root(path) ⇒ Object

Public: Get the relative path from the

directory where the supplied path points to
the src root dir

path - an absolute or relative path to a file or dir


177
178
179
180
# File 'lib/giblish/utils.rb', line 177

def reldir_to_src_root(path)
  src = self.class.closest_dir path
  @src_root_abs.relative_path_from(src)
end

#relpath_to_dir_after_generate(src_filepath, dir_path) ⇒ Object

return the relative path from a generated document to the supplied folder given the corresponding absolute source file path


212
213
214
215
216
# File 'lib/giblish/utils.rb', line 212

def relpath_to_dir_after_generate(src_filepath, dir_path)
  dst_abs = dst_abs_from_src_abs(src_filepath)
  dir = self.class.to_pathname(dir_path)
  dir.relative_path_from(dst_abs)
end