Class: StringInreplaceExtension

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/extend/string.rb

Overview

Used by the inreplace function (in utils.rb).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ StringInreplaceExtension

Returns a new instance of StringInreplaceExtension.


9
10
11
12
# File 'Library/Homebrew/extend/string.rb', line 9

def initialize(str)
  @inreplace_string = str
  @errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors


7
8
9
# File 'Library/Homebrew/extend/string.rb', line 7

def errors
  @errors
end

#inreplace_stringObject

Returns the value of attribute inreplace_string


7
8
9
# File 'Library/Homebrew/extend/string.rb', line 7

def inreplace_string
  @inreplace_string
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.


33
34
35
36
37
# File 'Library/Homebrew/extend/string.rb', line 33

def change_make_var!(flag, new_value)
  return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", false)

  errors << "expected to change #{flag.inspect} to #{new_value.inspect}"
end

#get_make_var(flag) ⇒ Object

Finds the specified variable


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

def get_make_var(flag)
  inreplace_string[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]
end

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

Warn if nothing was replaced


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

def gsub!(before, after, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
  result = inreplace_string.gsub!(before, after)
  errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil?
  result
end

#remove_make_var!(flags) ⇒ Object

Removes variable assignments completely.


40
41
42
43
44
45
46
47
# File 'Library/Homebrew/extend/string.rb', line 40

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

#sub!(before, after) ⇒ Object


18
19
20
21
22
# File 'Library/Homebrew/extend/string.rb', line 18

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