Class: Keg::Relocation
Constant Summary collapse
- RELOCATABLE_PATH_REGEX_PREFIX =
/(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))/.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#add_replacement_pair(key, old_value, new_value, path: false) ⇒ void
-
#freeze ⇒ Object
-
#initialize ⇒ Relocation
constructor
A new instance of Relocation.
-
#replace_text(text) ⇒ Boolean
-
#replacement_pair_for(key) ⇒ Array<String, Regexp>
Constructor Details
#initialize ⇒ Relocation
Returns a new instance of Relocation.
17 18 19 |
# File 'keg_relocate.rb', line 17 def initialize @replacement_map = {} end |
Class Method Details
.path_to_regex(path) ⇒ Regexp
54 55 56 57 58 59 60 61 62 |
# File 'keg_relocate.rb', line 54 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 returns an undefined value.
27 28 29 30 |
# File 'keg_relocate.rb', line 27 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 ⇒ Object
21 22 23 24 |
# File 'keg_relocate.rb', line 21 def freeze @replacement_map.freeze super end |
#replace_text(text) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'keg_relocate.rb', line 38 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[key]) any_changed ||= changed end !any_changed.nil? end |