2025-03-18 17:38:37 +00:00
|
|
|
# typed: true # rubocop:todo Sorbet/StrictSigil
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "fileutils"
|
|
|
|
require "pathname"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module Bundle
|
|
|
|
module Dumper
|
2025-03-21 04:24:55 +00:00
|
|
|
private_class_method def self.can_write_to_brewfile?(brewfile_path, force: false)
|
2025-03-18 17:38:37 +00:00
|
|
|
raise "#{brewfile_path} already exists" if should_not_write_file?(brewfile_path, overwrite: force)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2025-07-04 11:25:41 +01:00
|
|
|
def self.build_brewfile(describe:, no_restart:, formulae:, taps:, casks:, mas:, whalebrew:, vscode:)
|
2025-03-24 21:55:47 +08:00
|
|
|
require "bundle/tap_dumper"
|
2025-07-04 11:25:41 +01:00
|
|
|
require "bundle/formula_dumper"
|
2025-03-24 21:55:47 +08:00
|
|
|
require "bundle/cask_dumper"
|
|
|
|
require "bundle/mac_app_store_dumper"
|
|
|
|
require "bundle/whalebrew_dumper"
|
|
|
|
require "bundle/vscode_extension_dumper"
|
|
|
|
|
2025-03-18 17:38:37 +00:00
|
|
|
content = []
|
|
|
|
content << TapDumper.dump if taps
|
2025-07-04 11:25:41 +01:00
|
|
|
content << FormulaDumper.dump(describe:, no_restart:) if formulae
|
2025-03-18 17:38:37 +00:00
|
|
|
content << CaskDumper.dump(describe:) if casks
|
|
|
|
content << MacAppStoreDumper.dump if mas
|
|
|
|
content << WhalebrewDumper.dump if whalebrew
|
|
|
|
content << VscodeExtensionDumper.dump if vscode
|
|
|
|
"#{content.reject(&:empty?).join("\n")}\n"
|
|
|
|
end
|
|
|
|
|
2025-07-04 11:25:41 +01:00
|
|
|
def self.dump_brewfile(global:, file:, describe:, force:, no_restart:, formulae:, taps:, casks:, mas:,
|
|
|
|
whalebrew:, vscode:)
|
2025-03-18 17:38:37 +00:00
|
|
|
path = brewfile_path(global:, file:)
|
|
|
|
can_write_to_brewfile?(path, force:)
|
2025-07-04 11:25:41 +01:00
|
|
|
content = build_brewfile(describe:, no_restart:, taps:, formulae:, casks:, mas:, whalebrew:, vscode:)
|
2025-03-18 17:38:37 +00:00
|
|
|
write_file path, content
|
|
|
|
end
|
|
|
|
|
2025-03-21 04:24:55 +00:00
|
|
|
def self.brewfile_path(global: false, file: nil)
|
2025-03-24 21:55:47 +08:00
|
|
|
require "bundle/brewfile"
|
2025-03-18 17:38:37 +00:00
|
|
|
Brewfile.path(dash_writes_to_stdout: true, global:, file:)
|
|
|
|
end
|
|
|
|
|
2025-03-21 04:24:55 +00:00
|
|
|
private_class_method def self.should_not_write_file?(file, overwrite: false)
|
2025-03-18 17:38:37 +00:00
|
|
|
file.exist? && !overwrite && file.to_s != "/dev/stdout"
|
|
|
|
end
|
|
|
|
|
2025-03-21 04:24:55 +00:00
|
|
|
def self.write_file(file, content)
|
2025-03-18 17:38:37 +00:00
|
|
|
Bundle.exchange_uid_if_needed! do
|
|
|
|
file.open("w") { |io| io.write content }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|