pm_base.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. emerge) pm_emerge_get_missing_deps "$@" ;;
  33. *)
  34. log_error "No package manager detected"
  35. echo ""
  36. ;;
  37. esac
  38. }
  39. # Install missing dependencies using the detected package manager
  40. pm_install_missing_deps() {
  41. case "$PM_TYPE" in
  42. apk) pm_apk_install_missing_deps "$@" ;;
  43. apt) pm_apt_install_missing_deps "$@" ;;
  44. dnf) pm_dnf_install_missing_deps "$@" ;;
  45. emerge) pm_emerge_install_missing_deps "$@" ;;
  46. *)
  47. log_error "No package manager detected"
  48. return 1
  49. ;;
  50. esac
  51. }
  52. # Count missing dependencies
  53. count_missing_deps() {
  54. local missing=$(pm_get_missing_deps)
  55. if [[ -z "$missing" ]]; then
  56. echo 0
  57. else
  58. echo "$missing" | wc -w
  59. fi
  60. }