bundle/dumper: typed: strict

This commit is contained in:
Ruoyu Zhong 2025-08-21 01:26:30 +08:00
parent 579c31f001
commit a03aaef339
No known key found for this signature in database

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "fileutils"
@ -7,12 +7,25 @@ require "pathname"
module Homebrew
module Bundle
module Dumper
sig { params(brewfile_path: Pathname, force: T::Boolean).returns(T::Boolean) }
private_class_method def self.can_write_to_brewfile?(brewfile_path, force: false)
raise "#{brewfile_path} already exists" if should_not_write_file?(brewfile_path, overwrite: force)
true
end
sig {
params(
describe: T::Boolean,
no_restart: T::Boolean,
formulae: T::Boolean,
taps: T::Boolean,
casks: T::Boolean,
mas: T::Boolean,
whalebrew: T::Boolean,
vscode: T::Boolean,
).returns(String)
}
def self.build_brewfile(describe:, no_restart:, formulae:, taps:, casks:, mas:, whalebrew:, vscode:)
require "bundle/tap_dumper"
require "bundle/formula_dumper"
@ -31,6 +44,21 @@ module Homebrew
"#{content.reject(&:empty?).join("\n")}\n"
end
sig {
params(
global: T::Boolean,
file: T.nilable(String),
describe: T::Boolean,
force: T::Boolean,
no_restart: T::Boolean,
formulae: T::Boolean,
taps: T::Boolean,
casks: T::Boolean,
mas: T::Boolean,
whalebrew: T::Boolean,
vscode: T::Boolean,
).void
}
def self.dump_brewfile(global:, file:, describe:, force:, no_restart:, formulae:, taps:, casks:, mas:,
whalebrew:, vscode:)
path = brewfile_path(global:, file:)
@ -39,15 +67,18 @@ module Homebrew
write_file path, content
end
sig { params(global: T::Boolean, file: T.nilable(String)).returns(Pathname) }
def self.brewfile_path(global: false, file: nil)
require "bundle/brewfile"
Brewfile.path(dash_writes_to_stdout: true, global:, file:)
end
sig { params(file: Pathname, overwrite: T::Boolean).returns(T::Boolean) }
private_class_method def self.should_not_write_file?(file, overwrite: false)
file.exist? && !overwrite && file.to_s != "/dev/stdout"
end
sig { params(file: Pathname, content: String).void }
def self.write_file(file, content)
Bundle.exchange_uid_if_needed! do
file.open("w") { |io| io.write content }