| 
									
										
										
										
											2023-07-23 15:56:26 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Used by the `inreplace` function (in `utils.rb`). | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @api private | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2020-10-15 22:18:40 +11:00
										 |  |  |   sig { params(before: T.any(Regexp, String), after: String).returns(T.nilable(String)) } | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   def sub!(before, after) | 
					
						
							|  |  |  |     result = inreplace_string.sub!(before, after) | 
					
						
							|  |  |  |     errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result | 
					
						
							|  |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Same as `String#gsub!`, but warns if nothing was replaced. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api public | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |   sig { | 
					
						
							| 
									
										
										
										
											2023-07-23 21:14:21 -07:00
										 |  |  |     params(before: T.any(Pathname, Regexp, String), after: T.any(Pathname, String), audit_result: T::Boolean) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       .returns(T.nilable(String)) | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   def gsub!(before, after, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |     return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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. | 
					
						
							|  |  |  |       unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=(?:.*\\\n)*.*$\n?/, "", false) | 
					
						
							|  |  |  |         errors << "expected to remove #{flag.inspect}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     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 |