| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 22:02:00 +02:00
										 |  |  | require "active_support/core_ext/object/blank" | 
					
						
							| 
									
										
										
										
											2017-05-29 17:51:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 17:26:27 +00:00
										 |  |  | class String | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |   # String.chomp, but if result is empty: returns nil instead. | 
					
						
							|  |  |  |   # Allows `chuzzle || foo` short-circuits. | 
					
						
							| 
									
										
										
										
											2018-09-17 22:02:00 +02:00
										 |  |  |   # TODO: Deprecate. | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |   def chuzzle | 
					
						
							|  |  |  |     s = chomp | 
					
						
							|  |  |  |     s unless s.empty? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NilClass | 
					
						
							| 
									
										
										
										
											2018-09-17 22:02:00 +02:00
										 |  |  |   # TODO: Deprecate. | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |   def chuzzle; end | 
					
						
							| 
									
										
										
										
											2010-02-27 17:26:27 +00:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  | # Used by the inreplace function (in `utils.rb`). | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  | module StringInreplaceExtension | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  |   attr_accessor :errors | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.extended(str) | 
					
						
							|  |  |  |     str.errors = [] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def sub!(before, after) | 
					
						
							| 
									
										
										
										
											2014-09-27 19:10:12 -05:00
										 |  |  |     result = super | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |     errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result | 
					
						
							| 
									
										
										
										
											2014-09-27 19:10:12 -05:00
										 |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-25 16:57:03 -08:00
										 |  |  |   # Warn if nothing was replaced | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def gsub!(before, after, audit_result = true) | 
					
						
							| 
									
										
										
										
											2014-09-27 19:10:12 -05:00
										 |  |  |     result = super(before, after) | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |     errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? | 
					
						
							| 
									
										
										
										
											2014-09-27 19:10:12 -05:00
										 |  |  |     result | 
					
						
							| 
									
										
										
										
											2012-02-25 16:57:03 -08:00
										 |  |  |   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. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def change_make_var!(flag, new_value) | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |     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. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   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. | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |       unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=(?:.*\\\n)*.*$\n?/, "", false) | 
					
						
							| 
									
										
										
										
											2019-10-20 17:14:48 -07:00
										 |  |  |         errors << "expected to remove #{flag.inspect}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Finds the specified variable | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def get_make_var(flag) | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |     self[/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*((?:.*\\\n)*.*)$/, 1] | 
					
						
							| 
									
										
										
										
											2010-11-12 21:05:35 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | end |