-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·143 lines (121 loc) · 4.08 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
#!/bin/bash
set -e
## 编译项目
#cargo build --release
# 定义ezcd function目标文件路径
ezcd_func_target_dir="$HOME/.bashrc.bak.ezcd"
bashrc_path="$HOME/.bashrc"
# 定义echo输出的前缀emoji
suc_prefix_emoji="💖"
fail_prefix_emoji="💔"
# 定义输出颜色
GREEN=$'\e[0;32m'
# 检查并创建备份
if [ ! -f "$ezcd_func_target_dir" ]; then
cp "$HOME/.bashrc" "$ezcd_func_target_dir" && echo "$suc_prefix_emoji ${GREEN}Backup created successfully." || echo "$fail_prefix_emoji Failed to create backup."
fi
# ezcd-bin目录
ezcd_bin_dir="$HOME/.local/bin"
# 定义配置目录和文件
config_dir="$HOME/.config/ezcd"
alias_file="$config_dir/aliases.list"
log_dir="$HOME/.config/ezcd/log"
env_shell_file="$HOME/.config/ezcd/env.sh"
# 换行
echo
# 创建配置目录,如果它还不存在
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
echo "$suc_prefix_emoji ${GREEN}Created configuration directory at $config_dir."
fi
# 创建日志文件夹,如果他还不存在
if [ ! -d "$log_dir" ]; then
mkdir -p "$log_dir"
echo "$suc_prefix_emoji ${GREEN}Created log directory at $log_dir."
fi
# 创建别名列表文件,如果它还不存在
if [ ! -f "$alias_file" ]; then
touch "$alias_file"
echo "$suc_prefix_emoji ${GREEN}Created alias file at $alias_file."
fi
# 创建env shell脚本文件
if [ ! -f "$env_shell_file" ]; then
touch "$env_shell_file"
echo "$suc_prefix_emoji ${GREEN}Created env shell file at $env_shell_file."
fi
# 定义ezcd函数
# shellcheck disable=SC2016
env_shell_content='# ezcd is a shell function which utilize the ezcd-bin to make you change directory conveniently.
function ezcd() {
# Check if no arguments are provided
if [[ $# -eq 0 ]]; then
echo "EZCD Error: The Arg of ezcd can not be empty."
return
fi
if [[ "$1" != "--set" && "$1" != "--list" && "$1" != "--remove" && "$1" != "--help" && "$1" != "--update" ]]; then
local dir=$(ezcd-bin "$@")
if [[ -n "$dir" && -d "$dir" ]]; then
cd "$dir" || return
else
echo "$dir"
fi
else
ezcd-bin "$@"
fi
}
declare -a EZCD_COMPLETIONS=()
declare -i EZCD_INDEX=0
declare PREV_COMP_WORDS=""
_ezcd_completion() {
local current_word="${COMP_WORDS[COMP_CWORD]}"
if [[ ! " ${EZCD_COMPLETIONS[*]} " =~ " ${current_word} " ]]; then
EZCD_COMPLETIONS=()
EZCD_INDEX=0
IFS=" " read -ra EZCD_COMPLETIONS <<< "$(ezcd-bin --suggest "${COMP_WORDS[@]}")"
fi
if [[ "${#EZCD_COMPLETIONS[@]}" -gt 1 ]]; then
if [[ "$EZCD_INDEX" -ge "${#EZCD_COMPLETIONS[@]}" ]]; then
EZCD_INDEX=0
fi
COMPREPLY=("${EZCD_COMPLETIONS[EZCD_INDEX]}")
((EZCD_INDEX++))
elif [[ "${#EZCD_COMPLETIONS[@]}" -eq 1 ]]; then
COMPREPLY=("${EZCD_COMPLETIONS[0]} ")
else
COMPREPLY=()
fi
}
complete -o nospace -F _ezcd_completion ezcd
'
# 覆盖写入上述脚本到env_shell_file中
echo "$env_shell_content" > "$env_shell_file"
if [ $? -eq 0 ]; then
echo "$suc_prefix_emoji ${GREEN}Ezcd configuration has been successfully written to $env_shell_file."
else
echo "$fail_prefix_emoji Failed to write ezcd configuration to $env_shell_file."
fi
# 检查是否已经有了这个 source 行
if grep -Fxq "source $env_shell_file" "$bashrc_path"
then
echo "$suc_prefix_emoji ${GREEN}The source line already exists in $bashrc_path."
else
echo "source $env_shell_file" >> "$bashrc_path"
echo "$suc_prefix_emoji ${GREEN}Added 'source $env_shell_file' to $bashrc_path."
fi
# 安装二进制文件
copied_path="$ezcd_bin_dir/ezcd-bin"
cp target/release/ezcd-bin $copied_path
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "$fail_prefix_emoji Failed to copy ezcd-bin to $copied_path."
exit 1
fi
chmod +x $copied_path
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "$fail_prefix_emoji Failed to make ezcd-bin executable."
exit 1
fi
echo "$suc_prefix_emoji ${GREEN}The CLI tool 'ezcd' installed successfully."
echo "$suc_prefix_emoji ${GREEN}Please restart your terminal or source your '$bashrc_path' to use ezcd."
echo