forked from Jackson-Qiu/BrewMyMac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brew_my_mac.sh
executable file
·275 lines (238 loc) · 7.44 KB
/
brew_my_mac.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#! /bin/bash
cat << -EOF
#######################################################################
# 🍻 BrewMyMac -- 自动化安装 & 备份 macOS 应用程序
#
# 使用说明&项目地址:
# https://github.com/jsmjsm/BrewMyMac
#
# 原理:
# 利用 Homebrew 作为 macOS 的包管理器
# brew install 安装命令行与 GUI 程序
#
# 注意事项:
# 1. macOS 尽量保持较新版本,否则可能满足不了 Homebrew 的依赖要求
# 2. 中途若遇见安装非常慢的情况,可用 Ctrl+C 打断,直接进行下一项的安装
#######################################################################
-EOF
# Global Variable
type=0
WD=`pwd`/backup
# > Install Homebrew
install_homebrew(){
if [ `command -v brew` ]; then
echo '👌 Homebrew 已安装'
else
echo '🍺 正在安装 Homebrew... (link to Homebrew: https://brew.sh/)'
# install script:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if [ $? -ne 0 ]; then
echo '🍻 Homebrew 安装成功'
else
echo '🚫 安装失败,请检查你的网络环境,或尝试其他安装方式'
exit 127
fi
fi
}
# > Change to diffrent Homebrew source
select_homebrew_mirror (){
flag=0;
while [ "$flag" != 1 ]
do
echo
echo " 请选择 Homebrew 镜像: "
echo " 默认选项:[1] Homebrew 官方源"
echo
echo " 0: 跳过切换镜像"
echo " 1: Homebrew Default Mirror 官方源"
echo " 2: 清华大学 Tuna 源"
echo " 3: USTC 中科大源"
echo
read input
case $input in
1)
# echo "select_homebrew_mirror -> Debug: 1"
_change_homebrew_default
flag=1
;;
2)
# echo "select_homebrew_mirror -> Debug: 2"
_change_homebrew_tuna
flag=1
;;
3)
# echo "select_homebrew_mirror -> Debug: 3"
_change_homebrew_ustc
flag=1
;;
0)
return 0
;;
*)
_change_homebrew_default
# echo "select_homebrew_mirror -> Debug: default"
flag=1
;;
esac
done
return 0
}
_change_homebrew_default (){
echo "Changing the homebrew mirror to: Deafult ..."
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
echo "Change Finifh! Run 'brew update' now... "
brew update
}
_change_homebrew_tuna (){
echo "Changing the homebrew mirror to: Tuna (清华大学 Tuna 源) ..."
echo "Reference from (参考): https://mirror.tuna.tsinghua.edu.cn/help/homebrew/ "
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
echo "Change Finifh! Run 'brew update' now... "
brew update
}
_change_homebrew_ustc (){
echo "Changing the homebrew mirror to: USTC (USTC 中科大源) ..."
echo "Reference from (参考): https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git "
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
echo "Change Finifh! Run 'brew update' now... "
brew update
}
# > Install List
list_install (){
# echo "Debug: list install Begin"
for app in `cat $1`
do
install $app
done
# echo "Debug: list install End"
}
# > Install Package
install () {
# echo "Debug: install Begin"
check_installation $1
if [[ $? -eq 0 ]]; then
echo "👌 ==> 已安装" $1 ", 尝试安装下一项..."
else
echo "🔥 ==> 正在安装: " $1
# echo "Debug: Running brew install "
brew install $1
# echo $? >> /dev/null
fi
if [[ $? -eq 0 ]]; then
echo "🍺 ==> 安装成功 " $1
else
echo "🚫 ==> 安装失败" $1
fi
}
# > Installed Package Checking
check_installation (){
# if [[ $type == "cli" ]]; then
brew list -1 | grep $1 > /dev/null
if [[ $? -eq 0 ]]; then
# not installed, return 0
return 0
fi
# installed, return 1
return 1
}
# > show menu
# TODO: update the menu
show_menu () {
echo
read -p "✨ 请选择要安装的软件包类型: [0] 命令行 (默认) [1] 图形化 [2]命令行 + 图形化 " ans
echo
case $ans in
0) cd $WD && cat $WD/formulae.list && type=0
;;
1) cd $WD && cat $WD/cask.list && type=1
;;
2) cd $WD && echo "==>formulae: " && cat $WD/formulae.list && echo "==>cask.list: " && cat $WD/cask.list && type=2
;;
*) cd $WD && cat $WD/cask.list && type=0
;;
esac
}
# > 对比 Cask 安装的 App 和 Application.list 备份的 App
compare_application() {
# echo "Debug: compare_application"
cd $WD
if [ ! -f application.list ]; then
echo "application.list doesn't exists"
return 1
else
# echo "application.list exists"
ls /Applications | sed s'/\.app$//' > /tmp/cask_app.list
diff application.list /tmp/cask_app.list | grep '<.*' | sed s'/<.// ' > not_install_app.list
echo "🍃 以下 App 存在于 Application 备份列表,但未通过 brew 安装,可以尝试通过 brew 安装或自行手动安装,列表见 backup/not_install_app.list "
echo
cat not_install_app.list
echo
fi
}
# > 检测并提醒 Setapp 应用列表
setapp_notify(){
# echo "Debubg: setapp_notify"
cd $WD
if [ ! -f setapp.list ]; then
echo "setapp.list 不存在"
return 1
else
echo "🔍 检测到有 Setapp 应用列表备份: "
echo
cat setapp.list
echo
echo "Setapp 应用备份列表存放在 backup/setapp.list ,你可以自行查看"
fi
}
# 检查AWK是否可用
check_awk () {
if ! `command -v awk > /dev/null`; then
echo 未检测到AWK,请先安装AWK再执行本程序...
exit 127
fi
}
#! 程序入口
echo
echo "🙏 请花 5 秒时间看一下上述注意事项"
sleep 5s
install_homebrew
echo '🪞 假如你处于中国大陆境内,网络环境不佳,可以尝试使用 Homebrew 国内镜像源 (脚本结束后可以切换回官方源) '
select_homebrew_mirror
while :
do
show_menu
# echo "Debug: type: $type"
echo
case $type in
0) list_install $WD/formulae.list
;;
1) list_install $WD/cask.list
;;
2) list_install $WD/formulae.list && list_install $WD/cask.list
;;
*) echo "Debug: error list"
;;
esac
echo
read -p "📕 是否继续查看菜单列表,Y/y继续,N/n退出 : " ans
case $ans in
Y|y) :
;;
*) break
;;
esac
done
echo '🪞 brew 使用完毕,你可以再次选取 Homebrew 的镜像源'
select_homebrew_mirror
echo "🤔 查看 package 信息 (用于配置环境变量): 运行 \$brew info [模块名]"
compare_application
sleep 3
setapp_notify
echo
echo '🤖️ 自动化备份安装,请先运行 ./backup.sh 运行正常无误后,再运行 ./install_backup.sh '
echo
echo '🎉 享受你的新 Mac 吧!'
exit 0