 37be5bea52
			
		
	
	
		37be5bea52
		
			
		
	
	
	
	
		
			
			Before this change, this would fail every time, even when available on disk (which was not the case for formulae). With this change, it will attempt to find it from a tap on disk and only fail if it cannot be found in any.
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| # typed: strict
 | |
| # frozen_string_literal: true
 | |
| 
 | |
| require "formula"
 | |
| require "cli/parser"
 | |
| 
 | |
| module Homebrew
 | |
|   extend T::Sig
 | |
| 
 | |
|   module_function
 | |
| 
 | |
|   sig { returns(CLI::Parser) }
 | |
|   def edit_args
 | |
|     Homebrew::CLI::Parser.new do
 | |
|       description <<~EOS
 | |
|         Open a <formula> or <cask> in the editor set by `EDITOR` or `HOMEBREW_EDITOR`,
 | |
|         or open the Homebrew repository for editing if no formula 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."
 | |
| 
 | |
|       conflicts "--formula", "--cask"
 | |
| 
 | |
|       named_args [:formula, :cask]
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   sig { void }
 | |
|   def edit
 | |
|     args = edit_args.parse
 | |
| 
 | |
|     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 == "subl"
 | |
|         ["--project", "#{HOMEBREW_REPOSITORY}/.sublime/homebrew.sublime-project"]
 | |
|       else
 | |
|         # If no formulae are listed, open the project root in an editor.
 | |
|         [HOMEBREW_REPOSITORY]
 | |
|       end
 | |
|     else
 | |
|       args.named.to_paths.select do |path|
 | |
|         next path if path.exist?
 | |
| 
 | |
|         not_exist_message = if args.cask?
 | |
|           "#{path.basename(".rb")} doesn't exist on disk."
 | |
|         else
 | |
|           "#{path} doesn't exist on disk."
 | |
|         end
 | |
| 
 | |
|         message = if Homebrew::EnvConfig.install_from_api?
 | |
|           <<~EOS
 | |
|             #{not_exist_message}
 | |
|             This is expected with HOMEBREW_INSTALL_FROM_API set!
 | |
|           EOS
 | |
|         elsif args.cask?
 | |
|           <<~EOS
 | |
|             #{not_exist_message}
 | |
|             Run #{Formatter.identifier("brew create --cask --set-name #{path.basename(".rb")} $URL")} \
 | |
|             to create a new cask!
 | |
|           EOS
 | |
|         else
 | |
|           <<~EOS
 | |
|             #{not_exist_message}
 | |
|             Run #{Formatter.identifier("brew create --formula --set-name #{path.basename} $URL")} \
 | |
|             to create a new formula!
 | |
|           EOS
 | |
|         end
 | |
|         raise UsageError, message
 | |
|       end.presence
 | |
|     end
 | |
| 
 | |
|     if args.print_path?
 | |
|       paths.each(&method(:puts))
 | |
|       return
 | |
|     end
 | |
| 
 | |
|     exec_editor(*paths)
 | |
|   end
 | |
| end
 |