Compare commits

...

No commits in common. "master" and "dev" have entirely different histories.
master ... dev

2 changed files with 179 additions and 148 deletions

View File

@ -1,14 +1,16 @@
# dependency
A system agnostic function to have dependencies be installed or removed.
## Description
A system agnostic function to have dependencies installed or removed.
## Options
- `-r/--remove`, uninstall packages (instead of installing them).
- `-n/--name [function]`, output the name of a function, and ask for permission to have its dependencies installed.
- `-n/--name [function]`, output the name of a function that calls for the installation/update of given packages, and ask for permission to have them installed/updated.
- `-f/--force [package]`, when uninstalling a package, do so by simply deleating its executable file.
- `-f/--force [package]`, when uninstalling a package, do so by simply deleating its binary.
- `--p/pip [package]`, describe a package to be handled using `pip`.
@ -19,3 +21,7 @@ A system agnostic function to have dependencies be installed or removed.
## Disclaimer
> This script has only been tested on Termux and some Debian-based operating systems, both of which use the `apt` package manager by default. The commands for verification, installation, updating and uninstallation of other default package managers have been drawn from reference. If you experience problems using this script wih your default package manager, issue reports and pull requests will be most welcome.
---
Ⓐ Made in Anarchy. No wage slaves were economically coerced into the making of this work.

View File

@ -1,11 +1,3 @@
# Check if this is a adequate fish version
if test (fish --version | string match -ar '\d' | string join '') -lt 300
set_color red
echo 'This plugin is compatible with fish version 3.0.0 or above, please update before trying to use it' 1>&2
set_color normal
exit 1
end
# Load dependency
function dep_plugin -d "Install or uninstall a fish plugin"
switch "$argv[1]"
@ -21,30 +13,41 @@ function dep_plugin -d "Install or uninstall a fish plugin"
and return 0
omf install $argv 2>&1 \
| not string match -qr '^(Error$|Could not install)'
or return 1
set -l install_script \
$OMF_PATH/pkg/(command basename $argv)/hooks/install.fish
test -e "$install_script"
and fish "$install_script"
for function in \
(command realpath -s $OMF_PATH/pkg/(command basename $argv)/functions/*)
source $function
end
end
end
dep_plugin https://git.disroot.org/lusiadas/feedback
dep_plugin https://gitlab.com/lusiadas/feedback
# Parse arguments
if argparse -n dependency 'r/remove' 'n/name=' 'f/force=+' 'N/npm=+' 'p/pip=+' 'P/plugin=+' -- $argv 2>&1 | read err
if argparse -n dependency 'r/remove' 'n/name=' 'f/force=+' 'N/npm=+' 'p/pip=+' 'P/plugin=+' -- $argv 2>&1 | read err
err $err
exit 1
end
set -l --append _flag_plugin (omf list | string match -qr "\b(feedback|contains_opts)\b")
# Declare variables
set -l --append _flag_plugin https://git.disroot.org/lusiadas/contains_opts
set -l failed
# Check for available permissions
set -l sudo
if command id -u | string match -qv 0
if command groups | string match -qe sudo
type -qf sudo
and set sudo sudo
end
end
# Check for a default package manager
test -n "$_flag_remove"
and dim -n "Checking available package managers..."
set -l verify
set -l install
set -l remove
set -l installed
set -l not_installed
command id -u | string match -qv 0
and command groups | string match -qe sudo
and type -qf sudo
and set -l sudo sudo
# Find the default package manager
if type -qf apt
set verify "dpkg -s"
set install 'apt install -y'
@ -70,52 +73,103 @@ else if type -qf emerge
set install 'emerge'
set remove 'emerge -c'
else if test "$argv"
err "dependency: A package manager wasn't found to handle |"(string join '|, |' $argv)"|"
err "A package manager wasn't found to handle |"(string join '|, |' $argv)"|"
reg 'Ignoring... '
set --erase argv
set failed true
end
# Search for an specific package manager
if test -n "$_flag_pip" -o -n "$_flag_npm"
set -l flags _flag_pip pip python _flag_npm npm nodejs
for i in 1 4
set --query $flags[$i]
or continue
if not type -qf $flags[(math $i + 1)]
set -l failed
if test -z "$install"
set failed true
else if read -n 1 -p "wrn -n \"|"$flags[(math $i + 2)]"| isn't installed. Install it before proceding with installation? [y/n]: \"" | string match -viq y
set failed true
end
if test -z "$failed"
dim -on "Installing |"$flags[(math $i + 2)]"|... "
eval $install $flags[(math $i + 2)] >/dev/null 2>&1
and reg -o "|"$flags[(math $i + 2)]"| installed"
or set failed true
end
if test "$failed"
string match -q $flags[$i] _flag_pip
and set -l packages $_flag_pip
or set -l packages $_flag_npm
err -o "|"$flags[(math $i + 2)]"| isn't installed. Cancelling the installation of |"(string join '|, |' $packages)"|"
set --erase $flags[$i]
end
end
end
end
# Check if package is installed
for dependency in (command printf '%s\n' $argv $_flag_pip $_flag_npm $_flag_force $flag_plugin \
set --query _flag_remove
and dim -on "Checking for dependencies... "
set -l installed
set -l not_installed
for dependency in (command printf '%s\n' $argv $_flag_pip $_flag_npm $_flag_plugin $_flag_force \
| command awk '!x[$0]++')
type -q (command basename $dependency)
if test $status = 1
if contains $dependency $argv
eval $verify $dependency >/dev/null 2>&1
else if contains $dependency $_flag_plugin
dep_plugin check $dependency
else if contains $dependency $_flag_pip
type -qf pip
and pip show -q $dependency 2>/dev/null
else if contains $dependency $flag_npm
type -qf npm
and npm list -g | string match -qe $dependency
end
end
if test $status = 0
if type -q (command basename $dependency)
set -a installed $dependency
continue
end
set --append not_installed $dependency
if contains $dependency $argv
if eval $verify $dependency >/dev/null 2>&1
set -a installed $dependency
continue
end
end
if contains $dependency $_flag_plugin $argv
if dep_plugin check $dependency
set -a installed $dependency
continue
end
end
if contains $dependency $_flag_pip $argv
if type -qf pip
if pip show -q $dependency 2>/dev/null
set -a installed $dependency
continue
end
end
end
if contains $dependency $flag_npm $argv
if type -qf npm
if npm list -g | string match -qe $dependency
set -a installed $dependency
continue
end
end
end
set not_installed $not_installed $dependency
end
# Ask for confirmation before installing or uninstalling packages
if test -n "$_flag_remove"
# Remove dependencies
if test -n "$_flag_remove" -a -n "$installed"
# Offer to uninstall dependencies
if string match -qv contains_opts "$installed"
read -n 1 -P 'Uninstall some dependencies as well? [y/n]: ' \
echo -en \r(tput el)
if test (count $installed) -eq 1
read -n 1 -p "wrn \"Uninstall dependency |$installed|? [y/n]: \"" \
| string match -qir y
or exit 0
else
read -n 1 -p "wrn 'Uninstall some dependencies as well? [y/n]: '" \
| string match -qir y
end
or exit 0
# List available dependencies
for i in (count $installed | command xargs seq)
string match -q contains_opts $installed[$i]
and reg "$i. contains_opts, feedback"
or reg "$i. $installed[$i]"
# List available dependencies
if test (count $installed) -gt 1
for i in (seq (count $installed))
echo $i. $installed[$i]
end
reg -e "[a]. |all|\n[c]. |cancel|"
reg -e '[a]. |all|\n[c]. |cancel|'
# Select dependencies to be removed
read -P 'Which? [list one or more]: ' opt
@ -127,113 +181,84 @@ if test -n "$_flag_remove"
set opt (string replace '-' '..' $opt)
set installed $installed[$opt]
end
else
read -n 1 -P 'Uninstall |contains_opts| and |feedback| as well? [y/n]: ' \
| string match -qir y
or exit 0
end
if contains contains_opts $installed
set --append installed feedback
set --append _flag_plugin feedback
end
# Check the package managers needed for installation
else if test "$not_installed"
if test -n "$_flag_pip" -o -n "$_flag_npm"
set -l flags _flag_pip pip python _flag_npm npm nodejs
for i in 1 4
set --query $flags[$i]
or continue
type -qf $flags[(math $i + 1)]
and continue
test -n "$install"
and read -n 1 -p "wrn \"|"$flags[(math $i + 2)]"| isn't installed. Install it before proceding with installation? [y/n]: \"" | string match -viq y
if $status = 0
dim "Installing |"$flags[(math $i + 2)]"|... "
if eval $install $flags[(math $i + 2)] >/dev/null 2>&1
reg -o "|"$flags[(math $i + 2)]"| installed"
continue
end
# Find the appropriate package manager to uninstall
for dependency in $installed
dim -n "Uninstalling |$dependency|... "
if contains $dependency $argv
if eval "$sudo" $uninstall $dependency >/dev/null 2>&1
reg -o "|$dependency| removed."
continue
end
string match -q $flags[$i] _flag_pip
and set -l packages $_flag_pip
or set -l packages $_flag_npm
err "dependency: |"$flags[(math $i + 2)]"| wasn't installed."
reg "Cancelling the installation of |"(string join '|, |' $packages)"|"
set not_installed (command printf '%s\n' $not_installed $packages \
| command awk '!x[$0]++' )
set failed true
end
if contains $dependency $_flag_pip $argv
if command pip list | string match -qr '^youtube-dl\b'
command pip uninstall -y $dependency >/dev/null 2>&1
reg -o "|$dependency| removed."
continue
end
end
if contains $dependency $_flag_plugin
if dep_plugin uninstall $dependency
reg -o "|$dependency| removed."
continue
end
end
if contains $dependency $flag_npm
if command npm list -g | string match -qr "\b$dependency(?=@)"
command npm uninstall -g $dependency >/dev/null 2>&1
reg -o "|$dependency| removed."
continue
end
end
if contains $dependency $_flag_force
if command rm $dependency >/dev/null 2>&1
reg -o "|$dependency| removed."
continue
end
end
err -o "Failed to uninstall |$dependency|"
end
omf list | string match -qr "\bfeedback\b"
and omf list | not string match -qr "\bcontains_opts\b"
and dep_plugin uninstall feedback
and reg -o "|feedback| removed."
# Confirm installation
if set --query _flag_name
wrn "Plugin |$_flag_name| requires |"(string match -ar '[^/]+$' $not_installed \
| command xargs basename --multiple \
| string join '|, |')"|."
if test (count $not_inatalled) = 1
read -n 1 -P "Install it? [y/n]: " | string match -qir y
else
read -n 1 -P "Install them? [y/n]: " | string match -qir y
end
# Install dependencies
else if test -z "$not_installed"
exit 0
else
# Ask for confirmation
if test -n "$_flag_name" -a -n "$not_installed"
test (count $not_installed) -eq 1
and wrn -o "Plugin |$_flag_name| requires dependency |"(string match -ar '[^/]+$' $not_installed)"|. Install it? [y/n]: "
or wrn -o "Plugin |$_flag_name| requires dependencies |"(string match -ar '[^/]+$' $not_installed | string join '|, |')"|. Install them? [y/n]: "
read -n 1 | string match -qir y
or exit 1
end
end
# Install or uninstall
set --query _flag_remove
and set -l dependencies $installed
or set -l dependencies $not_installed
for dependency in (printf '%s\n' $dependencies | tac)
set --query _flag_remove
and dim -n "Uninstalling |$dependency|... "
if contains $dependency $argv
if set --query _flag_remove
eval "$sudo" $remove $dependency >/dev/null 2>&1
else
# Find appropriate package manager to install
set -l failed
for dependency in $not_installed
dim -on "Installing |"(command basename $dependency)"|... "
if contains $dependency $argv
eval "$sudo" $install $dependency >/dev/null 2>&1
end
else if contains $dependency $_flag_pip
if set --query _flag_remove
command pip uninstall -y $dependency 2>&1 \
| not string match -qe 'not installed'
else
else if contains $dependency $_flag_pip
command pip install --user $dependency >/dev/null 2>&1
end
else if contains $dependency $_flag_plugin
if set --query _flag_remove
dep_plugin uninstall $dependency
else
else if contains $dependency $_flag_plugin
dep_plugin $dependency
end
else if contains $dependency $_flag_npm
if set --query _flag_remove
command npm uninstall -g $dependency 2>&1 \
| string match -qe removed
else
else if contains $dependency $_flag_npm
command npm install -g $dependency >/dev/null 2>&1
end
else if contains $dependency $_flag_force
type -P $dependency | command xargs rm >/dev/null 2>&1
if $status = 0
reg -o "|$dependency| was installed"
continue
end
err -o "Failed to install |$dependency|"
set failed true
end
and continue
set --query _flag_remove
and err -o "dependency: Failed to uninstall |$dependency|"
or err -o "dependency: Failed to install |$dependency|"
set --erase dependencies[(contains -i $dependency $dependencies)]
set failed true
functions -e (functions | string match -ar '^dep_.+')
test -z "$failed"
end
# Output result, exit status, and terminate
if test "$dependencies"
echo
if set --query _flag_remove
set_color green
echo(string join , $dependencies) removed.
set_color normal
else
win "|"(string join '|, |' $dependencies)"| added."
end
end
functions -e dep_plugin
test -z "$failed"