Class: StringInreplaceExtension Private

Inherits:
Object
  • Object
show all
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 inreplace function (in utils.rb).

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:



13
14
15
16
# File 'utils/string_inreplace_extension.rb', line 13

def initialize(string)
  @inreplace_string = string
  @errors = T.let([], T::Array[String])
end

Instance Attribute Details

#errorsArray<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.

Returns:



7
8
9
# File 'utils/string_inreplace_extension.rb', line 7

def errors
  @errors
end

#inreplace_stringString

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:



10
11
12
# File 'utils/string_inreplace_extension.rb', line 10

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.

Parameters:



47
48
49
50
51
# File 'utils/string_inreplace_extension.rb', line 47

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) ⇒ String

Finds the specified variable.

Parameters:

Returns:



70
71
72
# File 'utils/string_inreplace_extension.rb', line 70

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

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

Same as String#gsub!, but warns if nothing was replaced.

Parameters:

Returns:



35
36
37
38
39
40
# File 'utils/string_inreplace_extension.rb', line 35

def gsub!(before, after, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
  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.

Parameters:



57
58
59
60
61
62
63
64
# File 'utils/string_inreplace_extension.rb', line 57

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) ⇒ String?

Same as String#sub!, but warns if nothing was replaced.

Parameters:

Returns:



22
23
24
25
26
# File 'utils/string_inreplace_extension.rb', line 22

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