aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/download_strategy.rb
diff options
context:
space:
mode:
authorMasayuki Morita2017-01-08 16:44:54 +0900
committerMasayuki Morita2017-01-08 16:44:54 +0900
commit335be35acf805a2853a6fe92b06d9a643616f463 (patch)
tree84882384d264a46d4fe0ddcae70040dd181370ee /Library/Homebrew/download_strategy.rb
parenta4330f458a09e946ef7bac912fba63628dbf67ca (diff)
downloadbrew-335be35acf805a2853a6fe92b06d9a643616f463.tar.bz2
Generalize GitHubReleaseDownloadStrategy in order to support archive URL
Diffstat (limited to 'Library/Homebrew/download_strategy.rb')
-rw-r--r--Library/Homebrew/download_strategy.rb81
1 files changed, 57 insertions, 24 deletions
diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb
index 19af8820c..3e028949b 100644
--- a/Library/Homebrew/download_strategy.rb
+++ b/Library/Homebrew/download_strategy.rb
@@ -532,42 +532,78 @@ class S3DownloadStrategy < CurlDownloadStrategy
end
end
-# GitHubReleaseDownloadStrategy downloads tarballs from GitHub Release assets.
-# To use it, add ":using => GitHubReleaseDownloadStrategy" to the URL section
-# of your formula. This download strategy uses GitHub access tokens (in the
-# environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request.
-# This strategy is suitable for corporate use just like S3DownloadStrategy,
-# because it lets you use a private GttHub repository for internal distribution.
-# It works with public one, but in that case simply use CurlDownloadStrategy.
-class GitHubReleaseDownloadStrategy < CurlDownloadStrategy
- require "utils/formatter"
- require 'utils/github'
-
+# GitHubPrivateRepositoryDownloadStrategy downloads contents from GitHub
+# Private Repository. To use it, add
+# ":using => GitHubPrivateRepositoryDownloadStrategy" to the URL section of
+# your formula. This download strategy uses GitHub access tokens (in the
+# environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request. This
+# strategy is suitable for corporate use just like S3DownloadStrategy, because
+# it lets you use a private GttHub repository for internal distribution. It
+# works with public one, but in that case simply use CurlDownloadStrategy.
+class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
def initialize(name, resource)
super
+ set_github_token
+ parse_url_pattern
+ end
+
+ def parse_url_pattern
+ url_pattern = %r|https://github.com/([^/]+)/([^/]+)/(\S+)|
+ unless @url =~ url_pattern
+ raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository."
+ end
+
+ _, @owner, @repo, @filepath = *(@url.match(url_pattern))
+ end
+
+ def download_url
+ "https://#{@github_token}@github.com/#{@owner}/#{@repo}/#{@filepath}"
+ end
+
+ def _fetch
+ curl download_url, "-C", downloaded_size, "-o", temporary_path
+ end
+
+ private
+ def set_github_token
@github_token = ENV["HOMEBREW_GITHUB_API_TOKEN"]
- raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required." unless @github_token
+ unless @github_token
+ raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required."
+ end
+ end
+end
- url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)|
- raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern
+# GitHubPrivateRepositoryReleaseDownloadStrategy downloads tarballs from GitHub
+# Release assets. To use it, add
+# ":using => GitHubPrivateRepositoryReleaseDownloadStrategy" to the URL section
+# of your formula. This download strategy uses GitHub access tokens (in the
+# environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request.
+class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy
+ require "utils/formatter"
+ require 'utils/github'
+
+ def parse_url_pattern
+ url_pattern = %r|https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)|
+ unless @url =~ url_pattern
+ raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release."
+ end
_, @owner, @repo, @tag, @filename = *(@url.match(url_pattern))
end
+ def download_url
+ "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}"
+ end
+
def _fetch
- puts "Download asset_id: #{asset_id}"
# HTTP request header `Accept: application/octet-stream` is required.
# Without this, the GitHub API will respond with metadata, not binary.
- curl asset_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream'
+ curl download_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream'
end
private
- def asset_url
- "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}"
- end
-
def asset_id
@asset_id ||= resolve_asset_id
end
@@ -580,11 +616,8 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy
return assets.first["id"]
end
- def release_url
- "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}"
- end
-
def fetch_release_metadata
+ release_url = "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}"
GitHub.open(release_url)
end
end