pm_base.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. # pm_base.sh - Base package manager interface for USM installer
  3. #
  4. # This file provides the generic interface that dispatches to the
  5. # appropriate package manager implementation.
  6. #
  7. # To add a new package manager:
  8. # 1. Create pm_<name>.sh with:
  9. # - pm_<name>_detect: Returns 0 if available, 1 otherwise
  10. # - pm_<name>_get_missing_deps: Prints missing package names
  11. # - pm_<name>_install_missing_deps: Installs missing packages
  12. # 2. The compiler will automatically pick it up
  13. # Detect the available package manager
  14. # Sets PM_TYPE and returns 0 on success, 1 if no supported PM found
  15. detect_package_manager() {
  16. # Try each PM implementation
  17. for pm_impl in "${PM_IMPLEMENTATIONS[@]}"; do
  18. if "pm_${pm_impl}_detect" 2>/dev/null; then
  19. PM_TYPE="$pm_impl"
  20. return 0
  21. fi
  22. done
  23. PM_TYPE="unknown"
  24. return 1
  25. }
  26. # Get missing dependencies using the detected package manager
  27. pm_get_missing_deps() {
  28. case "$PM_TYPE" in
  29. apk) pm_apk_get_missing_deps "$@" ;;
  30. apt) pm_apt_get_missing_deps "$@" ;;
  31. dnf) pm_dnf_get_missing_deps "$@" ;;
  32. *)
  33. log_error "No package manager detected"
  34. echo ""
  35. ;;
  36. esac
  37. }
  38. # Install missing dependencies using the detected package manager
  39. pm_install_missing_deps() {
  40. case "$PM_TYPE" in
  41. apk) pm_apk_install_missing_deps "$@" ;;
  42. apt) pm_apt_install_missing_deps "$@" ;;
  43. dnf) pm_dnf_install_missing_deps "$@" ;;
  44. *)
  45. log_error "No package manager detected"
  46. return 1
  47. ;;
  48. esac
  49. }
  50. # Count missing dependencies
  51. count_missing_deps() {
  52. local missing=$(pm_get_missing_deps)
  53. if [[ -z "$missing" ]]; then
  54. echo 0
  55. else
  56. echo "$missing" | wc -w
  57. fi
  58. }