hbc/qualified_token: simplify token parsing
- Stop supporting archaic "user-repo/token" syntax - Move regex for parsing tapped Cask token to tap_constants
This commit is contained in:
		
							parent
							
								
									512a0c950e
								
							
						
					
					
						commit
						44f1354d63
					
				| @ -1,37 +1,11 @@ | ||||
| module Hbc | ||||
|   module QualifiedToken | ||||
|     REPO_PREFIX = "homebrew-".freeze | ||||
| 
 | ||||
|     # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 | ||||
|     USER_REGEX = /[a-z0-9_\-]+/ | ||||
| 
 | ||||
|     # per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121 | ||||
|     REPO_REGEX = /(?:#{REPO_PREFIX})?\w+/ | ||||
| 
 | ||||
|     # per https://github.com/caskroom/homebrew-cask/blob/master/CONTRIBUTING.md#generating-a-token-for-the-cask | ||||
|     TOKEN_REGEX = /[a-z0-9\-]+/ | ||||
| 
 | ||||
|     TAP_REGEX = %r{#{USER_REGEX}[/\-]#{REPO_REGEX}} | ||||
| 
 | ||||
|     QUALIFIED_TOKEN_REGEX = %r{#{TAP_REGEX}/#{TOKEN_REGEX}} | ||||
| 
 | ||||
|     def self.parse(arg) | ||||
|       return nil unless arg.is_a?(String) && arg.downcase =~ /^#{QUALIFIED_TOKEN_REGEX}$/ | ||||
|       path_elements = arg.downcase.split("/") | ||||
|       if path_elements.count == 2 | ||||
|         # eg phinze-cask/google-chrome. | ||||
|         # Not certain this form is needed, but it was supported in the past. | ||||
|         token = path_elements[1] | ||||
|         dash_elements = path_elements[0].split("-") | ||||
|         repo = dash_elements.pop | ||||
|         dash_elements.pop if dash_elements.count > 1 && dash_elements[-1] + "-" == REPO_PREFIX | ||||
|         user = dash_elements.join("-") | ||||
|       else | ||||
|         # eg caskroom/cask/google-chrome | ||||
|         # per https://github.com/Homebrew/brew/blob/master/docs/brew-tap.md | ||||
|         user, repo, token = path_elements | ||||
|       end | ||||
|       repo.sub!(/^#{REPO_PREFIX}/, "") | ||||
|       return nil unless arg.is_a?(String) | ||||
|       return nil unless arg.downcase =~ HOMEBREW_TAP_CASK_REGEX | ||||
|       # eg caskroom/cask/google-chrome | ||||
|       # per https://github.com/Homebrew/brew/blob/master/docs/brew-tap.md | ||||
|       user, repo, token = arg.downcase.split("/") | ||||
|       odebug "[user, repo, token] might be [#{user}, #{repo}, #{token}]" | ||||
|       [user, repo, token] | ||||
|     end | ||||
|  | ||||
| @ -1,5 +1,7 @@ | ||||
| # match taps' formulae, e.g. someuser/sometap/someformula | ||||
| HOMEBREW_TAP_FORMULA_REGEX = %r{^([\w-]+)/([\w-]+)/([\w+-.@]+)$} | ||||
| # match taps' casks, e.g. someuser/sometap/somecask | ||||
| HOMEBREW_TAP_CASK_REGEX = %r{^([\w-]+)/([\w-]+)/([a-z0-9\-]+)$} | ||||
| # match taps' directory paths, e.g. HOMEBREW_LIBRARY/Taps/someuser/sometap | ||||
| HOMEBREW_TAP_DIR_REGEX = %r{#{Regexp.escape(HOMEBREW_LIBRARY.to_s)}/Taps/([\w-]+)/([\w-]+)} | ||||
| # match taps' formula paths, e.g. HOMEBREW_LIBRARY/Taps/someuser/sometap/someformula | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Josh Hagins
						Josh Hagins