286 lines
8.1 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "source_location"
2023-09-06 09:55:17 -04:00
require "utils/curl"
module Cask
# Class corresponding to the `url` stanza.
class URL < Delegator
class DSL
attr_reader :uri, :specs,
:verified, :using,
:tag, :branch, :revisions, :revision,
:trust_cert, :cookies, :referer, :header, :user_agent,
:data, :only_path
extend Forwardable
def_delegators :uri, :path, :scheme, :to_s
2021-03-31 06:14:41 +02:00
sig {
params(
uri: T.any(URI::Generic, String),
# @api public
verified: T.nilable(String),
# @api public
using: T.any(Class, Symbol, NilClass),
# @api public
tag: T.nilable(String),
# @api public
branch: T.nilable(String),
# @api public
revisions: T.nilable(T::Array[String]),
# @api public
revision: T.nilable(String),
# @api public
trust_cert: T.nilable(T::Boolean),
# @api public
cookies: T.nilable(T::Hash[String, String]),
referer: T.nilable(T.any(URI::Generic, String)),
# @api public
header: T.nilable(T.any(String, T::Array[String])),
user_agent: T.nilable(T.any(Symbol, String)),
# @api public
data: T.nilable(T::Hash[String, String]),
# @api public
only_path: T.nilable(String),
).void
}
def initialize(
uri,
verified: nil,
using: nil,
tag: nil,
branch: nil,
revisions: nil,
revision: nil,
trust_cert: nil,
cookies: nil,
referer: nil,
header: nil,
user_agent: nil,
data: nil,
only_path: nil
)
@uri = URI(uri)
header = Array(header) unless header.nil?
specs = {}
specs[:verified] = @verified = verified
specs[:using] = @using = using
specs[:tag] = @tag = tag
specs[:branch] = @branch = branch
specs[:revisions] = @revisions = revisions
specs[:revision] = @revision = revision
specs[:trust_cert] = @trust_cert = trust_cert
specs[:cookies] = @cookies = cookies
specs[:referer] = @referer = referer
specs[:headers] = @header = header
specs[:user_agent] = @user_agent = user_agent || :default
specs[:data] = @data = data
specs[:only_path] = @only_path = only_path
@specs = specs.compact
end
end
class BlockDSL
# Allow accessing the URL associated with page contents.
module PageWithURL
# Get the URL of the fetched page.
#
# ### Example
#
# ```ruby
# url "https://example.org/download" do |page|
# file = page[/href="([^"]+.dmg)"/, 1]
# URL.join(page.url, file)
# end
# ```
#
# @api public
sig { returns(URI::Generic) }
attr_accessor :url
end
sig {
params(
uri: T.nilable(T.any(URI::Generic, String)),
dsl: ::Cask::DSL,
2023-04-29 03:09:50 +02:00
block: T.proc.params(arg0: T.all(String, PageWithURL))
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
).void
}
def initialize(uri, dsl:, &block)
@uri = uri
@dsl = dsl
@block = block
end
2023-04-29 03:09:50 +02:00
sig { returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])) }
def call
if @uri
2023-09-06 09:55:17 -04:00
result = ::Utils::Curl.curl_output("--fail", "--silent", "--location", @uri)
result.assert_success!
page = result.stdout
page.extend PageWithURL
page.url = URI(@uri)
instance_exec(page, &@block)
else
instance_exec(&@block)
end
end
# Allows calling a nested `url` stanza in a `url do` block.
#
# @api public
sig {
params(
uri: T.any(URI::Generic, String),
2023-04-29 03:09:50 +02:00
block: T.proc.params(arg0: T.all(String, PageWithURL))
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
).returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash]))
}
def url(uri, &block)
self.class.new(uri, dsl: @dsl, &block).call
end
private :url
# This allows calling DSL methods from inside a `url` block.
#
# @api public
def method_missing(method, *args, &block)
if @dsl.respond_to?(method)
2024-08-18 16:28:17 -07:00
@dsl.public_send(method, *args, &block)
else
super
end
end
private :method_missing
def respond_to_missing?(method, include_all)
@dsl.respond_to?(method, include_all) || super
end
private :respond_to_missing?
end
2021-03-31 06:14:41 +02:00
sig {
params(
uri: T.nilable(T.any(URI::Generic, String)),
verified: T.nilable(String),
using: T.any(Class, Symbol, NilClass),
tag: T.nilable(String),
branch: T.nilable(String),
revisions: T.nilable(T::Array[String]),
revision: T.nilable(String),
trust_cert: T.nilable(T::Boolean),
cookies: T.nilable(T::Hash[String, String]),
referer: T.nilable(T.any(URI::Generic, String)),
2023-11-09 10:23:13 +11:00
header: T.nilable(T.any(String, T::Array[String])),
user_agent: T.nilable(T.any(Symbol, String)),
data: T.nilable(T::Hash[String, String]),
only_path: T.nilable(String),
caller_location: Thread::Backtrace::Location,
dsl: T.nilable(::Cask::DSL),
2023-04-29 03:09:50 +02:00
block: T.nilable(
T.proc.params(arg0: T.all(String, BlockDSL::PageWithURL))
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
),
2021-03-31 06:14:41 +02:00
).void
}
def initialize(
uri = nil,
2021-03-31 06:14:41 +02:00
verified: nil,
using: nil,
tag: nil,
branch: nil,
revisions: nil,
revision: nil,
trust_cert: nil,
cookies: nil,
referer: nil,
header: nil,
user_agent: nil,
data: nil,
only_path: nil,
caller_location: T.must(caller_locations).fetch(0),
dsl: nil,
&block
2021-03-31 06:14:41 +02:00
)
super(
if block
LazyObject.new do
uri2, options = *BlockDSL.new(uri, dsl: T.must(dsl), &block).call
2023-04-29 03:09:50 +02:00
options ||= {}
DSL.new(uri2, **options)
end
else
DSL.new(
T.must(uri),
2024-03-07 16:20:20 +00:00
verified:,
using:,
tag:,
branch:,
revisions:,
revision:,
trust_cert:,
cookies:,
referer:,
header:,
user_agent:,
data:,
only_path:,
)
end
)
2021-03-31 06:14:41 +02:00
@from_block = !block.nil?
@caller_location = caller_location
2021-03-31 06:14:41 +02:00
end
def __getobj__
@dsl
2021-03-31 06:14:41 +02:00
end
def __setobj__(dsl)
2021-03-31 06:14:41 +02:00
@dsl = dsl
end
sig { returns(Homebrew::SourceLocation) }
def location
Homebrew::SourceLocation.new(@caller_location.lineno, raw_url_line&.index("url"))
end
sig { returns(T.nilable(String)) }
def raw_url_line
return @raw_url_line if defined?(@raw_url_line)
2021-03-31 06:14:41 +02:00
2023-11-05 08:55:58 -08:00
@raw_url_line = Pathname(T.must(@caller_location.path))
.each_line
.drop(@caller_location.lineno - 1)
.first
2021-03-31 06:14:41 +02:00
end
private :raw_url_line
2021-03-31 06:14:41 +02:00
sig { params(ignore_major_version: T::Boolean).returns(T::Boolean) }
def unversioned?(ignore_major_version: false)
interpolated_url = raw_url_line&.then { |line| line[/url\s+"([^"]+)"/, 1] }
2021-03-31 06:14:41 +02:00
return false unless interpolated_url
2016-08-18 22:11:42 +03:00
interpolated_url = interpolated_url.gsub(/\#{\s*version\s*\.major\s*}/, "") if ignore_major_version
interpolated_url.exclude?('#{')
end
2020-12-12 06:01:26 +01:00
sig { returns(T::Boolean) }
def from_block?
@from_block
end
2020-12-12 06:01:26 +01:00
end
2016-08-18 22:11:42 +03:00
end