Preliminary compatibility fixes for Ruby 3

This commit is contained in:
Bo Anderson 2022-10-08 01:08:15 +01:00
parent f30f68be7d
commit 9b99594753
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
16 changed files with 99 additions and 54 deletions

View File

@ -11,8 +11,10 @@ module Cask
class StageOnly < AbstractArtifact
extend T::Sig
def self.from_args(cask, *args)
raise CaskInvalidError.new(cask.token, "'stage_only' takes only a single argument: true") if args != [true]
def self.from_args(cask, *args, **kwargs)
if args != [true] || kwargs.present?
raise CaskInvalidError.new(cask.token, "'stage_only' takes only a single argument: true")
end
new(cask, true)
end

View File

@ -0,0 +1,22 @@
# typed: false
# frozen_string_literal: true
require "set"
module Cask
# Sorted set containing all cask artifacts.
#
# @api private
class ArtifactSet < ::Set
def each(&block)
return enum_for(__method__) { size } unless block
to_a.each(&block)
self
end
def to_a
super.sort
end
end
end

View File

@ -33,8 +33,9 @@ module Cask
Cmd.parser do
instance_eval(&block) if block
OPTIONS.each do |option|
send(*option)
OPTIONS.map(&:dup).each do |option|
kwargs = option.pop
send(*option, **kwargs)
end
end
end

View File

@ -24,8 +24,9 @@ module Cask
switch "--force",
description: "Force overwriting existing files."
OPTIONS.each do |option|
send(*option)
OPTIONS.map(&:dup).each do |option|
kwargs = option.pop
send(*option, **kwargs)
end
instance_eval(&block) if block

View File

@ -6,6 +6,7 @@ require "lazy_object"
require "livecheck"
require "cask/artifact"
require "cask/artifact_set"
require "cask/caskroom"
require "cask/exceptions"
@ -207,14 +208,14 @@ module Cask
end
# @api public
def appcast(*args)
set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
def appcast(*args, **kwargs)
set_unique_stanza(:appcast, args.empty? && kwargs.empty?) { DSL::Appcast.new(*args, **kwargs) }
end
# @api public
def container(*args)
set_unique_stanza(:container, args.empty?) do
DSL::Container.new(*args)
def container(**kwargs)
set_unique_stanza(:container, kwargs.empty?) do
DSL::Container.new(**kwargs)
end
end
@ -261,12 +262,12 @@ module Cask
# `depends_on` uses a load method so that multiple stanzas can be merged.
# @api public
def depends_on(*args)
def depends_on(**kwargs)
@depends_on ||= DSL::DependsOn.new
return @depends_on if args.empty?
return @depends_on if kwargs.empty?
begin
@depends_on.load(*args)
@depends_on.load(**kwargs)
rescue RuntimeError => e
raise CaskInvalidError.new(cask, e)
end
@ -274,13 +275,13 @@ module Cask
end
# @api public
def conflicts_with(*args)
def conflicts_with(**kwargs)
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
set_unique_stanza(:conflicts_with, kwargs.empty?) { DSL::ConflictsWith.new(**kwargs) }
end
def artifacts
@artifacts ||= SortedSet.new
@artifacts ||= ArtifactSet.new
end
def caskroom_path
@ -337,13 +338,13 @@ module Cask
end
ORDINARY_ARTIFACT_CLASSES.each do |klass|
define_method(klass.dsl_key) do |*args|
define_method(klass.dsl_key) do |*args, **kwargs|
if [*artifacts.map(&:class), klass].include?(Artifact::StageOnly) &&
(artifacts.map(&:class) & ACTIVATABLE_ARTIFACT_CLASSES).any?
raise CaskInvalidError.new(cask, "'stage_only' must be the only activatable artifact.")
end
artifacts.add(klass.from_args(cask, *args))
artifacts.add(klass.from_args(cask, *args, **kwargs))
rescue CaskInvalidError
raise
rescue => e

View File

@ -16,7 +16,7 @@ module Cask
attr_accessor(*VALID_KEYS, :pairs)
def initialize(pairs = {})
def initialize(**pairs)
@pairs = pairs
pairs.each do |key, value|
raise "invalid container key: #{key.inspect}" unless VALID_KEYS.include?(key)

View File

@ -156,6 +156,12 @@ module Cask
version { gsub(DIVIDER_REGEX, "") }
end
# @api public
sig { params(separator: T.nilable(String)).returns(T.self_type) }
def chomp(separator = nil)
version { to_s.chomp(T.unsafe(separator)) }
end
private
sig { returns(T.self_type) }

View File

@ -367,8 +367,9 @@ module Homebrew
end
def cask_options
self.class.global_cask_options.each do |method, *args, **options|
send(method, *args, **options)
self.class.global_cask_options.each do |args|
options = args.pop
send(*args, **options)
conflicts "--formula", args.last
end
@cask_options = true

View File

@ -121,16 +121,18 @@ module Homebrew
[:switch, "--overwrite", {
description: "Delete files that already exist in the prefix while linking.",
}],
].each do |*args, **options|
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--cask", args.last
end
formula_options
[
[:switch, "--cask", "--casks", { description: "Treat all named arguments as casks." }],
*Cask::Cmd::AbstractCommand::OPTIONS,
*Cask::Cmd::Install::OPTIONS,
].each do |*args, **options|
*Cask::Cmd::AbstractCommand::OPTIONS.map(&:dup),
*Cask::Cmd::Install::OPTIONS.map(&:dup),
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--formula", args.last
end

View File

@ -68,18 +68,20 @@ module Homebrew
[:switch, "-g", "--git", {
description: "Create a Git repository, useful for creating patches to the software.",
}],
].each do |options|
send(*options)
conflicts "--cask", options[-2]
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--cask", args.last
end
formula_options
[
[:switch, "--cask", "--casks", { description: "Treat all named arguments as casks." }],
*Cask::Cmd::AbstractCommand::OPTIONS,
*Cask::Cmd::Install::OPTIONS,
].each do |options|
send(*options)
conflicts "--formula", options[-2]
*Cask::Cmd::AbstractCommand::OPTIONS.map(&:dup),
*Cask::Cmd::Install::OPTIONS.map(&:dup),
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--formula", args.last
end
cask_options

View File

@ -76,9 +76,10 @@ module Homebrew
env: :display_install_times,
description: "Print install times for each package at the end of the run.",
}],
].each do |options|
send(*options)
conflicts "--cask", options[-2]
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--cask", args.last
end
formula_options
[
@ -86,11 +87,12 @@ module Homebrew
description: "Treat all named arguments as casks. If no named arguments " \
"are specified, upgrade only outdated casks.",
}],
*Cask::Cmd::AbstractCommand::OPTIONS,
*Cask::Cmd::Upgrade::OPTIONS,
].each do |options|
send(*options)
conflicts "--formula", options[-2]
*Cask::Cmd::AbstractCommand::OPTIONS.map(&:dup),
*Cask::Cmd::Upgrade::OPTIONS.map(&:dup),
].each do |args|
options = args.pop
send(*args, **options)
conflicts "--formula", args.last
end
cask_options

View File

@ -594,7 +594,7 @@ class CurlGitHubPackagesDownloadStrategy < CurlDownloadStrategy
# GitHub Packages authorization header.
# HOMEBREW_GITHUB_PACKAGES_AUTH set in brew.sh
meta[:headers] << "Authorization: #{HOMEBREW_GITHUB_PACKAGES_AUTH}"
super(url, name, version, meta)
super(url, name, version, **meta)
end
private

View File

@ -107,7 +107,7 @@ class SoftwareSpec
def url(val = nil, specs = {})
return @resource.url if val.nil?
@resource.url(val, specs)
@resource.url(val, **specs)
dependency_collector.add(@resource)
end
@ -467,10 +467,11 @@ class Bottle
image_name = GitHubPackages.image_formula_name(@name)
image_tag = GitHubPackages.image_version_rebuild(version_rebuild)
resource.url("#{root_url}/#{image_name}/manifests/#{image_tag}", {
resource.url(
"#{root_url}/#{image_name}/manifests/#{image_tag}",
using: CurlGitHubPackagesDownloadStrategy,
headers: ["Accept: application/vnd.oci.image.index.v1+json"],
})
)
resource.downloader.resolved_basename = "#{name}-#{version_rebuild}.bottle_manifest.json"
resource
end
@ -501,7 +502,7 @@ class Bottle
filename = Filename.create(resource.owner, @tag, @spec.rebuild)
path, resolved_basename = Utils::Bottles.path_resolved_basename(val, name, resource.checksum, filename)
@resource.url("#{val}/#{path}", select_download_strategy(specs))
@resource.url("#{val}/#{path}", **select_download_strategy(specs))
@resource.downloader.resolved_basename = resolved_basename if resolved_basename.present?
end
end

View File

@ -24,12 +24,12 @@ class SystemCommand
module Mixin
extend T::Sig
def system_command(*args)
T.unsafe(SystemCommand).run(*args)
def system_command(executable, **options)
SystemCommand.run(executable, **options)
end
def system_command!(*args)
T.unsafe(SystemCommand).run!(*args)
def system_command!(command, **options)
SystemCommand.run!(command, **options)
end
end
@ -37,11 +37,11 @@ class SystemCommand
extend Predicable
def self.run(executable, **options)
T.unsafe(self).new(executable, **options).run!
new(executable, **options).run!
end
def self.run!(command, **options)
T.unsafe(self).run(command, **options, must_succeed: true)
run(command, **options, must_succeed: true)
end
sig { returns(SystemCommand::Result) }

View File

@ -10,7 +10,7 @@ describe User do
describe "#gui?" do
before do
allow(SystemCommand).to receive(:run).with("who")
allow(SystemCommand).to receive(:run).with("who", {})
.and_return([who_output, "", instance_double(Process::Status, success?: true)])
end

View File

@ -119,6 +119,10 @@ module Homebrew
end
def install_bundler!
if Gem.ruby_version >= Gem::Version.new("2.7")
raise "Installing and using Bundler is currently only supported on Ruby 2.6."
end
setup_gem_environment!(gem_home: gem_user_dir, gem_bindir: gem_user_bindir)
install_gem_setup_path!(
"bundler",