Ruby 自制-自定义下载策略不起作用
我试图在自制软件中创建一个自定义下载策略,但tap不起作用 在我尝试执行brew tap ericofusco/test后,下面显示了错误 我按照以下说明操作:Ruby 自制-自定义下载策略不起作用,ruby,homebrew,Ruby,Homebrew,我试图在自制软件中创建一个自定义下载策略,但tap不起作用 在我尝试执行brew tap ericofusco/test后,下面显示了错误 我按照以下说明操作: 似乎它期望我有一个类名和我的文件名 Error: Invalid formula: /usr/local/Homebrew/Library/Taps/ericofusco/homebrew-test/custom_download_strategy.rb No available formula with the name "custo
似乎它期望我有一个类名和我的文件名
Error: Invalid formula: /usr/local/Homebrew/Library/Taps/ericofusco/homebrew-test/custom_download_strategy.rb
No available formula with the name "custom_download_strategy"
In formula file: /usr/local/Homebrew/Library/Taps/ericofusco/homebrew-test/custom_download_strategy.rb
Expected to find class CustomDownloadStrategy, but only found: CustomGitHubPrivateRepositoryDownloadStrategy, CustomGitHubPrivateRepositoryReleaseDownloadStrategy (not derived from Formula!).
Error: Cannot tap ericofusco/test: invalid syntax in tap!
formula.rb
require_relative "../custom_download_strategy"
class Test < Formula
desc ""
homepage "https://github.com/ericofusco/test"
url "https://github.com/ericofusco/test/releases/download/v1.0.0/test_1.0.0_darwin_amd64.tar.gz", :using => CustomGitHubPrivateRepositoryReleaseDownloadStrategy
version "1.0.0"
sha256 "2436b4c63020b343f0b667c2f6797681c71d2598fa600691b7fd9593fd9ca7ee"
def install
bin.install "test"
end
end
require\u relative./自定义\u下载\u策略
类测试<公式
描述“
主页“https://github.com/ericofusco/test"
url“https://github.com/ericofusco/test/releases/download/v1.0.0/test_1.0.0_darwin_amd64.tar.gz,:使用=>CustomGitHubPrivateRepositoryReleaseDownloadStrategy
版本“1.0.0”
sha256“2436b4c63020b343f0b667c2f6797681c71d2598fa600691b7fd9593fd9ca7ee”
def安装
bin.install“test”
结束
结束
自定义_下载_strategy.rb
require "download_strategy"
# GitHubPrivateRepositoryDownloadStrategy downloads contents from GitHub
# Private Repository. To use it, add
# `:using => :github_private_repo` 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 GitHub repository for internal distribution. It
# works with public one, but in that case simply use CurlDownloadStrategy.
class CustomGitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
require "utils/formatter"
require "utils/github"
def initialize(url, name, version, **meta)
super
parse_url_pattern
set_github_token
end
def parse_url_pattern
unless match = url.match(%r{https://github.com/([^/]+)/([^/]+)/(\S+)})
raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository."
end
_, @owner, @repo, @filepath = *match
end
def download_url
"https://github.com/#{@owner}/#{@repo}/#{@filepath}"
end
private
def _fetch(url:, resolved_url:)
curl_download download_url, "--header", "Authorization: token #{@github_token}", to: temporary_path
end
def set_github_token
@github_token = ENV["HOMEBREW_GITHUB_API_TOKEN"]
unless @github_token
raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required."
end
validate_github_repository_access!
end
def validate_github_repository_access!
# Test access to the repository
GitHub.repository(@owner, @repo)
rescue GitHub::HTTPNotFoundError
# We only handle HTTPNotFoundError here,
# becase AuthenticationFailedError is handled within util/github.
message = <<~EOS
HOMEBREW_GITHUB_API_TOKEN can not access the repository: #{@owner}/#{@repo}
This token may not have permission to access the repository or the url of formula may be incorrect.
EOS
raise CurlDownloadStrategyError, message
end
end
# GitHubPrivateRepositoryReleaseDownloadStrategy downloads tarballs from GitHub
# Release assets. To use it, add `:using => :github_private_release` 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 CustomGitHubPrivateRepositoryReleaseDownloadStrategy < CustomGitHubPrivateRepositoryDownloadStrategy
require 'net/http'
def initialize(url, name, version, **meta)
super
end
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}"
#blah = curl_output "--header", "Accept: application/octet-stream", "--header", "Authorization: token #{@github_token}", "-I"
uri = URI("https://api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}")
req = Net::HTTP::Get.new(uri)
req['Accept'] = 'application/octet-stream'
req['Authorization'] = "token #{@github_token}"
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
res['location']
end
private
def _fetch(url:, resolved_url:)
# HTTP request header `Accept: application/octet-stream` is required.
# Without this, the GitHub API will respond with metadata, not binary.
curl_download download_url, "--header", "Accept: application/octet-stream", to: temporary_path
end
def asset_id
@asset_id ||= resolve_asset_id
end
def resolve_asset_id
release_metadata = fetch_release_metadata
assets = release_metadata["assets"].select { |a| a["name"] == @filename }
raise CurlDownloadStrategyError, "Asset file not found." if assets.empty?
assets.first["id"]
end
def fetch_release_metadata
release_url = "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}"
GitHub.open_api(release_url)
end
end
需要“下载策略”
#GitHubPrivateRepositoryDownloadStrategy从GitHub下载内容
#私有存储库。要使用它,请添加
#`:using=>:github\u private\u repo`到
#你的公式。此下载策略使用GitHub访问令牌(在
#环境变量“HOMEBREW\u GITHUB\u API\u TOKEN”)对请求进行签名。这
#战略适合企业使用,就像S3DownloadStrategy一样,因为
#它允许您使用私有GitHub存储库进行内部分发。信息技术
#与public one配合使用,但在这种情况下,只需使用CurlDownloadStrategy即可。
类CustomGitHubPrivateRepositoryDownloadStrategy
您需要将自定义下载策略.rb
移动到tap存储库的子文件夹中,例如,在自制测试/lib/custom下载策略.rb
中,并相应地更改require\u relative
调用
默认情况下,存储库根目录中的所有
.rb
文件以及公式
和自制公式
子文件夹都假定为有效的自制公式。详细信息。此外,不建议使用公式和容器中的相对位置,例如要求相对“./自定义下载策略”
当卸载
,重新安装
等时,找不到“自定义下载策略”的问题。例如: 因此,建议使用类似的绝对路径:
require_relative HOMEBREW_LIBRARY/"Tap/<User>/<Repository>/lib/custom_download_strategy.rb"
require_relative HOMEBREW_LIBRARY/“点击///lib/custom_download_strategy.rb”
“它似乎希望我有一个类名和我的文件名。”是的,看起来就是这样。更改类名和/或文件名以匹配是否解决了问题?它没有解决问题,而是导致了另一个错误。当我以文件名命名类时,它似乎是homebrew调用的模块或具有错误参数的东西。require\u relative
确实有效。但是,如果您想更明确地使用require
,而不使用硬编码路径,那么您可以在cask内执行require“{@cask.tap.path}/lib/custom_download_strategy”
。@DanielBayley Putrequire“{@cask.tap.path}/lib/custom_download_strategy”
在cask xxx do
之后执行,brew安装
正常,但brew卸载
错误错误:Cask'xxxxx'不可读:nil:NILCASS的未定义方法“路径”
啊,是的,我后来遇到了这个问题……这是因为Cask的快照被保存到/usr/local/Caskroom/.metadata/*/Casks/.rb
,这是为卸载读取的文件,由于某些原因,它无法访问@cask.tap.path
。另一种选择是执行require“#{Tap.fetch(“/”).path}/lib/custom_download_strategy”
。我目前的方法是引入环境变量作为引用,以避免在重命名它们之后必须批量修改的问题。在*.rd
中使用require ENV[“自制\自定义\下载\策略”]
,然后运行echo“导出自制\自定义\下载\策略=$(brew--r