| 
									
										
										
										
											2020-11-25 17:03:23 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class Edit < AbstractCommand | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							| 
									
										
										
										
											2025-06-03 11:06:35 -04:00
										 |  |  |           Open a <formula>, <cask> or <tap> in the editor set by `$EDITOR` or `$HOMEBREW_EDITOR`, | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |           or open the Homebrew repository for editing if no argument is provided. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Treat all named arguments as casks." | 
					
						
							|  |  |  |         switch "--print-path", | 
					
						
							|  |  |  |                description: "Print the file path to be edited, without opening an editor." | 
					
						
							| 
									
										
										
										
											2023-09-27 12:39:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |         conflicts "--formula", "--cask" | 
					
						
							| 
									
										
										
										
											2023-09-27 12:39:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |         named_args [:formula, :cask, :tap], without_api: true | 
					
						
							| 
									
										
										
										
											2023-09-25 08:09:03 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         ENV["COLORTERM"] = ENV.fetch("HOMEBREW_COLORTERM", nil) | 
					
						
							| 
									
										
										
										
											2024-09-05 15:49:11 +08:00
										 |  |  |         # Recover $TMPDIR for emacsclient | 
					
						
							|  |  |  |         ENV["TMPDIR"] = ENV.fetch("HOMEBREW_TMPDIR", nil) | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         unless (HOMEBREW_REPOSITORY/".git").directory? | 
					
						
							|  |  |  |           odie <<~EOS | 
					
						
							|  |  |  |             Changes will be lost! | 
					
						
							|  |  |  |             The first time you `brew update`, all local changes will be lost; you should | 
					
						
							|  |  |  |             thus `brew update` before you `brew edit`! | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         paths = if args.named.empty? | 
					
						
							|  |  |  |           # Sublime requires opting into the project editing path, | 
					
						
							|  |  |  |           # as opposed to VS Code which will infer from the .vscode path | 
					
						
							|  |  |  |           if which_editor(silent: true) == "subl" | 
					
						
							| 
									
										
										
										
											2024-08-20 16:30:43 -07:00
										 |  |  |             ["--project", HOMEBREW_REPOSITORY/".sublime/homebrew.sublime-project"] | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |           else | 
					
						
							|  |  |  |             # If no formulae are listed, open the project root in an editor. | 
					
						
							|  |  |  |             [HOMEBREW_REPOSITORY] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           expanded_paths = args.named.to_paths | 
					
						
							|  |  |  |           expanded_paths.each do |path| | 
					
						
							|  |  |  |             raise_with_message!(path, args.cask?) unless path.exist? | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           expanded_paths | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if args.print_path? | 
					
						
							|  |  |  |           paths.each { puts _1 } | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exec_editor(*paths) | 
					
						
							| 
									
										
										
										
											2024-06-02 16:05:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 13:49:46 +03:00
										 |  |  |         is_formula = T.let(false, T::Boolean) | 
					
						
							| 
									
										
										
										
											2025-04-11 16:07:29 +01:00
										 |  |  |         if !Homebrew::EnvConfig.no_env_hints? && paths.any? do |path| | 
					
						
							| 
									
										
										
										
											2024-08-21 08:51:04 +01:00
										 |  |  |              next if path == "--project" | 
					
						
							| 
									
										
										
										
											2024-08-20 16:30:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 22:24:23 +03:00
										 |  |  |              is_formula = core_formula_path?(path) | 
					
						
							| 
									
										
										
										
											2025-04-02 23:13:39 +03:00
										 |  |  |              is_formula || core_cask_path?(path) || core_formula_tap?(path) || core_cask_tap?(path) | 
					
						
							| 
									
										
										
										
											2024-06-02 16:05:08 +03:00
										 |  |  |            end | 
					
						
							| 
									
										
										
										
											2025-04-02 22:24:23 +03:00
										 |  |  |           from_source = " --build-from-source" if is_formula | 
					
						
							| 
									
										
										
										
											2025-04-11 17:14:21 +03:00
										 |  |  |           no_api = "HOMEBREW_NO_INSTALL_FROM_API=1 " unless Homebrew::EnvConfig.no_install_from_api? | 
					
						
							| 
									
										
										
										
											2025-04-02 09:12:06 +03:00
										 |  |  |           puts <<~EOS | 
					
						
							|  |  |  |             To test your local edits, run: | 
					
						
							| 
									
										
										
										
											2025-04-11 17:14:21 +03:00
										 |  |  |               #{no_api}brew install#{from_source} --verbose --debug #{args.named.join(" ")} | 
					
						
							| 
									
										
										
										
											2024-06-02 16:05:08 +03:00
										 |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-09-25 08:09:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2018-03-24 19:38:34 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							|  |  |  |       def core_formula_path?(path) | 
					
						
							|  |  |  |         path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-10-30 00:01:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							|  |  |  |       def core_cask_path?(path) | 
					
						
							|  |  |  |         path.fnmatch?("**/homebrew-cask/Casks/**.rb", File::FNM_DOTMATCH) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-08-01 15:56:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							|  |  |  |       def core_formula_tap?(path) | 
					
						
							|  |  |  |         path == CoreTap.instance.path | 
					
						
							| 
									
										
										
										
											2021-04-02 15:36:29 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							|  |  |  |       def core_cask_tap?(path) | 
					
						
							|  |  |  |         path == CoreCaskTap.instance.path | 
					
						
							| 
									
										
										
										
											2023-09-25 13:09:11 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |       sig { params(path: Pathname, cask: T::Boolean).returns(T.noreturn) } | 
					
						
							|  |  |  |       def raise_with_message!(path, cask) | 
					
						
							|  |  |  |         name = path.basename(".rb").to_s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (tap_match = Regexp.new("#{HOMEBREW_TAP_DIR_REGEX.source}$").match(path.to_s)) | 
					
						
							|  |  |  |           raise TapUnavailableError, CoreTap.instance.name if core_formula_tap?(path) | 
					
						
							|  |  |  |           raise TapUnavailableError, CoreCaskTap.instance.name if core_cask_tap?(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           raise TapUnavailableError, "#{tap_match[:user]}/#{tap_match[:repo]}" | 
					
						
							|  |  |  |         elsif cask || core_cask_path?(path) | 
					
						
							| 
									
										
										
										
											2025-08-12 17:04:15 -04:00
										 |  |  |           if !CoreCaskTap.instance.installed? && Homebrew::API.cask_tokens.include?(name) | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |             command = "brew tap --force #{CoreCaskTap.instance.name}" | 
					
						
							|  |  |  |             action = "tap #{CoreCaskTap.instance.name}" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             command = "brew create --cask --set-name #{name} $URL" | 
					
						
							|  |  |  |             action = "create a new cask" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         elsif core_formula_path?(path) && | 
					
						
							|  |  |  |               !CoreTap.instance.installed? && | 
					
						
							| 
									
										
										
										
											2025-08-12 17:04:15 -04:00
										 |  |  |               Homebrew::API.formula_names.include?(name) | 
					
						
							| 
									
										
										
										
											2024-03-21 08:18:52 -07:00
										 |  |  |           command = "brew tap --force #{CoreTap.instance.name}" | 
					
						
							|  |  |  |           action = "tap #{CoreTap.instance.name}" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           command = "brew create --set-name #{name} $URL" | 
					
						
							|  |  |  |           action = "create a new formula" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         raise UsageError, <<~EOS | 
					
						
							|  |  |  |           #{name} doesn't exist on disk. | 
					
						
							|  |  |  |           Run #{Formatter.identifier(command)} to #{action}! | 
					
						
							| 
									
										
										
										
											2023-09-26 10:08:54 +03:00
										 |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2023-09-24 21:05:37 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-10-13 10:27:12 -06:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-24 14:45:01 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |