| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  | # typed: strong | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  | # Used by the {Utils::Inreplace.inreplace} function. | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | class StringInreplaceExtension | 
					
						
							| 
									
										
										
										
											2023-07-23 15:56:26 -07:00
										 |  |  |   sig { returns(T::Array[String]) } | 
					
						
							|  |  |  |   attr_accessor :errors | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sig { returns(String) } | 
					
						
							|  |  |  |   attr_accessor :inreplace_string | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   sig { params(string: String).void } | 
					
						
							|  |  |  |   def initialize(string) | 
					
						
							|  |  |  |     @inreplace_string = string | 
					
						
							| 
									
										
										
										
											2023-07-23 15:56:26 -07:00
										 |  |  |     @errors = T.let([], T::Array[String]) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Same as `String#sub!`, but warns if nothing was replaced. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  |   sig { params(before: T.any(Regexp, String), after: String, audit_result: T::Boolean).returns(T.nilable(String)) } | 
					
						
							|  |  |  |   def sub!(before, after, audit_result: true) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     result = inreplace_string.sub!(before, after) | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  |     errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Same as `String#gsub!`, but warns if nothing was replaced. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |   sig { | 
					
						
							| 
									
										
										
										
											2024-08-31 12:29:43 +08:00
										 |  |  |     params( | 
					
						
							|  |  |  |       before:           T.any(Pathname, Regexp, String), | 
					
						
							|  |  |  |       after:            T.any(Pathname, String), | 
					
						
							|  |  |  |       old_audit_result: T.nilable(T::Boolean), | 
					
						
							|  |  |  |       audit_result:     T::Boolean, | 
					
						
							|  |  |  |     ).returns(T.nilable(String)) | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-08-31 12:29:43 +08:00
										 |  |  |   def gsub!(before, after, old_audit_result = nil, audit_result: true) | 
					
						
							| 
									
										
										
										
											2024-09-02 16:02:35 +08:00
										 |  |  |     # NOTE: must check for `#nil?` and not `#blank?`, or else `old_audit_result = false` will not call `odeprecated`. | 
					
						
							| 
									
										
										
										
											2024-08-31 12:29:43 +08:00
										 |  |  |     unless old_audit_result.nil? | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |       odeprecated "gsub!(before, after, #{old_audit_result})", | 
					
						
							|  |  |  |                   "gsub!(before, after, audit_result: #{old_audit_result})" | 
					
						
							| 
									
										
										
										
											2024-08-31 12:29:43 +08:00
										 |  |  |       audit_result = old_audit_result | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-07-23 18:42:22 -07:00
										 |  |  |     before = before.to_s if before.is_a?(Pathname) | 
					
						
							| 
									
										
										
										
											2023-07-23 21:14:21 -07:00
										 |  |  |     result = inreplace_string.gsub!(before, after.to_s) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? | 
					
						
							|  |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Looks for Makefile style variable definitions and replaces the | 
					
						
							|  |  |  |   # value with "new_value", or removes the definition entirely. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							| 
									
										
										
										
											2020-11-08 23:25:52 +11:00
										 |  |  |   sig { params(flag: String, new_value: T.any(String, Pathname)).void } | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   def change_make_var!(flag, new_value) | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |     return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, | 
					
						
							|  |  |  |                     "#{flag}=#{new_value}", | 
					
						
							|  |  |  |                     audit_result: false) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     errors << "expected to change #{flag.inspect} to #{new_value.inspect}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Removes variable assignments completely. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							|  |  |  |   sig { params(flags: T.any(String, T::Array[String])).void } | 
					
						
							|  |  |  |   def remove_make_var!(flags) | 
					
						
							|  |  |  |     Array(flags).each do |flag| | 
					
						
							|  |  |  |       # Also remove trailing \n, if present. | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |       next if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=(?:.*\\\n)*.*$\n?/, | 
					
						
							|  |  |  |                     "", | 
					
						
							|  |  |  |                     audit_result: false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       errors << "expected to remove #{flag.inspect}" | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Finds the specified variable. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							|  |  |  |   sig { params(flag: String).returns(String) } | 
					
						
							|  |  |  |   def get_make_var(flag) | 
					
						
							| 
									
										
										
										
											2023-07-23 15:56:26 -07:00
										 |  |  |     T.must(inreplace_string[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | end |