| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  | module Utils | 
					
						
							|  |  |  |   module Inreplace | 
					
						
							|  |  |  |     def inreplace paths, before=nil, after=nil | 
					
						
							|  |  |  |       Array(paths).each do |path| | 
					
						
							| 
									
										
										
										
											2014-03-26 16:07:49 -05:00
										 |  |  |         s = File.open(path, "rb", &:read) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if before.nil? && after.nil? | 
					
						
							| 
									
										
										
										
											2014-03-26 16:07:49 -05:00
										 |  |  |           yield s.extend(StringInreplaceExtension) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2013-08-16 19:50:52 -04:00
										 |  |  |           after = after.to_s if Symbol === after | 
					
						
							| 
									
										
										
										
											2014-03-26 16:07:49 -05:00
										 |  |  |           unless s.gsub!(before, after) | 
					
						
							| 
									
										
										
										
											2013-11-12 15:42:46 +00:00
										 |  |  |             message = <<-EOS.undent
 | 
					
						
							|  |  |  |               inreplace in '#{path}' failed | 
					
						
							|  |  |  |               Expected replacement of '#{before}' with '#{after}' | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |             ARGV.homebrew_developer? ? odie(message) : opoo(message) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-26 16:07:49 -05:00
										 |  |  |         Pathname(path).atomic_write(s) | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-06-18 19:23:42 -05:00
										 |  |  |     module_function :inreplace | 
					
						
							| 
									
										
										
										
											2013-07-11 21:55:02 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |