| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | require "formula" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class BumpRevision < AbstractCommand | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Create a commit to increment the revision of <formula>. If no revision is | 
					
						
							|  |  |  |           present, "revision 1" will be added. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "-n", "--dry-run", | 
					
						
							|  |  |  |                description: "Print what would be done rather than doing it." | 
					
						
							|  |  |  |         switch "--remove-bottle-block", | 
					
						
							|  |  |  |                description: "Remove the bottle block in addition to bumping the revision." | 
					
						
							|  |  |  |         switch "--write-only", | 
					
						
							|  |  |  |                description: "Make the expected file modifications without taking any Git actions." | 
					
						
							|  |  |  |         flag   "--message=", | 
					
						
							|  |  |  |                description: "Append <message> to the default commit message." | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |         conflicts "--dry-run", "--write-only" | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |         named_args :formula, min: 1, without_api: true | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         # As this command is simplifying user-run commands then let's just use a | 
					
						
							|  |  |  |         # user path, too. | 
					
						
							|  |  |  |         ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 10:39:35 +01:00
										 |  |  |         Homebrew.install_bundler_gems!(groups: ["ast"]) unless args.dry_run? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |         args.named.to_formulae.each do |formula| | 
					
						
							|  |  |  |           current_revision = formula.revision | 
					
						
							|  |  |  |           new_revision = current_revision + 1
 | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |           if args.dry_run? | 
					
						
							|  |  |  |             unless args.quiet? | 
					
						
							|  |  |  |               old_text = "revision #{current_revision}" | 
					
						
							|  |  |  |               new_text = "revision #{new_revision}" | 
					
						
							|  |  |  |               if current_revision.zero? | 
					
						
							|  |  |  |                 ohai "add #{new_text.inspect}" | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 ohai "replace #{old_text.inspect} with #{new_text.inspect}" | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2020-08-15 10:43:29 +02:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |             require "utils/ast" | 
					
						
							| 
									
										
										
										
											2021-01-06 09:11:34 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |             formula_ast = Utils::AST::FormulaAST.new(formula.path.read) | 
					
						
							|  |  |  |             if current_revision.zero? | 
					
						
							|  |  |  |               formula_ast.add_stanza(:revision, new_revision) | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               formula_ast.replace_stanza(:revision, new_revision) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             formula_ast.remove_stanza(:bottle) if args.remove_bottle_block? | 
					
						
							|  |  |  |             formula.path.atomic_write(formula_ast.process) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:42:40 -07:00
										 |  |  |           message = "#{formula.name}: revision bump #{args.message}" | 
					
						
							|  |  |  |           if args.dry_run? | 
					
						
							|  |  |  |             ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}" | 
					
						
							|  |  |  |           elsif !args.write_only? | 
					
						
							|  |  |  |             formula.path.parent.cd do | 
					
						
							|  |  |  |               safe_system "git", "commit", "--no-edit", "--verbose", | 
					
						
							|  |  |  |                           "--message=#{message}", "--", formula.path | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-08-15 10:43:29 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2019-03-31 19:06:29 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |