brew/Library/Homebrew/system_config.rb

203 lines
5.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 "hardware"
require "software_spec"
require "development_tools"
require "extend/ENV"
require "system_command"
require "git_repository"
2020-08-19 07:35:50 +02:00
# Helper module for querying information about the system configuration.
module SystemConfig
class << self
include SystemCommand::Mixin
def clang
@clang ||= if DevelopmentTools.installed?
DevelopmentTools.clang_version
else
Version::NULL
end
end
def clang_build
@clang_build ||= if DevelopmentTools.installed?
DevelopmentTools.clang_build_version
else
Version::NULL
end
end
2023-04-15 19:06:40 -07:00
sig { returns(GitRepository) }
def homebrew_repo
2023-04-15 19:06:40 -07:00
GitRepository.new(HOMEBREW_REPOSITORY)
end
2024-11-24 11:58:12 +08:00
sig { returns(String) }
def branch
homebrew_repo.branch_name || "(none)"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def head
2023-04-15 16:46:13 -07:00
homebrew_repo.head_ref || "(none)"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def last_commit
2023-04-15 16:46:13 -07:00
homebrew_repo.last_committed || "never"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def origin
2023-04-15 16:46:13 -07:00
homebrew_repo.origin_url || "(none)"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def describe_clang
return "N/A" if clang.null?
if clang_build.null?
clang.to_s
else
"#{clang} build #{clang_build}"
end
end
def describe_path(path)
return "N/A" if path.nil?
2018-09-17 02:45:00 +02:00
realpath = path.realpath
if realpath == path
path
else
"#{path} => #{realpath}"
end
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def describe_homebrew_ruby
"#{RUBY_VERSION} => #{RUBY_PATH}"
end
2020-10-20 12:03:48 +02:00
sig { returns(T.nilable(String)) }
def hardware
return if Hardware::CPU.type == :dunno
2018-09-17 02:45:00 +02:00
"CPU: #{Hardware.cores_as_words}-core #{Hardware::CPU.bits}-bit #{Hardware::CPU.family}"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def kernel
`uname -m`.chomp
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def describe_git
2020-08-23 06:32:26 +02:00
return "N/A" unless Utils::Git.available?
2018-09-17 02:45:00 +02:00
2020-08-23 06:32:26 +02:00
"#{Utils::Git.version} => #{Utils::Git.path}"
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def describe_curl
out, = system_command(Utils::Curl.curl_executable, args: ["--version"], verbose: false)
2018-08-29 19:56:32 +02:00
2023-04-15 15:06:58 -07:00
match_data = /^curl (?<curl_version>[\d.]+)/.match(out)
if match_data
"#{match_data[:curl_version]} => #{Utils::Curl.curl_path}"
2018-08-29 19:56:32 +02:00
else
"N/A"
end
end
def dump_tap_config(tap, out = $stdout)
case tap
when CoreTap
tap_name = "Core tap"
json_file_name = "formula.jws.json"
when CoreCaskTap
tap_name = "Core cask tap"
json_file_name = "cask.jws.json"
else
raise ArgumentError, "Unknown tap: #{tap}"
end
if tap.installed?
out.puts "#{tap_name} origin: #{tap.remote}" if tap.remote != tap.default_remote
out.puts "#{tap_name} HEAD: #{tap.git_head || "(none)"}"
out.puts "#{tap_name} last commit: #{tap.git_last_commit || "never"}"
default_branches = %w[main master].freeze
out.puts "#{tap_name} branch: #{tap.git_branch || "(none)"}" if default_branches.exclude?(tap.git_branch)
end
json_file = Homebrew::API::HOMEBREW_CACHE_API/json_file_name
if json_file.exist?
out.puts "#{tap_name} JSON: #{json_file.mtime.utc.strftime("%d %b %H:%M UTC")}"
elsif !tap.installed?
out.puts "#{tap_name}: N/A"
end
end
def core_tap_config(out = $stdout)
dump_tap_config(CoreTap.instance, out)
end
def homebrew_config(out = $stdout)
out.puts "HOMEBREW_VERSION: #{HOMEBREW_VERSION}"
out.puts "ORIGIN: #{origin}"
out.puts "HEAD: #{head}"
out.puts "Last commit: #{last_commit}"
2024-11-24 11:58:12 +08:00
out.puts "Branch: #{branch}"
end
def homebrew_env_config(out = $stdout)
out.puts "HOMEBREW_PREFIX: #{HOMEBREW_PREFIX}"
2020-04-05 15:44:50 +01:00
{
HOMEBREW_REPOSITORY: Homebrew::DEFAULT_REPOSITORY,
HOMEBREW_CELLAR: Homebrew::DEFAULT_CELLAR,
}.freeze.each do |key, default|
value = Object.const_get(key)
out.puts "#{key}: #{value}" if value.to_s != default.to_s
end
2020-04-05 15:44:50 +01:00
Homebrew::EnvConfig::ENVS.each do |env, hash|
method_name = Homebrew::EnvConfig.env_method_name(env, hash)
if hash[:boolean]
out.puts "#{env}: set" if Homebrew::EnvConfig.send(method_name)
2020-04-05 15:44:50 +01:00
next
end
value = Homebrew::EnvConfig.send(method_name)
next unless value
2020-09-01 14:05:52 +01:00
next if (default = hash[:default].presence) && value.to_s == default.to_s
2020-04-05 15:44:50 +01:00
if ENV.sensitive?(env)
out.puts "#{env}: set"
2020-04-05 15:44:50 +01:00
else
out.puts "#{env}: #{value}"
end
end
out.puts "Homebrew Ruby: #{describe_homebrew_ruby}"
end
def host_software_config(out = $stdout)
out.puts "Clang: #{describe_clang}"
out.puts "Git: #{describe_git}"
out.puts "Curl: #{describe_curl}"
end
def dump_verbose_config(out = $stdout)
homebrew_config(out)
core_tap_config(out)
homebrew_env_config(out)
out.puts hardware if hardware
host_software_config(out)
end
end
end
require "extend/os/system_config"