49 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "active_support/core_ext/object/blank"
2017-05-29 17:51:38 -07:00
# Used by the inreplace function (in `utils.rb`).
2010-11-12 21:05:35 -08:00
module StringInreplaceExtension
attr_accessor :errors
def self.extended(str)
str.errors = []
end
def sub!(before, after)
result = super
errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result
result
end
# Warn if nothing was replaced
def gsub!(before, after, audit_result = true)
result = super(before, after)
errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil?
result
end
2017-09-10 16:31:02 +00:00
# Looks for Makefile style variable definitions and replaces the
2010-11-12 21:05:35 -08:00
# value with "new_value", or removes the definition entirely.
def change_make_var!(flag, new_value)
return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", false)
2018-09-17 02:45:00 +02:00
2016-09-23 22:02:23 +02:00
errors << "expected to change #{flag.inspect} to #{new_value.inspect}"
2010-11-12 21:05:35 -08:00
end
# Removes variable assignments completely.
def remove_make_var!(flags)
2013-02-15 00:35:52 -06:00
Array(flags).each do |flag|
2010-11-12 21:05:35 -08:00
# Also remove trailing \n, if present.
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=(?:.*\\\n)*.*$\n?/, "", false)
errors << "expected to remove #{flag.inspect}"
end
2010-11-12 21:05:35 -08:00
end
end
# Finds the specified variable
def get_make_var(flag)
self[/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]
2010-11-12 21:05:35 -08:00
end
end