Module: Utils::Path::Helpers Private

Extended by:
T::Helpers
Included in:
Utils::Path, Utils::Path
Defined in:
utils/path.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Path helpers available as private methods when this module is included.

Instance Method Summary collapse

Instance Method Details

#child_of?(parent, child) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'utils/path.rb', line 20

def child_of?(parent, child)
  parent_pathname = Pathname(parent).expand_path
  child_pathname = Pathname(child).expand_path
  child_pathname.ascend { |p| return true if p == parent_pathname }
  false
end

#cp_path_sub(path, pattern, replacement, &_block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'utils/path.rb', line 42

def cp_path_sub(path, pattern, replacement, &_block)
  raise "#{path} does not exist" unless path.exist?

  pattern = pattern.to_s if pattern.is_a?(Pathname)
  replacement = replacement.to_s if replacement.is_a?(Pathname)
  dst = path.sub(pattern, replacement)

  raise "#{path} is the same file as #{dst}" if path == dst

  if path.directory?
    dst.mkpath
  else
    dst.dirname.mkpath
    dst = yield(path, dst) if block_given?
    FileUtils.cp(path, dst)
  end
end

#ensure_child_of!(parent, child, message:) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



28
29
30
31
32
# File 'utils/path.rb', line 28

def ensure_child_of!(parent, child, message:)
  return if child_of?(parent, child)

  raise message
end

#ensure_writable(path, &_block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



95
96
97
98
99
100
101
102
103
104
105
# File 'utils/path.rb', line 95

def ensure_writable(path, &_block)
  path = Pathname(path)
  saved_perms = nil
  unless path.writable?
    saved_perms = path.stat.mode
    FileUtils.chmod "u+rw", path.to_path
  end
  yield
ensure
  path.chmod saved_perms if saved_perms
end

#resolved_path(path) ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



81
82
83
# File 'utils/path.rb', line 81

def resolved_path(path)
  path.symlink? ? path.dirname.join(path.readlink) : path
end

#resolved_path_exists?(path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'utils/path.rb', line 86

def resolved_path_exists?(path)
  link = path.readlink
rescue ArgumentError
  false
else
  path.dirname.join(link).exist?
end

#rmdir_if_possible(path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'utils/path.rb', line 61

def rmdir_if_possible(path)
  path.rmdir
  true
rescue Errno::ENOTEMPTY
  if (ds_store = path/".DS_Store").exist? && path.children.one?
    ds_store.unlink
    retry
  else
    false
  end
rescue Errno::EACCES, Errno::ENOENT, Errno::EBUSY, Errno::EPERM
  false
end

#text_executable?(path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


76
77
78
# File 'utils/path.rb', line 76

def text_executable?(path)
  /\A#!\s*\S+/.match?(path.open("r") { |file| file.read(1024) })
end