Mac提升生产力的工具

Posted by Lucky Xue on 2020-05-02

解决Homebrew下载更新极慢的问题

近期使用Homebrew去下载安装软件的时候总是卡在update阶段,时间非常久,难以忍受。记录一下解决方法,使用Homebrew安装软件的时候一直卡在Update阶段。同时发现从github.com下载文件也极度缓慢(几十KB/s)。

问题定位:
使用brew update --verbose观察update过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
brew update --verbose
Checking if we need to fetch /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
Fetching /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
remote: Enumerating objects: 337, done.
remote: Counting objects: 100% (337/337), done.
remote: Compressing objects: 100% (88/88), done.
remote: Total 298 (delta 221), reused 287 (delta 210), pack-reused 0
Receiving objects: 100% (298/298), 50.91 KiB | 39.00 KiB/s, done.
Resolving deltas: 100% (221/221), completed with 39 local objects.
From https://github.com/Homebrew/homebrew-core
65a45a9..583b7f1 master -> origin/master
remote: Enumerating objects: 179429, done.
remote: Counting objects: 100% (179429/179429), done.
remote: Compressing objects: 100% (56607/56607), done.
Receiving objects: 4% (7628/177189), 1.48 MiB | 8.00 KiB/s

解决问题:
由于问题主要是在国内网络环境github下载慢,因此尝试:
更换使用国内的homebrew镜像源;
使用代理访问github.com;
更换Homebrew源;

使用以下命令更换国内中科大上的homebrew镜像:

1
2
3
4
5
6
7
8
9
# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 替换homebrew-bottles:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

使用以下命令更换国内阿里云上的homebrew镜像:

1
2
3
4
5
6
7
8
9
# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
# 替换homebrew-bottles:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 替换后,问题依旧,继续查看日志:
brew update --verbose
Checking if we need to fetch /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
Fetching /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
From https://mirrors.aliyun.com/homebrew/homebrew-core
+ 583b7f1...8435590 master -> origin/master (forced update)
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Fetching /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
remote: Enumerating objects: 179429, done.
remote: Counting objects: 100% (179429/179429), done.
remote: Compressing objects: 100% (56607/56607), done.
Receiving objects: 6% (11170/177189), 2.16 MiB | 30.00 KiB/s

可以看到由于homebrew-cask的仓库依然指向了Github,这个过程还是慢。阿里云的镜像站没有提供homebrew-cask,进一步搜索找到USTC镜像站,该站提供了homebrew-cask的源。使用上述同样的命令更换源:

1
2
3
# 替换homebrew-cask.git:
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

Note: Homebrew Bottles源的更换方法详见这里:
官方源地址:
https://github.com/Homebrew/brew.git
https://github.com/Homebrew/homebrew-core.git
https://github.com/Homebrew/homebrew-cask

PS: 最近阿里的Homebrew源经常会出现404问题,最后只能去官方获取源码编译安装,这种方式非常耗时也没有必要

Mac下unzip解压文件名乱码问题

定位问题:
zip为windows平台常用压缩文件格式,在团队协作中,经常会碰到同事发过来的zip格式压缩文件。我们在mac一般利用unzip来解压,在文件名全是英文的情况可以很好的解决问题,但是如果文件名包含中文,则会出现乱码问题,本文旨在提供一个最简单的方式解决该问题。

解决问题:
为什么unzip不能handle这种情况?
unzip有很多的版本,而适合用在MAC OS上的版本很多不支持指定编码格式。这样就会导致unzip只能使用系统默认编码格式,而windows上压缩时一般采用GBK或者GB2312进行编码,若你的mac的编码格式与zip的不一致则会导致乱码问题。主要原因还是unzip不支持自定义指定源文件编码格式。
所以解决方案是寻找一个支持指定自定义编码格式的解压缩命令工具即可,unar则是一款优秀的支持多重格式的解压工具。
Mac下zip文件解压乱码如何解决呢?
首先安装Homebrew (The missing package manager for macOS)

1
2
3
4
5
# 安转unar
brew install unar
# 解压目标文件
unar -e GBK yourfile.zip
# -e指定源文件编码格式,而目标文件编码格式会根据系统的locale自动获取

brew后台启动进程服务问题