Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more type signatures. #8956

Merged
merged 1 commit into from Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions Library/.rubocop.yml
Expand Up @@ -297,15 +297,9 @@ Sorbet/ConstantsFromStrings:
Style/AccessModifierDeclarations:
Enabled: false

# don't group nicely documented or private attr_readers
# Conflicts with type signatures on `attr_*`s.
Style/AccessorGrouping:
Exclude:
- 'Homebrew/formula.rb'
- 'Homebrew/formulary.rb'
- 'Homebrew/migrator.rb'
- 'Homebrew/resource.rb'
- 'Homebrew/system_command.rb'
- 'Homebrew/tap.rb'
Enabled: false

# make rspec formatting more flexible
Style/BlockDelimiters:
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/bintray.rb
Expand Up @@ -8,13 +8,16 @@
#
# @api private
class Bintray
extend T::Sig

include Context

API_URL = "https://api.bintray.com"

class Error < RuntimeError
end

sig { returns(String) }
def inspect
"#<Bintray: org=#{@bintray_org}>"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/abstract_artifact.rb
Expand Up @@ -7,6 +7,8 @@ module Artifact
#
# @api private
class AbstractArtifact
extend T::Sig

include Comparable
extend Predicable

Expand Down Expand Up @@ -132,6 +134,7 @@ def config
cask.config
end

sig { returns(String) }
def to_s
"#{summarize} (#{self.class.english_name})"
end
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/artifact/abstract_uninstall.rb
Expand Up @@ -15,6 +15,8 @@ module Artifact
#
# @api private
class AbstractUninstall < AbstractArtifact
extend T::Sig

ORDERED_DIRECTIVES = [
:early_script,
:launchctl,
Expand Down Expand Up @@ -53,6 +55,7 @@ def to_h
directives.to_h
end

sig { returns(String) }
def summarize
to_h.flat_map { |key, val| Array(val).map { |v| "#{key.inspect} => #{v.inspect}" } }.join(", ")
end
Expand Down Expand Up @@ -128,6 +131,7 @@ def running_processes(bundle_id)
end
end

sig { returns(String) }
def automation_access_instructions
"Enable Automation Access for “Terminal > System Events” in " \
"“System Preferences > Security > Privacy > Automation” if you haven't already."
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/artifact.rb
Expand Up @@ -12,6 +12,9 @@ module Artifact
#
# @api private
class Artifact < Moved
extend T::Sig

sig { returns(String) }
def self.english_name
"Generic Artifact"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/mdimporter.rb
Expand Up @@ -9,6 +9,9 @@ module Artifact
#
# @api private
class Mdimporter < Moved
extend T::Sig

sig { returns(String) }
def self.english_name
"Spotlight metadata importer"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/moved.rb
Expand Up @@ -9,6 +9,9 @@ module Artifact
#
# @api private
class Moved < Relocated
extend T::Sig

sig { returns(String) }
def self.english_description
"#{english_name}s"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/prefpane.rb
Expand Up @@ -9,6 +9,9 @@ module Artifact
#
# @api private
class Prefpane < Moved
extend T::Sig

sig { returns(String) }
def self.english_name
"Preference Pane"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/qlplugin.rb
Expand Up @@ -9,6 +9,9 @@ module Artifact
#
# @api private
class Qlplugin < Moved
extend T::Sig

sig { returns(String) }
def self.english_name
"QuickLook Plugin"
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/artifact/relocated.rb
Expand Up @@ -12,6 +12,8 @@ module Artifact
#
# @api private
class Relocated < AbstractArtifact
extend T::Sig

def self.from_args(cask, *args)
source_string, target_hash = args

Expand Down Expand Up @@ -49,6 +51,7 @@ def to_a
end
end

sig { returns(String) }
def summarize
target_string = @target_string.empty? ? "" : " -> #{@target_string}"
"#{@source_string}#{target_string}"
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/artifact/stage_only.rb
Expand Up @@ -9,16 +9,20 @@ module Artifact
#
# @api private
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]

new(cask)
end

sig { returns(T::Array[T::Boolean]) }
def to_a
[true]
end

sig { returns(String) }
def summarize
"true"
end
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/artifact/suite.rb
Expand Up @@ -9,10 +9,14 @@ module Artifact
#
# @api private
class Suite < Moved
extend T::Sig

sig { returns(String) }
def self.english_name
"App Suite"
end

sig { returns(Symbol) }
def self.dirmethod
:appdir
end
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/artifact/symlinked.rb
Expand Up @@ -9,10 +9,14 @@ module Artifact
#
# @api private
class Symlinked < Relocated
extend T::Sig

sig { returns(String) }
def self.link_type_english_name
"Symlink"
end

sig { returns(String) }
def self.english_description
"#{english_name} #{link_type_english_name}s"
end
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/audit.rb
Expand Up @@ -13,6 +13,8 @@ module Cask
#
# @api private
class Audit
extend T::Sig

extend Predicable

attr_reader :cask, :download
Expand Down Expand Up @@ -117,6 +119,7 @@ def result
end
end

sig { returns(String) }
def summary
summary = ["audit for #{cask}: #{result}"]

Expand Down Expand Up @@ -416,6 +419,7 @@ def core_formula_names
core_tap.formula_names
end

sig { returns(String) }
def core_formula_url
"#{core_tap.default_remote}/blob/HEAD/Formula/#{cask.token}.rb"
end
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/cask.rb
Expand Up @@ -12,6 +12,8 @@ module Cask
#
# @api private
class Cask
extend T::Sig

extend Enumerable
extend Forwardable
extend Searchable
Expand Down Expand Up @@ -64,6 +66,7 @@ def config=(config)
define_method(method_name) { |&block| @dsl.send(method_name, &block) }
end

sig { returns(T::Array[[String, String]]) }
def timestamped_versions
Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*"))
.map { |p| p.relative_path_from(p.parent.parent) }
Expand All @@ -89,6 +92,7 @@ def installed?
!versions.empty?
end

sig { returns(T.nilable(Time)) }
def install_time
_, time = timestamped_versions.last
return unless time
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/cask/cask_loader.rb
Expand Up @@ -90,6 +90,8 @@ def cask(header_token, **options, &block)

# Loads a cask from a URI.
class FromURILoader < FromPathLoader
extend T::Sig

def self.can_load?(ref)
uri_regex = ::URI::DEFAULT_PARSER.make_regexp
return false unless ref.to_s.match?(Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options))
Expand All @@ -103,6 +105,7 @@ def self.can_load?(ref)

attr_reader :url

sig { params(url: T.any(URI::Generic, String)).void }
def initialize(url)
@url = URI(url)
super Cache.path/File.basename(@url.path)
Expand Down Expand Up @@ -177,10 +180,13 @@ def load(config:)

# Pseudo-loader which raises an error when trying to load the corresponding cask.
class NullLoader < FromPathLoader
extend T::Sig

def self.can_load?(*)
true
end

sig { params(ref: T.any(String, Pathname)).void }
def initialize(ref)
token = File.basename(ref, ".rb")
super CaskLoader.default_path(token)
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/cmd.rb
Expand Up @@ -38,6 +38,8 @@ module Cask
#
# @api private
class Cmd
extend T::Sig

include Context

ALIASES = {
Expand All @@ -61,6 +63,7 @@ class Cmd
Cmd::Upgrade => "brew upgrade --cask",
}.freeze

sig { returns(String) }
def self.description
max_command_length = Cmd.commands.map(&:length).max

Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/cask/cmd/--cache.rb
Expand Up @@ -7,18 +7,24 @@ class Cmd
#
# @api private
class Cache < AbstractCommand
extend T::Sig

sig { override.returns(T.nilable(T.any(Integer, Symbol))) }
def self.min_named
:cask
end

sig { returns(String) }
def self.description
"Display the file used to cache a <cask>."
end

sig { returns(String) }
def self.command_name
"--cache"
end

sig { void }
def run
casks.each do |cask|
puts self.class.cached_location(cask)
Expand Down
14 changes: 13 additions & 1 deletion Library/Homebrew/cask/cmd/abstract_command.rb
Expand Up @@ -9,16 +9,22 @@ class Cmd
#
# @api private
class AbstractCommand
extend T::Sig
extend T::Helpers

include Homebrew::Search

sig { returns(T.nilable(T.any(Integer, Symbol))) }
def self.min_named
nil
end

sig { returns(T.nilable(Integer)) }
def self.max_named
nil
end

sig { returns(String) }
def self.banner_args
if min_named == :cask && max_named != 1
" <cask>"
Expand All @@ -29,6 +35,7 @@ def self.banner_args
end
end

sig { returns(String) }
def self.banner_headline
"`#{command_name}` [<options>]#{banner_args}"
end
Expand Down Expand Up @@ -72,24 +79,29 @@ def self.parser(&block)
end
end

sig { returns(String) }
def self.command_name
@command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
end

sig { returns(T::Boolean) }
def self.abstract?
name.split("::").last.match?(/^Abstract[^a-z]/)
end

sig { returns(T::Boolean) }
def self.visible?
true
end

sig { returns(String) }
def self.help
parser.generate_help_text
end

sig { returns(String) }
def self.short_description
description.split(".").first
description[/\A[^.]*\./]
end

def self.run(*args)
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cask/cmd/abstract_internal_command.rb
Expand Up @@ -7,10 +7,14 @@ class Cmd
#
# @api private
class AbstractInternalCommand < AbstractCommand
extend T::Sig

sig { returns(String) }
def self.command_name
super.sub(/^internal_/i, "_")
end

sig { returns(T::Boolean) }
def self.visible?
false
end
Expand Down