pm_base.sh 2.0 KB

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