-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-git-pg.sh
executable file
·128 lines (102 loc) · 4.23 KB
/
install-git-pg.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
#!/bin/bash
git_pg_echo() {
echo -e "$*" 2>/dev/null
}
git_pg_try_user_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
git_pg_echo "${1}"
}
git_pg_detect_user_profile() {
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
git_pg_echo "${PROFILE}"
return
fi
local DETECTED
DETECTED=''
if [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bash_profile" ]; then
DETECTED="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
DETECTED="$HOME/.bashrc"
fi
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED="$HOME/.zshrc"
fi
fi
if [ -z "$DETECTED" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
if DETECTED="$(git_pg_try_user_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi
if [ -n "$DETECTED" ]; then
git_pg_echo "$DETECTED"
fi
}
install() {
# Text colors
LIGHT_GREEN="\033[1;32m"
LIGHT_CYAN="\033[1;36m"
# PURPLE="\033[0;35m"
ORANGE="\033[0;33m"
NORMAL="\033[0m"
local USER_BIN_DIR
USER_BIN_DIR="$HOME/bin"
git_pg_echo
if [ ! -d "$USER_BIN_DIR" ]; then
git_pg_echo "${LIGHT_GREEN}> Creating directory: \`${USER_BIN_DIR}/\` ...${NORMAL}"
mkdir -v -p "$USER_BIN_DIR"
git_pg_echo
fi
git_pg_echo "${LIGHT_GREEN}> Copy file \`./git-pg\` to \`${USER_BIN_DIR}/\`...${NORMAL}"
cp -v ./git-pg "$USER_BIN_DIR"
git_pg_echo
local GIT_PG_USER_PROFILE
GIT_PG_USER_PROFILE="$(git_pg_detect_user_profile)"
# Add git-pg into PATH
local GIT_PG_SOURCE_PATH
# shellcheck disable=SC2016
GIT_PG_SOURCE_PATH='export PATH=$PATH:$HOME/bin'
if [ -z "${GIT_PG_USER_PROFILE-}" ]; then
git_pg_echo "${ORANGE}User profile not found.${NORMAL}"
git_pg_echo "${ORANGE}Please add the following lines to your profile manually:${NORMAL}"
git_pg_echo "\n${ORANGE}${GIT_PG_SOURCE_PATH}${NORMAL}"
git_pg_echo
else
# shellcheck disable=SC2016
if ! command grep -qc '$HOME/bin' "$GIT_PG_USER_PROFILE"; then
git_pg_echo "${LIGHT_GREEN}> Adding git-pg to your PATH...${NORMAL}"
git_pg_echo "${GIT_PG_SOURCE_PATH}\n" >>"$GIT_PG_USER_PROFILE"
else
git_pg_echo "${LIGHT_GREEN}> git-pg has already added in your \$PATH.${NORMAL}"
fi
fi
local GIT_PG_ALIAS_STR
GIT_PG_ALIAS_STR="!f() { $USER_BIN_DIR/git-pg; } f;"
git config --global alias.pg "${GIT_PG_ALIAS_STR}"
git_pg_echo
git_pg_echo "${LIGHT_GREEN}> Set \`pg\` as an git alias for \`git-pg\`.${NORMAL}"
git_pg_echo
git_pg_echo "If the ${LIGHT_CYAN}\`git-pg\`${NORMAL} command is not found, please try adding the following line to your shell profile:"
git_pg_echo
git_pg_echo "\t${ORANGE}${GIT_PG_SOURCE_PATH}${NORMAL}"
git_pg_echo
git_pg_echo "If you can't use ${LIGHT_CYAN}\`git pg\`${NORMAL} as the alias to ${LIGHT_CYAN}\`git-pg\`${NORMAL}, please check if the following line is in your ${ORANGE}\`\$HOME/.gitconfig\`${NORMAL}:"
git_pg_echo
git_pg_echo "\t${ORANGE}[alias]
\t pg = ${GIT_PG_ALIAS_STR}${NORMAL}"
git_pg_echo
git_pg_echo "┌─────────────────────────────────────────────────────────────────┐"
git_pg_echo "│ │"
git_pg_echo "│ ${LIGHT_CYAN}git-pg ${LIGHT_GREEN}installed sucessfully!${NORMAL} │"
git_pg_echo "│ │"
git_pg_echo "│ Please run ${LIGHT_CYAN}\`git pg --usage\`${NORMAL} to see the usage. │"
git_pg_echo "│ │"
git_pg_echo "└─────────────────────────────────────────────────────────────────┘"
git_pg_echo
}
install