crew: Check for dependency before removal (#12514)

* crew: Check for dependency before removal

Signed-off-by: SupeChicken666 <supechicken666@gmail.com>

* Remove trailing newlines

Signed-off-by: SupeChicken666 <supechicken666@gmail.com>

* Use `has_key?` instead of `keys.any`

Signed-off-by: SupeChicken666 <supechicken666@gmail.com>

* Bump version

Signed-off-by: SupeChicken666 <supechicken666@gmail.com>

---------

Signed-off-by: SupeChicken666 <supechicken666@gmail.com>
This commit is contained in:
SupeChicken666
2025-08-17 00:58:17 +08:00
committed by GitHub
parent 6fa6eddf96
commit df6d08380d
2 changed files with 25 additions and 5 deletions

View File

@@ -21,14 +21,34 @@ class Command
if CREW_ESSENTIAL_PACKAGES.include?(pkg.name) && !force
return if pkg.in_upgrade
puts <<~ESSENTIAL_PACKAGE_WARNING_EOF.gsub(/^(?=\w)/, ' ').lightred
# Exit with failure if attempt to remove an essential package
# is made.
abort <<~ESSENTIAL_PACKAGE_WARNING_EOF.gsub(/^(?=\w)/, ' ').chomp.lightred
#{pkg.name.capitalize} is considered an essential package needed for
Chromebrew to function and thus cannot be removed.
ESSENTIAL_PACKAGE_WARNING_EOF
end
# Exit with failure if attempt to remove an essential package
# is made.
exit 1
# Check whether the removal breaks dependency of other installed packages
unless force
pkgs_that_need_it = []
device_json['installed_packages'].each do |installed_pkg_info|
pkg_file = File.join(CREW_PACKAGES_PATH, "#{installed_pkg_info['name']}.rb")
installed_pkg = Package.load_package(pkg_file)
pkgs_that_need_it << installed_pkg.name if installed_pkg.dependencies.key?(pkg.name)
end
if pkgs_that_need_it.any?
abort <<~EOT.chomp.lightred
#{pkg.name.capitalize} is required by the following installed packages:
#{pkgs_that_need_it.join("\n ")}
Use `crew remove --force` if you meant to remove it.
EOT
end
end
# Perform any operations required prior to package removal.

View File

@@ -4,7 +4,7 @@ require 'etc'
require 'open3'
OLD_CREW_VERSION ||= defined?(CREW_VERSION) ? CREW_VERSION : '1.0'
CREW_VERSION ||= '1.64.7' unless defined?(CREW_VERSION) && CREW_VERSION == OLD_CREW_VERSION
CREW_VERSION ||= '1.64.8' unless defined?(CREW_VERSION) && CREW_VERSION == OLD_CREW_VERSION
# Kernel architecture.
KERN_ARCH ||= Etc.uname[:machine]