xpm.sh (3172B)
1 #!/usr/bin/env sh 2 3 pm="" 4 5 HOOKS_DIR=~/.config/xpm/hooks 6 if [ "$(uname)" = "Darwin" ]; then HOOKS_DIR=~/Library/Application Support/xpm/hooks; fi 7 8 # misc 9 usage() { 10 echo "usage: xpm COMMAND [PKG ...]" 11 echo "" 12 echo "xpm - x package manager, an interface to the system package manager." 13 echo "" 14 echo "COMMAND" 15 echo "install, in, i install all [PKG]" 16 echo "remove, re, r uninstall all [PKG] (and dependencies)" 17 echo "search, se, s search the repositories for [PKG]" 18 echo "query, qry, q query [PKG] for information" 19 echo "list, ls, l list installed files for [PKG]" 20 echo "update, up, u update all packages on the system" 21 echo "" 22 echo "See the README.md file for more details" 23 } 24 25 # hooks 26 exec_hooks() { 27 if [ "$1" = "" ]; then return; fi 28 29 if [ ! -d "$HOOKS_DIR" ]; then mkdir -p $HOOKS_DIR; fi 30 31 for f in "$HOOKS_DIR"/*; do 32 if [ "$(basename "$f")" = "$1" ]; then 33 echo "executing $f" 34 exec "$f" 35 fi 36 done 37 } 38 39 # identify the package manager 40 identify_pm() { 41 if [ "$(command -v apt)" ] && [ "$(command -v apt-get)" ]; then 42 pm="apt" 43 elif [ "$(command -v zypper)" ]; then 44 pm="zypper" 45 elif [ "$(command -v xbps-install)" ]; then 46 pm="xbps" 47 elif [ "$(command -v brew)" ]; then 48 pm="homebrew" 49 else 50 unknown_pm 51 fi 52 } 53 54 unknown_pm() { 55 echo "unknown package manager!" 56 echo " The package manager for your system could not be recognised." 57 echo " If you're using a stanadard package manager, report it to the dev so he can add support." 58 exit 59 } 60 61 # xpm commands 62 xpm_install() { 63 case $pm in 64 "apt") sudo apt install "$@" ;; 65 "zypper") sudo zypper install "$@" ;; 66 "xbps") sudo xbps-install "$@" ;; 67 "homebrew") brew install "$@" ;; 68 *) unknown_pm ;; 69 esac 70 } 71 72 xpm_remove() { 73 case $pm in 74 "apt") sudo apt purge "$@" ;; 75 "zypper") sudo zypper remove -u "$@" ;; 76 "xbps") sudo xbps-remove -R "$@" ;; 77 "homebrew") brew uninstall "$@" ;; 78 *) unknown_pm ;; 79 esac 80 } 81 82 xpm_search() { 83 case $pm in 84 "apt") apt search "$@" ;; 85 "zypper") zypper search "$@" ;; 86 "xbps") xbps-query -Rs "$@" ;; 87 "homebrew") brew search "$@" ;; 88 *) unknown_pm ;; 89 esac 90 } 91 92 xpm_query() { 93 case $pm in 94 "apt") apt show "$@" ;; 95 "zypper") zypper info "$@" ;; 96 "xbps") xbps-query -RS "$@" ;; 97 "homebrew") brew info "$@" ;; 98 *) unknown_pm ;; 99 esac 100 } 101 102 xpm_list() { 103 case $pm in 104 "apt") apt list --installed "$@" ;; 105 "zypper") zypper search --installed-only "$@" ;; 106 "xbps") xbps-query -S "$@" ;; 107 "homebrew") brew list "$@" ;; 108 *) unknown_pm ;; 109 esac 110 } 111 112 xpm_update() { 113 case $pm in 114 "apt") sudo apt update && sudo apt upgrade ;; 115 "zypper") sudo zypper refresh && sudo zypper update ;; 116 "xbps") sudo xbps-install -Suv ;; 117 "homebrew") brew update && brew upgrade ;; 118 *) unknown_pm ;; 119 esac 120 } 121 122 # main 123 identify_pm 124 125 cmd="$1"; shift 126 127 case "$cmd" in 128 "i"|"in"|"install") 129 xpm_install "$@" 130 exec_hooks install 131 ;; 132 "r"|"rm"|"remove") 133 xpm_remove "$@" 134 exec_hooks remove 135 ;; 136 "s"|"se"|"search") 137 xpm_search "$@" 138 exec_hooks search 139 ;; 140 "q"|"qry"|"query") 141 xpm_query "$@" 142 exec_hooks query 143 ;; 144 "l"|"ls"|"list") 145 xpm_list "$@" 146 exec_hooks list 147 ;; 148 "u"|"up"|"update") 149 xpm_update 150 exec_hooks update 151 ;; 152 *) 153 usage 154 exit 155 esac