-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·154 lines (131 loc) · 3.87 KB
/
install.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/zsh
{ # Ensure the whole script has downloaded
# Inspired by https://github.com/nvm-sh/nvm/blob/master/install.sh
cdu_echo() {
command printf %s\\n "$*" 2>/dev/null
}
cdu_default_install_dir() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.cdu" || printf %s "${XDG_CONFIG_HOME}/cdu"
}
cdu_latest_version() {
cdu_echo "master"
}
cdu_install_dir() {
if [ -n "$CDU_DIR" ]; then
printf %s "${CDU_DIR}"
else
cdu_default_install_dir
fi
}
cdu_source() {
local CDU_GITHUB_REPO
CDU_GITHUB_REPO="${CDU_INSTALL_GITHUB_REPO:-catapultcx/cdu}"
local CDU_VERSION
CDU_VERSION="${CDU_INSTALL_VERSION:-$(cdu_latest_version)}"
local CDU_SOURCE_URL
# CDU_SOURCE_URL="https://github.com/${CDU_GITHUB_REPO}.git"
CDU_SOURCE_URL="[email protected]:${CDU_GITHUB_REPO}.git"
cdu_echo "$CDU_SOURCE_URL"
}
try_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
cdu_echo "${1}"
}
#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
detect_profile() {
if [ "${PROFILE-}" = '/dev/null' ]; then
# the user has specifically requested NOT to have cdu touch their profile
return
fi
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
cdu_echo "${PROFILE}"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
if [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.zprofile" ]; then
DETECTED_PROFILE="$HOME/.zprofile"
fi
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zprofile" ".zshrc"
do
if DETECTED_PROFILE="$(try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi
if [ -n "$DETECTED_PROFILE" ]; then
cdu_echo "$DETECTED_PROFILE"
fi
}
uninstall_cdu() {
local INSTALL_DIR
INSTALL_DIR="$(cdu_install_dir)"
cdu_echo "$INSTALL_DIR"
rm -rf $INSTALL_DIR
}
install_cdu_from_git() {
local INSTALL_DIR
INSTALL_DIR="$(cdu_install_dir)"
CDU_VERSION="${CDU_INSTALL_VERSION:-$(cdu_latest_version)}"
local fetch_error
if [ -d "$INSTALL_DIR/.git" ]; then
# Updating repo
cdu_echo "=> cdu is already installed in $INSTALL_DIR, trying to update using git"
command printf '\r=> '
fetch_error="Failed to update cdu with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
else
fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"
cdu_echo "=> Downloading cdu from git to '$INSTALL_DIR'"
command printf '\r=> '
mkdir -p "${INSTALL_DIR}"
if [ "$(ls -A "${INSTALL_DIR}")" ]; then
# Initializing repo
command git init "${INSTALL_DIR}" || {
cdu_echo >&2 'Failed to initialize cdu repo. Please report this!'
exit 2
}
command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(cdu_source)" 2> /dev/null \
|| command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(cdu_source)" || {
cdu_echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
exit 2
}
else
# Cloning repo
command git clone "$(cdu_source)" --depth=1 "${INSTALL_DIR}" || {
cdu_echo >&2 'Failed to clone cdu repo. Please report this!'
exit 2
}
fi
fi
}
cdu_do_install() {
install_cdu_from_git
cdu_echo
cdu_echo "Now add the install dir to your path in $(detect_profile)"
cdu_reset
}
cdu_reset() {
unset -f cdu_install_dir cdu_latest_version \
cdu_source install_cdu_from_git \
cdu_do_install cdu_reset
}
cdu_do_install
}