Class: StringInreplaceExtension Private
- Includes:
 - Utils::Output::Mixin
 
- Defined in:
 - utils/string_inreplace_extension.rb
 
Overview
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.
Used by the Utils::Inreplace.inreplace function.
Instance Attribute Summary collapse
- #errors ⇒ Array<String> private
 - #inreplace_string ⇒ String private
 
Instance Method Summary collapse
- 
  
    
      #change_make_var!(flag, new_value)  ⇒ void 
    
    
  
  
  
  
  
  
  
  
  
    
Looks for Makefile style variable definitions and replaces the value with "new_value", or removes the definition entirely.
 - 
  
    
      #get_make_var(flag)  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Finds the specified variable.
 - 
  
    
      #gsub!(before, after, old_audit_result = nil, audit_result: true)  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    
Same as
String#gsub!, but warns if nothing was replaced. - #initialize(string) ⇒ void constructor private
 - 
  
    
      #remove_make_var!(flags)  ⇒ void 
    
    
  
  
  
  
  
  
  
  
  
    
Removes variable assignments completely.
 - 
  
    
      #sub!(before, after, audit_result: true)  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    
Same as
String#sub!, but warns if nothing was replaced. 
Methods included from Utils::Output::Mixin
#odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled
Constructor Details
#initialize(string) ⇒ 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.
      17 18 19 20  | 
    
      # File 'utils/string_inreplace_extension.rb', line 17 def initialize(string) @inreplace_string = string @errors = T.let([], T::Array[String]) end  | 
  
Instance Attribute Details
#errors ⇒ Array<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.
      11 12 13  | 
    
      # File 'utils/string_inreplace_extension.rb', line 11 def errors @errors end  | 
  
#inreplace_string ⇒ 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.
      14 15 16  | 
    
      # File 'utils/string_inreplace_extension.rb', line 14 def inreplace_string @inreplace_string end  | 
  
Instance Method Details
#change_make_var!(flag, new_value) ⇒ void
This method returns an undefined value.
Looks for Makefile style variable definitions and replaces the value with "new_value", or removes the definition entirely.
      61 62 63 64 65 66 67  | 
    
      # File 'utils/string_inreplace_extension.rb', line 61 def change_make_var!(flag, new_value) return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", audit_result: false) errors << "expected to change #{flag.inspect} to #{new_value.inspect}" end  | 
  
#get_make_var(flag) ⇒ String
Finds the specified variable.
      88 89 90  | 
    
      # File 'utils/string_inreplace_extension.rb', line 88 def get_make_var(flag) T.must(inreplace_string[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]) end  | 
  
#gsub!(before, after, old_audit_result = nil, audit_result: true) ⇒ String?
Same as String#gsub!, but warns if nothing was replaced.
      43 44 45 46 47 48 49 50 51 52 53 54  | 
    
      # File 'utils/string_inreplace_extension.rb', line 43 def gsub!(before, after, old_audit_result = nil, audit_result: true) # NOTE: must check for `#nil?` and not `#blank?`, or else `old_audit_result = false` will not call `odisabled`. unless old_audit_result.nil? odisabled "gsub!(before, after, #{old_audit_result})", "gsub!(before, after, audit_result: #{old_audit_result})" audit_result = old_audit_result end before = before.to_s if before.is_a?(Pathname) result = inreplace_string.gsub!(before, after.to_s) errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? result end  | 
  
#remove_make_var!(flags) ⇒ void
This method returns an undefined value.
Removes variable assignments completely.
      73 74 75 76 77 78 79 80 81 82  | 
    
      # File 'utils/string_inreplace_extension.rb', line 73 def remove_make_var!(flags) Array(flags).each do |flag| # Also remove trailing \n, if present. next if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=(?:.*\\\n)*.*$\n?/, "", audit_result: false) errors << "expected to remove #{flag.inspect}" end end  | 
  
#sub!(before, after, audit_result: true) ⇒ String?
Same as String#sub!, but warns if nothing was replaced.
      26 27 28 29 30  | 
    
      # File 'utils/string_inreplace_extension.rb', line 26 def sub!(before, after, audit_result: true) result = inreplace_string.sub!(before, after) errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? result end  |