mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Refactor FormulaCreator args and call parse_url automatically
Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
parent
d7d8c61f00
commit
85d48da364
@ -184,16 +184,15 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
fc = FormulaCreator.new(
|
fc = FormulaCreator.new(
|
||||||
args.set_name,
|
|
||||||
args.set_version,
|
|
||||||
tap: args.tap,
|
|
||||||
url: args.named.fetch(0),
|
url: args.named.fetch(0),
|
||||||
|
name: args.set_name,
|
||||||
|
version: args.set_version,
|
||||||
|
tap: args.tap,
|
||||||
mode:,
|
mode:,
|
||||||
license: args.set_license,
|
license: args.set_license,
|
||||||
fetch: !args.no_fetch?,
|
fetch: !args.no_fetch?,
|
||||||
head: args.HEAD?,
|
head: args.HEAD?,
|
||||||
)
|
)
|
||||||
fc.parse_url
|
|
||||||
# ask for confirmation if name wasn't passed explicitly
|
# ask for confirmation if name wasn't passed explicitly
|
||||||
if args.set_name.blank?
|
if args.set_name.blank?
|
||||||
print "Formula name [#{fc.name}]: "
|
print "Formula name [#{fc.name}]: "
|
||||||
@ -205,7 +204,7 @@ module Homebrew
|
|||||||
# Check for disallowed formula, or names that shadow aliases,
|
# Check for disallowed formula, or names that shadow aliases,
|
||||||
# unless --force is specified.
|
# unless --force is specified.
|
||||||
unless args.force?
|
unless args.force?
|
||||||
if (reason = MissingFormula.disallowed_reason(fc.name))
|
if (reason = MissingFormula.disallowed_reason(T.must(fc.name)))
|
||||||
odie <<~EOS
|
odie <<~EOS
|
||||||
The formula '#{fc.name}' is not allowed to be created.
|
The formula '#{fc.name}' is not allowed to be created.
|
||||||
#{reason}
|
#{reason}
|
||||||
@ -229,7 +228,7 @@ module Homebrew
|
|||||||
|
|
||||||
formula = Homebrew.with_no_api_env do
|
formula = Homebrew.with_no_api_env do
|
||||||
CoreTap.instance.clear_cache
|
CoreTap.instance.clear_cache
|
||||||
Formula[fc.name]
|
Formula[T.must(fc.name)]
|
||||||
end
|
end
|
||||||
PyPI.update_python_resources! formula, ignore_non_pypi_packages: true if args.python?
|
PyPI.update_python_resources! formula, ignore_non_pypi_packages: true if args.python?
|
||||||
|
|
||||||
|
@ -7,21 +7,30 @@ require "erb"
|
|||||||
module Homebrew
|
module Homebrew
|
||||||
# Class for generating a formula from a template.
|
# Class for generating a formula from a template.
|
||||||
class FormulaCreator
|
class FormulaCreator
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Version)) }
|
||||||
|
attr_reader :version
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
|
attr_reader :head
|
||||||
|
|
||||||
sig {
|
sig {
|
||||||
params(name: T.nilable(String), version: T.nilable(String), tap: T.nilable(String), url: String,
|
params(url: String, name: T.nilable(String), version: T.nilable(String), tap: T.nilable(String),
|
||||||
mode: T.nilable(Symbol), license: T.nilable(String), fetch: T::Boolean, head: T::Boolean).void
|
mode: T.nilable(Symbol), license: T.nilable(String), fetch: T::Boolean, head: T::Boolean).void
|
||||||
}
|
}
|
||||||
def initialize(name, version, tap:, url:, mode:, license:, fetch:, head:)
|
def initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false)
|
||||||
@name = name
|
|
||||||
@version = Version.new(version) if version
|
|
||||||
@tap = Tap.fetch(tap || "homebrew/core")
|
|
||||||
@url = url
|
@url = url
|
||||||
@mode = mode
|
@name = name.presence
|
||||||
@license = license
|
@version = T.let(Version.new(version), T.nilable(Version)) if version.present?
|
||||||
|
@tap = T.let(Tap.fetch(tap.presence || "homebrew/core"), Tap)
|
||||||
|
@mode = T.let(mode.presence, T.nilable(Symbol))
|
||||||
|
@license = T.let(license.presence, T.nilable(String))
|
||||||
@fetch = fetch
|
@fetch = fetch
|
||||||
@head = head
|
@head = head
|
||||||
|
|
||||||
|
parse_url
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { void }
|
sig { void }
|
||||||
@ -29,14 +38,15 @@ module Homebrew
|
|||||||
raise TapUnavailableError, @tap.name unless @tap.installed?
|
raise TapUnavailableError, @tap.name unless @tap.installed?
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { params(url: String).returns(T.nilable(String)) }
|
sig { void }
|
||||||
def self.name_from_url(url)
|
def parse_url
|
||||||
stem = Pathname.new(url).stem
|
@name ||= begin
|
||||||
|
stem = Pathname.new(@url).stem
|
||||||
|
name = if stem.start_with? "index.cgi"
|
||||||
# special cases first
|
# special cases first
|
||||||
if stem.start_with? "index.cgi"
|
|
||||||
# gitweb URLs e.g. http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary
|
# gitweb URLs e.g. http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary
|
||||||
stem.rpartition("=").last
|
stem.rpartition("=").last
|
||||||
elsif url =~ %r{github\.com/\S+/(\S+)/(archive|releases)/}
|
elsif @url =~ %r{github\.com/\S+/(\S+)/(archive|releases)/}
|
||||||
# e.g. https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz
|
# e.g. https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz
|
||||||
Regexp.last_match(1)
|
Regexp.last_match(1)
|
||||||
else
|
else
|
||||||
@ -44,13 +54,11 @@ module Homebrew
|
|||||||
pathver = Version.parse(stem).to_s
|
pathver = Version.parse(stem).to_s
|
||||||
stem.sub(/[-_.]?#{Regexp.escape(pathver)}$/, "")
|
stem.sub(/[-_.]?#{Regexp.escape(pathver)}$/, "")
|
||||||
end
|
end
|
||||||
|
odebug "name from url: #{name}"
|
||||||
|
name
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { void }
|
@version ||= Version.detect(@url)
|
||||||
def parse_url
|
|
||||||
@name = FormulaCreator.name_from_url(@url) if @name.blank?
|
|
||||||
odebug "name_from_url: #{@name}"
|
|
||||||
@version = Version.detect(@url) if @version.nil?
|
|
||||||
|
|
||||||
case @url
|
case @url
|
||||||
when %r{github\.com/(\S+)/(\S+)\.git}
|
when %r{github\.com/(\S+)/(\S+)\.git}
|
||||||
@ -91,7 +99,7 @@ module Homebrew
|
|||||||
raise "Downloaded URL is not archive"
|
raise "Downloaded URL is not archive"
|
||||||
end
|
end
|
||||||
|
|
||||||
@sha256 = filepath.sha256
|
@sha256 = T.let(filepath.sha256, T.nilable(String))
|
||||||
end
|
end
|
||||||
|
|
||||||
if @github
|
if @github
|
||||||
|
@ -3,28 +3,52 @@
|
|||||||
require "formula_creator"
|
require "formula_creator"
|
||||||
|
|
||||||
RSpec.describe Homebrew::FormulaCreator do
|
RSpec.describe Homebrew::FormulaCreator do
|
||||||
it "gets name from GitHub archive URL" do
|
describe ".new" do
|
||||||
t = described_class.name_from_url("https://github.com/abitrolly/lapce/archive/v0.3.0.tar.gz")
|
tests = {
|
||||||
expect(t).to eq("lapce")
|
"generic tarball URL": {
|
||||||
end
|
url: "http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz",
|
||||||
|
expected: {
|
||||||
|
name: "synscan",
|
||||||
|
version: "5.02",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"gitweb URL": {
|
||||||
|
url: "http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary",
|
||||||
|
expected: {
|
||||||
|
name: "libzipper",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"GitHub repo URL": {
|
||||||
|
url: "https://github.com/abitrolly/lapce.git",
|
||||||
|
expected: {
|
||||||
|
name: "lapce",
|
||||||
|
head: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"GitHub archive URL": {
|
||||||
|
url: "https://github.com/abitrolly/lapce/archive/v0.3.0.tar.gz",
|
||||||
|
expected: {
|
||||||
|
name: "lapce",
|
||||||
|
version: "0.3.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"GitHub download URL": {
|
||||||
|
url: "https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz",
|
||||||
|
expected: {
|
||||||
|
name: "stella",
|
||||||
|
version: "6.7",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
it "gets name from gitweb URL" do
|
tests.each do |description, test|
|
||||||
t = described_class.name_from_url("http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary")
|
it "parses #{description}" do
|
||||||
expect(t).to eq("libzipper")
|
fc = described_class.new(url: test.fetch(:url))
|
||||||
end
|
ex = test.fetch(:expected)
|
||||||
|
expect(fc.name).to eq(ex[:name]) if ex.key?(:name)
|
||||||
it "gets name from GitHub repo URL" do
|
expect(fc.version).to eq(ex[:version]) if ex.key?(:version)
|
||||||
t = described_class.name_from_url("https://github.com/abitrolly/lapce.git")
|
expect(fc.head).to eq(ex[:head]) if ex.key?(:head)
|
||||||
expect(t).to eq("lapce")
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gets name from GitHub download URL" do
|
|
||||||
t = described_class.name_from_url("https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz")
|
|
||||||
expect(t).to eq("stella")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "gets name from generic tarball URL" do
|
|
||||||
t = described_class.name_from_url("http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz")
|
|
||||||
expect(t).to eq("synscan")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user