Class: Keg::Relocation Private

Inherits:
Object show all
Defined in:
keg_relocate.rb

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

Constant Summary collapse

RELOCATABLE_PATH_REGEX_PREFIX =

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

/(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ 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.



23
24
25
# File 'keg_relocate.rb', line 23

def initialize
  @replacement_map = T.let({}, T::Hash[Symbol, [T.any(String, Regexp), String]])
end

Class Method Details

.path_to_regex(path) ⇒ Regexp

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:

  • (Regexp)


61
62
63
64
65
66
67
68
69
# File 'keg_relocate.rb', line 61

def self.path_to_regex(path)
  path = case path
  when String
    Regexp.escape(path)
  when Regexp
    path.source
  end
  Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + path)
end

Instance Method Details

#add_replacement_pair(key, old_value, new_value, path: false) ⇒ 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:

  • key (Symbol)
  • old_value (String, Regexp)
  • new_value (String)
  • path (Boolean) (defaults to: false)


34
35
36
37
# File 'keg_relocate.rb', line 34

def add_replacement_pair(key, old_value, new_value, path: false)
  old_value = self.class.path_to_regex(old_value) if path
  @replacement_map[key] = [old_value, new_value]
end

#freeze ⇒ Relocation

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.

Returns:



28
29
30
31
# File 'keg_relocate.rb', line 28

def freeze
  @replacement_map.freeze
  super
end

#replace_text!(text) ⇒ 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)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'keg_relocate.rb', line 45

def replace_text!(text)
  replacements = @replacement_map.values.to_h

  sorted_keys = replacements.keys.sort_by do |key|
    key.is_a?(String) ? key.length : 999
  end.reverse

  any_changed = T.let(nil, T.nilable(String))
  sorted_keys.each do |key|
    changed = text.gsub!(key, replacements.fetch(key))
    any_changed ||= changed
  end
  !any_changed.nil?
end

#replacement_pair_for(key) ⇒ Array<([String, Regexp], String)>

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:



40
41
42
# File 'keg_relocate.rb', line 40

def replacement_pair_for(key)
  @replacement_map.fetch(key)
end