Module: StringInreplaceExtension

Defined in:
Library/Homebrew/extend/string.rb

Overview

used by the inreplace function (in utils.rb)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors



23
24
25
# File 'Library/Homebrew/extend/string.rb', line 23

def errors
  @errors
end

Class Method Details

.extended(str) ⇒ Object



25
26
27
# File 'Library/Homebrew/extend/string.rb', line 25

def self.extended(str)
  str.errors = []
end

Instance Method Details

#change_make_var!(flag, new_value) ⇒ Object

Looks for Makefile style variable definitions and replaces the value with “new_value”, or removes the definition entirely.



48
49
50
51
# File 'Library/Homebrew/extend/string.rb', line 48

def change_make_var!(flag, new_value)
  return if gsub!(/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, "#{flag}=#{new_value}", false)
  errors << "expected to change #{flag.inspect} to #{new_value.inspect}"
end

#get_make_var(flag) ⇒ Object

Finds the specified variable



64
65
66
# File 'Library/Homebrew/extend/string.rb', line 64

def get_make_var(flag)
  self[/^#{Regexp.escape(flag)}[ \t]*=[ \t]*(.*)$/, 1]
end

#gsub!(before, after, audit_result = true) ⇒ Object

Warn if nothing was replaced



38
39
40
41
42
43
44
# File 'Library/Homebrew/extend/string.rb', line 38

def gsub!(before, after, audit_result = true)
  result = super(before, after)
  if audit_result && result.nil?
    errors << "expected replacement of #{before.inspect} with #{after.inspect}"
  end
  result
end

#remove_make_var!(flags) ⇒ Object

Removes variable assignments completely.



54
55
56
57
58
59
60
61
# File 'Library/Homebrew/extend/string.rb', line 54

def remove_make_var!(flags)
  Array(flags).each do |flag|
    # Also remove trailing \n, if present.
    unless gsub!(/^#{Regexp.escape(flag)}[ \t]*=.*$\n?/, "", false)
      errors << "expected to remove #{flag.inspect}"
    end
  end
end

#sub!(before, after) ⇒ Object



29
30
31
32
33
34
35
# File 'Library/Homebrew/extend/string.rb', line 29

def sub!(before, after)
  result = super
  unless result
    errors << "expected replacement of #{before.inspect} with #{after.inspect}"
  end
  result
end