brew/Library/Homebrew/cask/lib/hbc/qualified_token.rb

40 lines
1.5 KiB
Ruby
Raw Normal View History

2016-09-24 13:52:43 +02:00
module Hbc
module QualifiedToken
REPO_PREFIX = "homebrew-".freeze
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
# per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121
USER_REGEX = /[a-z0-9_\-]+/
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
# per https://github.com/Homebrew/homebrew/blob/4c7bc9ec3bca729c898ee347b6135ba692ee0274/Library/Homebrew/cmd/tap.rb#L121
REPO_REGEX = /(?:#{REPO_PREFIX})?\w+/
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
# per https://github.com/caskroom/homebrew-cask/blob/master/CONTRIBUTING.md#generating-a-token-for-the-cask
TOKEN_REGEX = /[a-z0-9\-]+/
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
TAP_REGEX = %r{#{USER_REGEX}[/\-]#{REPO_REGEX}}
2016-08-18 22:11:42 +03:00
2016-10-08 13:25:38 +02:00
QUALIFIED_TOKEN_REGEX = %r{#{TAP_REGEX}/#{TOKEN_REGEX}}
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.parse(arg)
return nil unless arg.is_a?(String) && arg.downcase =~ /^#{QUALIFIED_TOKEN_REGEX}$/
2016-09-24 13:52:43 +02:00
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
2016-09-24 13:52:43 +02:00
user, repo, token = path_elements
end
repo.sub!(/^#{REPO_PREFIX}/, "")
2016-09-24 13:52:43 +02:00
odebug "[user, repo, token] might be [#{user}, #{repo}, #{token}]"
[user, repo, token]
2016-08-18 22:11:42 +03:00
end
end
end