| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "fileutils" | 
					
						
							|  |  |  | require "pathname" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module Bundle | 
					
						
							|  |  |  |     module Dumper | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       private_class_method def self.can_write_to_brewfile?(brewfile_path, force: false) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         raise "#{brewfile_path} already exists" if should_not_write_file?(brewfile_path, overwrite: force) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.build_brewfile(describe:, no_restart:, brews:, taps:, casks:, mas:, whalebrew:, vscode:) | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/tap_dumper" | 
					
						
							| 
									
										
										
										
											2025-03-24 19:38:23 +01:00
										 |  |  |         require "bundle/brew_dumper" | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/cask_dumper" | 
					
						
							|  |  |  |         require "bundle/mac_app_store_dumper" | 
					
						
							|  |  |  |         require "bundle/whalebrew_dumper" | 
					
						
							|  |  |  |         require "bundle/vscode_extension_dumper" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         content = [] | 
					
						
							|  |  |  |         content << TapDumper.dump if taps | 
					
						
							|  |  |  |         content << BrewDumper.dump(describe:, no_restart:) if brews | 
					
						
							|  |  |  |         content << CaskDumper.dump(describe:) if casks | 
					
						
							|  |  |  |         content << MacAppStoreDumper.dump if mas | 
					
						
							|  |  |  |         content << WhalebrewDumper.dump if whalebrew | 
					
						
							|  |  |  |         content << VscodeExtensionDumper.dump if vscode | 
					
						
							|  |  |  |         "#{content.reject(&:empty?).join("\n")}\n" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.dump_brewfile(global:, file:, describe:, force:, no_restart:, brews:, taps:, casks:, mas:, whalebrew:, | 
					
						
							|  |  |  |                              vscode:) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         path = brewfile_path(global:, file:) | 
					
						
							|  |  |  |         can_write_to_brewfile?(path, force:) | 
					
						
							|  |  |  |         content = build_brewfile(describe:, no_restart:, taps:, brews:, casks:, mas:, whalebrew:, vscode:) | 
					
						
							|  |  |  |         write_file path, content | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.brewfile_path(global: false, file: nil) | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/brewfile" | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         Brewfile.path(dash_writes_to_stdout: true, global:, file:) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       private_class_method def self.should_not_write_file?(file, overwrite: false) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         file.exist? && !overwrite && file.to_s != "/dev/stdout" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.write_file(file, content) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         Bundle.exchange_uid_if_needed! do | 
					
						
							|  |  |  |           file.open("w") { |io| io.write content } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |