Module: Dotsync::PathUtils

Included in:
BaseAction, BaseConfig, DirectoryDiffer, Mapping, MappingsTransfer
Defined in:
lib/dotsync/utils/path_utils.rb

Constant Summary collapse

ENV_VARS_COLOR =
104

Instance Method Summary collapse

Instance Method Details

#colorize_env_vars(path) ⇒ Object



15
16
17
# File 'lib/dotsync/utils/path_utils.rb', line 15

def colorize_env_vars(path)
  path.gsub(/\$(\w+)/) { "\e[38;5;#{ENV_VARS_COLOR}m$#{$1}\e[0m" }
end

#expand_env_vars(path) ⇒ Object



7
8
9
# File 'lib/dotsync/utils/path_utils.rb', line 7

def expand_env_vars(path)
  path.gsub(/\$(\w+)/) { ENV[$1] }
end

#extract_env_vars(path) ⇒ Object



11
12
13
# File 'lib/dotsync/utils/path_utils.rb', line 11

def extract_env_vars(path)
  path.scan(/\$(\w+)/).flatten
end

#path_is_parent_or_same?(parent, child) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/dotsync/utils/path_utils.rb', line 27

def path_is_parent_or_same?(parent, child)
  parent = Pathname.new(parent).expand_path
  child  = Pathname.new(child).expand_path
  child.ascend.any? { |ancestor| ancestor == parent }
end

#relative_to_absolute(paths, base_path) ⇒ Array<String>

Converts an array of relative paths to absolute paths based on a specified base path

Parameters:

  • paths (Array<String>)

    The array of relative paths

  • base_path (String)

    The base absolute path to resolve relative paths

Returns:

  • (Array<String>)

    The array of absolute paths



23
24
25
# File 'lib/dotsync/utils/path_utils.rb', line 23

def relative_to_absolute(paths, base_path)
  paths.map { |path| File.join(base_path, path) }
end

#sanitize_path(path) ⇒ String

Sanitizes a given path by expanding it and translating /tmp to /private/tmp

Parameters:

  • path (String)

    The input path to sanitize

Returns:

  • (String)

    The sanitized path



48
49
50
# File 'lib/dotsync/utils/path_utils.rb', line 48

def sanitize_path(path)
  translate_tmp_path(File.expand_path(expand_env_vars(path)))
end

#translate_tmp_path(path) ⇒ String

Translates /tmp paths to /private/tmp paths on macOS Retains other paths as-is

Parameters:

  • path (String)

    The input path to translate

Returns:

  • (String)

    The translated path



37
38
39
40
41
42
43
# File 'lib/dotsync/utils/path_utils.rb', line 37

def translate_tmp_path(path)
  if path.start_with?("/tmp") && RUBY_PLATFORM.include?("darwin")
    path.sub("/tmp", "/private/tmp")
  else
    path
  end
end