forked from CodelyTV/dotly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd
executable file
·131 lines (118 loc) · 3.44 KB
/
add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# FORCE_LEGACY_EXECUTION
echo "Received args to add package $*" >> ~/dotly.log
#shellcheck disable=SC1091
. "${SLOTH_PATH:-${DOTLY_PATH:-}}/scripts/core/src/_main.sh"
##? Install a package
##?
##? Usage:
##? add -v | --version
##? add -h | --help
##? add [-s | --skip-recipe] [--pkgmgr <package_manager>] [--force] <packages_names>...
##?
##? Options:
##? -h --help Show script help
##? -v --version Show script version
##? -s --skip-recipe Skip the receipe and force to use a package manager, this omit any recipe
##? --pkgmgr Force to use a package manager, for example: cargo
##? --force Force to install the package
##?
#? v3.2.1
_all_args=("$@")
package_name=""
skip_recipe=false
package_manager=""
packages_names=()
force=false
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--skipe-recipe | -s)
skip_recipe=true
shift
;;
--pkgmgr)
package_manager="$2"
shift 2
;;
--force | -f)
force=true
shift
;;
--help | -h)
echo "help"
docs::parse_docopt "${BASH_SOURCE[$((${#BASH_SOURCE[*]} - 1))]}"
exit
;;
--version | -v)
echo "version"
docs::parse_version "${BASH_SOURCE[$((${#BASH_SOURCE[*]} - 1))]}"
exit
;;
*)
packages_names+=("$1")
shift
;;
esac
done
# Multiple package installation
number_of_packages="${#packages_names[@]}"
only_args_number=$((${#_all_args[@]} - number_of_packages))
given_options=("${_all_args[@]:0:$only_args_number}")
if [[ $number_of_packages -gt 1 ]]; then
any_error=0
for pkg in "${packages_names[@]}"; do
if [[ ${#given_options[@]} -gt 0 ]]; then
"${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" package add "${given_options[@]}" "$pkg" || any_error=1
else
"${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" package add "$pkg" || any_error=1
fi
done
# Exit here because we want to execute them in order
exit $any_error
elif [[ $number_of_packages -eq 1 ]]; then
package_name="$(echo "${packages_names[*]}" | str::to_lower | xargs)"
else
output::error "You should provide at least one package to be installed"
exit 1
fi
# Force to use package manager, skip recipe always
if [[ -n "${package_manager:-}" ]]; then
# Used in package::command
#shellcheck disable=SC2034
FORCED_PKGMGR="${package_manager:-}"
skip_recipe=true
fi
if [[ -z "${package_name:-}" ]]; then
output::error "No package name provided"
exit 1
fi
# Load cargo PATH
if [[ -f "$HOME/.cargo/env" ]]; then
. "$HOME/.cargo/env"
fi
# Check if package is installed
! ${force:-false} && package::is_installed "$package_name" && log::success "$package_name already installed" && exit 0
# If there is a recipe for the package use it
if ! ${skip_recipe:-false} &&
[[ -n "$(registry::recipe_exists "$package_name")" ]]; then
if ${force:-false}; then
registry::force_install "$package_name" || true
else
registry::install "$package_name" || true
fi
if registry::is_installed "$package_name"; then
output::write "✅ \`$package_name\` installed"
exit 0
fi
else
if ${force:-false}; then
package::force_install "$package_name" "${FORCED_PKGMGR:-auto}" 2>&1 || true
else
package::install "$package_name" "${FORCED_PKGMGR:-auto}" 2>&1 || true
fi
if package::is_installed "$package_name"; then
output::write "✅ \`$package_name\` installed"
exit 0
fi
fi
output::write "❌ \`$package_name\` could not be installed" && exit 1