-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathHomebrew_old.sh
951 lines (855 loc) · 27.4 KB
/
Homebrew_old.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#HomeBrew自动安装脚本
#brew brew brew brew
#获取硬件信息 判断inter还是苹果M
UNAME_MACHINE="$(uname -m)"
#在X86电脑上测试arm电脑
# UNAME_MACHINE="arm64"
# 判断是Linux还是Mac os
OS="$(uname)"
if [[ "${OS}" == "Linux" ]]
then
HOMEBREW_ON_LINUX=1
elif [[ "${OS}" == "Darwin" ]]
then
HOMEBREW_ON_MACOS=1
else
echo "Homebrew 只运行在 Mac OS 或 Linux."
fi
# 字符串染色程序
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_universal() { tty_escape "0;$1"; } #正常显示
tty_mkbold() { tty_escape "1;$1"; } #设置高亮
tty_underline="$(tty_escape "4;39")" #下划线
tty_blue="$(tty_universal 34)" #蓝色
tty_red="$(tty_universal 31)" #红色
tty_green="$(tty_universal 32)" #绿色
tty_yellow="$(tty_universal 33)" #黄色
tty_bold="$(tty_universal 39)" #加黑
tty_cyan="$(tty_universal 36)" #青色
tty_reset="$(tty_escape 0)" #去除颜色
#用户输入极速安装speed,git克隆只取最近新版本
#但是update会出错,提示需要下载全部数据
GIT_SPEED=""
if [[ $0 == "speed" ]]; then
GIT_SPEED="--depth=1"
else
for dir in $@; do
echo $dir
if [[ $dir == "speed" ]]; then
GIT_SPEED="--depth=1"
fi
done
fi
if [[ $GIT_SPEED != "" ]]; then
echo "${tty_red}
检测到参数speed,只拉取最新数据,可以正常install使用!
腾讯和阿里不支持speed拉取,需要腾讯阿里需要完全模式。
但是以后brew update的时候会报错,运行报错提示的两句命令即可修复
${tty_reset}"
fi
#获取前面两个.的数据
major_minor() {
echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"
}
#设置一些平台地址
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#Mac
if [[ "$UNAME_MACHINE" == "arm64" ]]; then
#M1
HOMEBREW_PREFIX="/opt/homebrew"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}"
else
#Inter
HOMEBREW_PREFIX="/usr/local"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
fi
HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
HOMEBREW_LOGS="${HOME}/Library/Logs/Homebrew"
#国内没有homebrew-services,手动在gitee创建了一个,有少数人用到。
USER_SERVICES_GIT=https://gitee.com/cunkai/homebrew-services.git
STAT_PRINTF=("stat" "-f")
PERMISSION_FORMAT="%A"
CHOWN=("/usr/sbin/chown")
CHGRP=("/usr/bin/chgrp")
GROUP="admin"
TOUCH=("/usr/bin/touch")
INSTALL=("/usr/bin/install" -d -o "root" -g "wheel" -m "0755")
#获取Mac系统版本
macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"
else
#Linux
UNAME_MACHINE="$(uname -m)"
HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
HOMEBREW_CACHE="${HOME}/.cache/Homebrew"
HOMEBREW_LOGS="${HOME}/.logs/Homebrew"
STAT_PRINTF=("stat" "--printf")
PERMISSION_FORMAT="%a"
CHOWN=("/bin/chown")
CHGRP=("/bin/chgrp")
GROUP="$(id -gn)"
TOUCH=("/bin/touch")
INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755")
fi
CHMOD=("/bin/chmod")
MKDIR=("/bin/mkdir" "-p")
#获取系统时间
TIME=$(date "+%Y-%m-%d %H:%M:%S")
JudgeSuccess()
{
if [ $? -ne 0 ];then
echo "${tty_red}此步骤失败 '$1'${tty_reset}"
if [[ "$2" == 'out' ]]; then
exit 0
fi
else
echo "${tty_green}此步骤成功${tty_reset}"
fi
}
# 判断是否有系统权限
have_sudo_access() {
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
/usr/bin/sudo -l mkdir &>/dev/null
HAVE_SUDO_ACCESS="$?"
fi
if [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
echo "${tty_red}开机密码输入错误,获取权限失败!${tty_reset}"
fi
return "$HAVE_SUDO_ACCESS"
}
abort() {
printf "%s\n" "$1"
# exit 1
}
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
execute() {
if ! "$@"; then
abort "$(printf "${tty_red}此命令运行失败: %s${tty_reset}" "$(shell_join "$@")")"
fi
}
ohai() {
printf "${tty_blue}运行代码 ==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
# 管理员运行
execute_sudo()
{
local -a args=("$@")
if have_sudo_access; then
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
ohai "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
#添加文件夹权限
AddPermission()
{
execute_sudo "/bin/chmod" "-R" "a+rwx" "$1"
execute_sudo "$CHOWN" "$USER" "$1"
execute_sudo "$CHGRP" "$GROUP" "$1"
}
#创建文件夹
CreateFolder()
{
echo '-> 创建文件夹' $1
execute_sudo "/bin/mkdir" "-p" "$1"
JudgeSuccess
AddPermission $1
}
RmAndCopy()
{
if [[ -d $1 ]]; then
echo " ---备份要删除的$1到系统桌面...."
if ! [[ -d $HOME/Desktop/Old_Homebrew/$TIME/$1 ]]; then
sudo mkdir -p "$HOME/Desktop/Old_Homebrew/$TIME/$1"
fi
sudo cp -rf $1 "$HOME/Desktop/Old_Homebrew/$TIME/$1"
echo " ---$1 备份完成"
fi
sudo rm -rf $1
}
RmCreate()
{
RmAndCopy $1
CreateFolder $1
}
#判断文件夹存在但不可写
exists_but_not_writable() {
[[ -e "$1" ]] && ! [[ -r "$1" && -w "$1" && -x "$1" ]]
}
#文件所有者
get_owner() {
"${STAT_PRINTF[@]}" "%u" "$1"
}
#文件本人无权限
file_not_owned() {
[[ "$(get_owner "$1")" != "$(id -u)" ]]
}
#获取所属的组
get_group() {
"${STAT_PRINTF[@]}" "%g" "$1"
}
#不在所属组
file_not_grpowned() {
[[ " $(id -G "${USER}") " != *" $(get_group "$1") "* ]]
}
#获得当前文件夹权限 例如777
get_permission() {
"${STAT_PRINTF[@]}" "${PERMISSION_FORMAT}" "$1"
}
#授权当前用户权限
user_only_chmod() {
[[ -d "$1" ]] && [[ "$(get_permission "$1")" != 75[0145] ]]
}
#创建brew需要的目录 直接复制于国外版本,同步
CreateBrewLinkFolder()
{
echo "--创建Brew所需要的目录"
directories=(
bin etc include lib sbin share opt var
Frameworks
etc/bash_completion.d lib/pkgconfig
share/aclocal share/doc share/info share/locale share/man
share/man/man1 share/man/man2 share/man/man3 share/man/man4
share/man/man5 share/man/man6 share/man/man7 share/man/man8
var/log var/homebrew var/homebrew/linked
bin/brew
)
group_chmods=()
for dir in "${directories[@]}"
do
if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"
then
group_chmods+=("${HOMEBREW_PREFIX}/${dir}")
fi
done
# zsh refuses to read from these directories if group writable
directories=(share/zsh share/zsh/site-functions)
zsh_dirs=()
for dir in "${directories[@]}"
do
zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}")
done
directories=(
bin etc include lib sbin share var opt
share/zsh share/zsh/site-functions
var/homebrew var/homebrew/linked
Cellar Caskroom Frameworks
)
mkdirs=()
for dir in "${directories[@]}"
do
if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]
then
mkdirs+=("${HOMEBREW_PREFIX}/${dir}")
fi
done
user_chmods=()
mkdirs_user_only=()
if [[ "${#zsh_dirs[@]}" -gt 0 ]]
then
for dir in "${zsh_dirs[@]}"
do
if [[ ! -d "${dir}" ]]
then
mkdirs_user_only+=("${dir}")
elif user_only_chmod "${dir}"
then
user_chmods+=("${dir}")
fi
done
fi
chmods=()
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
chmods+=("${group_chmods[@]}")
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
chmods+=("${user_chmods[@]}")
fi
chowns=()
chgrps=()
if [[ "${#chmods[@]}" -gt 0 ]]
then
for dir in "${chmods[@]}"
do
if file_not_owned "${dir}"
then
chowns+=("${dir}")
fi
if file_not_grpowned "${dir}"
then
chgrps+=("${dir}")
fi
done
fi
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
ohai "The following existing directories will be made group writable:"
printf "%s\n" "${group_chmods[@]}"
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
ohai "The following existing directories will be made writable by user only:"
printf "%s\n" "${user_chmods[@]}"
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:"
printf "%s\n" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
then
ohai "The following existing directories will have their group set to ${tty_underline}${GROUP}${tty_reset}:"
printf "%s\n" "${chgrps[@]}"
fi
if [[ "${#mkdirs[@]}" -gt 0 ]]
then
ohai "The following new directories will be created:"
printf "%s\n" "${mkdirs[@]}"
fi
non_default_repos=""
additional_shellenv_commands=()
if [[ "${HOMEBREW_BREW_DEFAULT_GIT_REMOTE}" != "${HOMEBREW_BREW_GIT_REMOTE}" ]]
then
ohai "HOMEBREW_BREW_GIT_REMOTE is set to a non-default URL:"
echo "${tty_underline}${HOMEBREW_BREW_GIT_REMOTE}${tty_reset} will be used as the Homebrew/brew Git remote."
non_default_repos="Homebrew/brew"
additional_shellenv_commands+=("export HOMEBREW_BREW_GIT_REMOTE=\"${HOMEBREW_BREW_GIT_REMOTE}\"")
fi
if [[ "${HOMEBREW_CORE_DEFAULT_GIT_REMOTE}" != "${HOMEBREW_CORE_GIT_REMOTE}" ]]
then
ohai "HOMEBREW_CORE_GIT_REMOTE is set to a non-default URL:"
echo "${tty_underline}${HOMEBREW_CORE_GIT_REMOTE}${tty_reset} will be used as the Homebrew/homebrew-core Git remote."
non_default_repos="${non_default_repos:-}${non_default_repos:+ and }Homebrew/homebrew-core"
additional_shellenv_commands+=("export HOMEBREW_CORE_GIT_REMOTE=\"${HOMEBREW_CORE_GIT_REMOTE}\"")
fi
if [[ -n "${HOMEBREW_NO_INSTALL_FROM_API-}" ]]
then
ohai "HOMEBREW_NO_INSTALL_FROM_API is set."
echo "Homebrew/homebrew-core will be tapped during this ${tty_bold}install${tty_reset} run."
fi
if [[ -d "${HOMEBREW_PREFIX}" ]]
then
if [[ "${#chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "u+rwx" "${chmods[@]}"
fi
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "g+rwx" "${group_chmods[@]}"
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "go-w" "${user_chmods[@]}"
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
execute_sudo "${CHOWN[@]}" "${USER}" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
then
execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}"
fi
else
execute_sudo "${INSTALL[@]}" "${HOMEBREW_PREFIX}"
fi
if [[ "${#mkdirs[@]}" -gt 0 ]]
then
execute_sudo "${MKDIR[@]}" "${mkdirs[@]}"
execute_sudo "${CHMOD[@]}" "ug=rwx" "${mkdirs[@]}"
if [[ "${#mkdirs_user_only[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "go-w" "${mkdirs_user_only[@]}"
fi
execute_sudo "${CHOWN[@]}" "${USER}" "${mkdirs[@]}"
execute_sudo "${CHGRP[@]}" "${GROUP}" "${mkdirs[@]}"
fi
if ! [[ -d "${HOMEBREW_REPOSITORY}" ]]
then
execute_sudo "${MKDIR[@]}" "${HOMEBREW_REPOSITORY}"
fi
execute_sudo "${CHOWN[@]}" "-R" "${USER}:${GROUP}" "${HOMEBREW_REPOSITORY}"
if ! [[ -d "${HOMEBREW_CACHE}" ]]
then
if [[ -n "${HOMEBREW_ON_MACOS-}" ]]
then
execute_sudo "${MKDIR[@]}" "${HOMEBREW_CACHE}"
else
execute "${MKDIR[@]}" "${HOMEBREW_CACHE}"
fi
fi
if exists_but_not_writable "${HOMEBREW_CACHE}"
then
execute_sudo "${CHMOD[@]}" "g+rwx" "${HOMEBREW_CACHE}"
fi
if file_not_owned "${HOMEBREW_CACHE}"
then
execute_sudo "${CHOWN[@]}" "-R" "${USER}" "${HOMEBREW_CACHE}"
fi
if file_not_grpowned "${HOMEBREW_CACHE}"
then
execute_sudo "${CHGRP[@]}" "-R" "${GROUP}" "${HOMEBREW_CACHE}"
fi
if [[ -d "${HOMEBREW_CACHE}" ]]
then
execute "${TOUCH[@]}" "${HOMEBREW_CACHE}/.cleaned"
fi
echo "--依赖目录脚本运行完成"
}
#git提交
git_commit(){
git add .
git commit -m "your del"
}
#version_gt 判断$1是否大于$2
version_gt() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]
}
#version_ge 判断$1是否大于等于$2
version_ge() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
}
#version_lt 判断$1是否小于$2
version_lt() {
[[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
}
#发现错误 关闭脚本 提示如何解决
error_game_over(){
echo "
${tty_red}失败$MY_DOWN_NUM 终端输入 ${HOMEBREW_REPOSITORY}/bin/brew -v 没有反应表示失败
右键下面地址查看常见错误解决办法
https://gitee.com/cunkai/HomebrewCN/blob/master/error.md
或者别的安装方法:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/
如果没有解决,把全部运行过程截图发到 [email protected] ${tty_reset}
"
exit 0
}
#一些警告判断
warning_if(){
git_https_proxy=$(git config --global https.proxy)
git_http_proxy=$(git config --global http.proxy)
if [[ -z "$git_https_proxy" && -z "$git_http_proxy" ]]; then
echo "未发现Git代理(属于正常状态)"
else
echo "${tty_yellow}
提示:发现你电脑设置了Git代理,如果Git报错,请运行下面两句话:
git config --global --unset https.proxy
git config --global --unset http.proxy${tty_reset}
"
fi
}
#开始克隆
start_clone_brew(){
#询问是否删除之前的
echo -n "${tty_green}!!!此脚本将要删除之前的brew(包括它下载的软件),请自行备份。
->是否现在开始执行脚本(N/Y) "
read MY_Del_Old
echo "${tty_reset}"
case $MY_Del_Old in
"y")
echo "--> 脚本开始执行"
;;
"Y")
echo "--> 脚本开始执行"
;;
*)
echo "你输入了 $MY_Del_Old ,脚本运行中断, 如果继续运行脚本应该输入Y或者y
"
exit 0
;;
esac
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#MAC
echo "${tty_yellow} Mac os设置开机密码方法:
(设置开机密码:在左上角苹果图标->系统偏好设置->"用户与群组"->更改密码)
(如果提示This incident will be reported. 在"用户与群组"中查看是否管理员) ${tty_reset}"
fi
echo "==> 通过命令删除之前的brew、创建一个新的Homebrew文件夹
${tty_cyan}请输入开机密码,输入过程不显示,输入完后回车${tty_reset}"
sudo echo '开始执行'
#删除以前的Homebrew
RmCreate ${HOMEBREW_REPOSITORY}
RmAndCopy $HOMEBREW_CACHE
RmAndCopy $HOMEBREW_LOGS
# 让环境暂时纯粹,脚本运行结束后恢复
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin
fi
git --version
if [ $? -ne 0 ];then
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
sudo rm -rf "/Library/Developer/CommandLineTools/"
echo "${tty_cyan}安装Git${tty_reset}后再运行此脚本,${tty_red}在系统弹窗中点击“安装”按钮
如果没有弹窗的老系统,需要自己下载安装:https://sourceforge.net/projects/git-osx-installer/ ${tty_reset}"
xcode-select --install
exit 0
else
echo "${tty_red} 发现缺少git,开始安装,请输入Y ${tty_reset}"
sudo apt install git
fi
fi
echo "
${tty_cyan}下载速度觉得慢可以ctrl+c或control+c重新运行脚本选择下载源${tty_reset}
==> 从 $USER_BREW_GIT 克隆Homebrew基本文件
"
warning_if
sudo git clone ${GIT_SPEED} $USER_BREW_GIT ${HOMEBREW_REPOSITORY}
JudgeSuccess 尝试再次运行自动脚本选择其他下载源或者切换网络 out
git config --global --add safe.directory ${HOMEBREW_REPOSITORY}
#依赖目录创建 授权等等
CreateBrewLinkFolder
echo '==> 创建brew的替身'
if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_PREFIX}" ]]; then
find ${HOMEBREW_PREFIX}/bin -name brew -exec sudo rm -f {} \;
execute "ln" "-sf" "${HOMEBREW_REPOSITORY}/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
fi
#询问是否删除之前的
echo -n "${tty_green}
brew下载完成。
如果需要Core、Cask、services的话,输入Y继续克隆
不需要的回车跳过:"
read MY_BREW_CUSTOM
echo "${tty_reset}"
if [[ $MY_BREW_CUSTOM == "Y" || $MY_BREW_CUSTOM == "y" ]]; then
#继续克隆啊
echo "==> 从 $USER_CORE_GIT 克隆Homebrew Core
${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}"
sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core
sudo git clone ${GIT_SPEED} $USER_CORE_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/
JudgeSuccess 尝试再次运行自动脚本选择其他下载源或者切换网络 out
git config --global --add safe.directory ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#MAC
echo "==> 从 $USER_CASK_GIT 克隆Homebrew Cask 图形化软件
${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}"
sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
sudo git clone ${GIT_SPEED} $USER_CASK_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask/
if [ $? -ne 0 ];then
sudo rm -rf ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
echo "${tty_red}尝试切换下载源或者切换网络,不过Cask组件非必须模块。可以忽略${tty_reset}"
else
echo "${tty_green}此步骤成功${tty_reset}"
git config --global --add safe.directory ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask/
fi
echo "==> 从 $USER_SERVICES_GIT 克隆Homebrew services 管理服务的启停
"
sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
sudo git clone ${GIT_SPEED} $USER_SERVICES_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-services/
JudgeSuccess
git config --global --add safe.directory ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-services/
brew services cleanup
else
#Linux
echo "${tty_yellow} Linux 不支持Cask图形化软件下载 此步骤跳过${tty_reset}"
fi
fi
}
#代码从这里开始执行
echo "
${tty_green} 开始执行Homebrew自动安装程序 ${tty_reset}
${tty_cyan} [[email protected]] ${tty_reset}
['$TIME']['$macos_version']
${tty_cyan} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset}
"
#选择一个brew下载源
echo -n "${tty_green}
请选择一个下载brew本体的序号,例如中科大,输入1回车。
源有时候不稳定,如果git克隆报错重新运行脚本选择源。
1、中科大下载源
2、清华大学下载源
3、北京外国语大学下载源 ${tty_reset}"
if [[ $GIT_SPEED == "" ]]; then
echo -n "${tty_green}
4、腾讯下载源
5、阿里巴巴下载源
6、跳过下载brew去配置下载源 ${tty_reset}"
fi
echo -n "
${tty_blue}请输入序号: "
read MY_DOWN_NUM
echo "${tty_reset}"
case $MY_DOWN_NUM in
"2")
echo "
你选择了清华大学brew本体下载源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/
#HomeBrew基础框架
USER_BREW_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
#HomeBrew Core
USER_CORE_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
#HomeBrew Cask
USER_CASK_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
USER_CASK_FONTS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
USER_CASK_DRIVERS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
#API
HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
;;
"3")
echo "
北京外国语大学brew本体下载源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles
#HomeBrew基础框架
USER_BREW_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/brew.git
#HomeBrew Core
USER_CORE_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-core.git
#HomeBrew Cask
USER_CASK_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask.git
USER_CASK_FONTS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-fonts.git
USER_CASK_DRIVERS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-drivers.git
;;
"4")
echo "
你选择了腾讯brew本体下载源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles
#HomeBrew基础框架
USER_BREW_GIT=https://mirrors.cloud.tencent.com/homebrew/brew.git
#HomeBrew Core
USER_CORE_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
#HomeBrew Cask
USER_CASK_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git
;;
"5")
echo "
你选择了阿里巴巴brew本体下载源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles
#HomeBrew基础框架
USER_BREW_GIT=https://mirrors.aliyun.com/homebrew/brew.git
#HomeBrew Core
USER_CORE_GIT=https://mirrors.aliyun.com/homebrew/homebrew-core.git
#HomeBrew Cask
USER_CASK_GIT=https://mirrors.aliyun.com/homebrew/homebrew-cask.git
;;
*)
echo "
你选择了中国科学技术大学brew本体下载源
"
#HomeBrew 下载源 install
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
#HomeBrew基础框架
USER_BREW_GIT=https://mirrors.ustc.edu.cn/brew.git
#HomeBrew Core
USER_CORE_GIT=https://mirrors.ustc.edu.cn/homebrew-core.git
#HomeBrew Cask
USER_CASK_GIT=https://mirrors.ustc.edu.cn/homebrew-cask.git
#API
HOMEBREW_API_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/api
;;
esac
if [[ $MY_DOWN_NUM == "6" ]]; then
echo '==> 跳过克隆brew,准备配置国内镜像源'
else
start_clone_brew
fi
echo "==> 配置国内镜像源HOMEBREW BOTTLE
${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}"
#判断下mac os终端是Bash还是zsh
case "$SHELL" in
*/bash*)
if [[ -r "$HOME/.bash_profile" ]]; then
shell_profile="${HOME}/.bash_profile"
else
shell_profile="${HOME}/.profile"
fi
;;
*/zsh*)
shell_profile="${HOME}/.zprofile"
;;
*)
shell_profile="${HOME}/.profile"
;;
esac
if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
#Linux
shell_profile="/etc/profile"
fi
if [[ -f ${shell_profile} ]]; then
AddPermission ${shell_profile}
fi
#删除之前的环境变量
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#Mac
sed -i "" "/ckbrew/d" ${shell_profile}
echo '有些电脑xcode和git混乱,再运行一次,此处如果有error正常。'
xcode-select --install
else
#Linux
sed -i "/ckbrew/d" ${shell_profile}
fi
#选择一个homebrew-bottles下载源
echo -n "${tty_green}
Homebrew已经安装成功,接下来配置国内源。
请选择今后brew install的时候访问那个国内镜像,例如阿里巴巴,输入5回车。
1、中科大国内源
2、清华大学国内源
3、北京外国语大学国内源
4、腾讯国内源
5、阿里巴巴国内源 ${tty_reset}"
echo -n "
${tty_blue}请输入序号: "
read MY_DOWN_NUM
echo "${tty_reset}"
case $MY_DOWN_NUM in
"2")
echo "
你选择了清华大学国内源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/
;;
"3")
echo "
北京外国语大学国内源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles
;;
"4")
echo "
你选择了腾讯国内源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles
;;
"5")
echo "
你选择了阿里巴巴国内源
"
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles
;;
*)
echo "
你选择了中国科学技术大学国内源
"
#HomeBrew 下载源 install
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
;;
esac
#写入环境变量到文件
echo "
环境变量写入->${shell_profile}
"
#这里暂时把api写死吧,很多源还没有更新
echo "
export HOMEBREW_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple #ckbrew
export HOMEBREW_API_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api #ckbrew
export HOMEBREW_BOTTLE_DOMAIN=${USER_HOMEBREW_BOTTLE_DOMAIN} #ckbrew
eval \$(${HOMEBREW_REPOSITORY}/bin/brew shellenv) #ckbrew
" >> ${shell_profile}
JudgeSuccess
source "${shell_profile}"
if [ $? -ne 0 ];then
echo "${tty_red}发现错误,${shell_profile} 文件中有错误,建议根据上一句提示修改;
否则会导致提示 permission denied: brew${tty_reset}"
fi
AddPermission ${HOMEBREW_REPOSITORY}
if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
#检测linux curl是否有安装
echo "${tty_red}-检测curl是否安装 留意是否需要输入Y${tty_reset}"
curl -V
if [ $? -ne 0 ];then
sudo apt-get install curl
if [ $? -ne 0 ];then
sudo yum install curl
if [ $? -ne 0 ];then
echo '失败 请自行安装curl 可以参考https://www.howtoing.com/install-curl-in-linux'
error_game_over
fi
fi
fi
fi
echo '
==> 安装完成,brew版本
'
brew -v
if [ $? -ne 0 ];then
echo '发现错误,自动修复一次!'
rm -rf $HOMEBREW_CACHE
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin
brew update-reset
brew -v
if [ $? -ne 0 ];then
error_game_over
fi
else
echo "${tty_green}Homebrew前期配置成功${tty_reset}"
fi
#brew 3.1.2版本 修改了很多地址,都写死在了代码中,没有调用环境变量。。额。。
#ruby下载需要改官方文件
ruby_URL_file=$HOMEBREW_REPOSITORY/Library/Homebrew/cmd/vendor-install.sh
#判断Mac系统版本
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
if version_gt "$macos_version" "10.14"; then
echo "电脑系统版本:$macos_version"
else
echo "${tty_red}检测到你不是最新系统,会有一些报错,请稍等Ruby下载安装;${tty_reset}
"
fi
if [[ -f ${ruby_URL_file} ]]; then
sed -i "" "s/ruby_URL=/ruby_URL=\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/homebrew-bottles\/bottles-portable-ruby\/\$ruby_FILENAME\" \#/g" $ruby_URL_file
fi
else
if [[ -f ${ruby_URL_file} ]]; then
sed -i "s/ruby_URL=/ruby_URL=\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/linuxbrew-bottles\/bottles-portable-ruby\/\$ruby_FILENAME\" \#/g" $ruby_URL_file
fi
fi
if [[ $GIT_SPEED == "" ]];then
echo '
==> brew update-reset
'
brew update-reset
if [[ $? -ne 0 ]];then
brew config
error_game_over
exit 0
fi
else
#极速模式提示Update修复方法
echo "
${tty_red} 极速版本安装完成,${tty_reset} install功能正常,如果需要update功能请自行运行下面三句命令
git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core fetch --unshallow
git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask fetch --unshallow
brew update-reset
"
fi
echo "
${tty_green}Homebrew自动安装程序运行完成${tty_reset}
${tty_green}国内地址已经配置完成${tty_reset}
桌面的Old_Homebrew文件夹,没有你需要的可以删除。
初步介绍几个brew命令
查看版本:brew -v 更新brew版本:brew update
查找:brew search python(其中python替换为要查找的关键字)
安装:brew install python 安装完成输入 python3 -h 查看
本地软件库列表:brew ls
${tty_green}
欢迎右键点击下方地址-打开链接 点个赞吧${tty_reset}
${tty_underline} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset}
"
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#Mac
echo "${tty_red} 安装成功 但还需要重启终端 或者 运行${tty_bold} source ${shell_profile} ${tty_reset} ${tty_red}否则国内地址无法生效${tty_reset}
"
else
#Linux
echo "${tty_red} Linux需要重启电脑 或者暂时运行${tty_bold} source ${shell_profile} ${tty_reset} ${tty_red}否则可能无法使用${tty_reset}
"
fi