| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | require "utils/string_inreplace_extension" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  | module Utils | 
					
						
							| 
									
										
										
										
											2020-08-19 08:18:14 +02:00
										 |  |  |   # Helper functions for replacing text in files in-place. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							|  |  |  |   module Inreplace | 
					
						
							| 
									
										
										
										
											2022-07-30 19:10:40 +02:00
										 |  |  |     # Error during text replacement. | 
					
						
							| 
									
										
										
										
											2020-08-19 08:18:14 +02:00
										 |  |  |     class Error < RuntimeError | 
					
						
							|  |  |  |       def initialize(errors) | 
					
						
							|  |  |  |         formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)| | 
					
						
							|  |  |  |           s << "#{path}:\n" << errs.map { |e| "  #{e}\n" }.join | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         super formatted_errors.freeze | 
					
						
							| 
									
										
										
										
											2016-09-11 17:47:04 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  |     module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-29 10:56:24 +01:00
										 |  |  |     # Sometimes we have to change a bit before we install. Mostly we | 
					
						
							| 
									
										
										
										
											2021-01-09 13:34:17 +11:00
										 |  |  |     # prefer a patch, but if you need the {Formula#prefix prefix} of | 
					
						
							|  |  |  |     # this formula in the patch you have to resort to `inreplace`, | 
					
						
							|  |  |  |     # because in the patch you don't have access to any variables | 
					
						
							|  |  |  |     # defined by the formula, as only `HOMEBREW_PREFIX` is available | 
					
						
							|  |  |  |     # in the {DATAPatch embedded patch}. | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2023-04-05 10:06:45 -07:00
										 |  |  |     # @example `inreplace` supports regular expressions: | 
					
						
							|  |  |  |     #   inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool" | 
					
						
							| 
									
										
										
										
											2020-08-19 08:18:14 +02:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2023-04-05 10:06:45 -07:00
										 |  |  |     # @example `inreplace` supports blocks: | 
					
						
							|  |  |  |     #   inreplace "Makefile" do |s| | 
					
						
							|  |  |  |     #     s.gsub! "/usr/local", HOMEBREW_PREFIX.to_s | 
					
						
							|  |  |  |     #   end | 
					
						
							| 
									
										
										
										
											2021-01-09 13:34:17 +11:00
										 |  |  |     # | 
					
						
							|  |  |  |     # @see StringInreplaceExtension | 
					
						
							| 
									
										
										
										
											2020-08-19 08:18:14 +02:00
										 |  |  |     # @api public | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |     sig { | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       params( | 
					
						
							| 
									
										
										
										
											2020-11-20 14:20:38 +01:00
										 |  |  |         paths:        T.any(T::Array[T.untyped], String, Pathname), | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         before:       T.nilable(T.any(Regexp, String)), | 
					
						
							|  |  |  |         after:        T.nilable(T.any(String, Symbol)), | 
					
						
							|  |  |  |         audit_result: T::Boolean, | 
					
						
							|  |  |  |       ).void | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-19 17:12:32 +01:00
										 |  |  |     def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter | 
					
						
							| 
									
										
										
										
											2020-09-19 01:53:59 +02:00
										 |  |  |       after = after.to_s if after.is_a? Symbol | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  |       errors = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 19:10:15 +00:00
										 |  |  |       errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |       Array(paths).each do |path| | 
					
						
							| 
									
										
										
										
											2021-12-23 14:58:51 -05:00
										 |  |  |         str = File.binread(path) | 
					
						
							| 
									
										
										
										
											2020-07-25 00:48:15 +05:30
										 |  |  |         s = StringInreplaceExtension.new(str) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if before.nil? && after.nil? | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  |           yield s | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           s.gsub!(T.must(before), after, audit_result) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 22:01:32 +08:00
										 |  |  |         errors[path] = s.errors unless s.errors.empty? | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 00:48:15 +05:30
										 |  |  |         Pathname(path).atomic_write(s.inreplace_string) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-09-28 01:08:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 16:18:06 +02:00
										 |  |  |       raise Utils::Inreplace::Error, errors if errors.present? | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-09 13:34:17 +11:00
										 |  |  |     # @api private | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  |     def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) | 
					
						
							| 
									
										
										
										
											2021-12-23 14:58:51 -05:00
										 |  |  |       str = File.binread(path) | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  |       contents = StringInreplaceExtension.new(str) | 
					
						
							|  |  |  |       replacement_pairs.each do |old, new| | 
					
						
							|  |  |  |         ohai "replace #{old.inspect} with #{new.inspect}" unless silent | 
					
						
							|  |  |  |         unless old | 
					
						
							|  |  |  |           contents.errors << "No old value for new value #{new}! Did you pass the wrong arguments?" | 
					
						
							|  |  |  |           next | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         contents.gsub!(old, new) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-08-08 16:18:06 +02:00
										 |  |  |       raise Utils::Inreplace::Error, path => contents.errors if contents.errors.present? | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run | 
					
						
							|  |  |  |       contents.inreplace_string | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |