Skip to content

Commit

Permalink
Merge pull request #47 from lmk123/lmk123-patch-1
Browse files Browse the repository at this point in the history
默认使用码云的镜像仓库检查更新 & 下载安装包
  • Loading branch information
lmk123 authored Mar 12, 2020
2 parents 26eac95 + 84364fb commit 9f362a7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ Oh My WeChat 支持下面几条命令:
2. 检测微信有没有安装小助手,没有则安装
3. 最后,打开微信

你还可以运行 `omw -n`,这样会跳过上面检查更新的步骤,优先使用下载过的安装包安装小助手。
你还可以添加下面两个参数:

如果下载安装包很慢,你可以使用 `omw load` 命令(后面有介绍)导入安装包,或者尝试用自己的梯子来下载:
- `-n`:跳过检查更新的步骤,优先使用下载过的安装包安装小助手。
- `-g`:默认情况下,omw 会从[国内的镜像仓库](https://gitee.com/mirrors/wechatextension-formac)检查更新及下载安装包,镜像仓库每天同步一次 [GitHub 仓库](https://github.com/MustangYM/WeChatExtension-ForMac),检查更新时可能不会及时发现最新版本,如果要确保安装最新版本,可以添加 `-g` 参数从 GitHub 仓库检查更新及下载安装包,但由于网络不稳定,可能会失败。

如果从 GitHub 仓库上下载安装包很慢,你可以使用 `omw load` 命令(后面有介绍)导入安装包,或者尝试用自己的梯子来下载:

```bash
# 这里假设你的梯子在你电脑的 1087 端口开了个 HTTP 代理
Expand All @@ -34,7 +37,9 @@ $ omw

### `omw load`

用 omw 从 GitHub 上下载安装包可能会非常慢,所以 Oh My WeChat 还提供了这条命令,可以直接将小助手的安装包导入到 Oh My WeChat 里。
前面说过,默认情况下,omw 会从国内的镜像仓库下载安装包,一般不会出现网络问题,但如果你想从 GitHub 仓库里下载最新版本,可以用这条命令直接将小助手的安装包导入到 Oh My WeChat 里。

> 注意:由于从镜像仓库下载安装包不会有网络问题,所以暂时不支持导入镜像仓库的安装包。
你可以用浏览器打开[小助手的最新版本发布页](https://github.com/MustangYM/WeChatExtension-ForMac/releases/latest),然后点击 _Source code (zip)_ 将安装包下载下来。假设现在最新版本是 v1.8.7,那么此时你的*下载文件夹*内会有一个 _WeChatExtension-ForMac-1.8.7.zip_ 安装包,接下来,你可以在终端内运行下面这条命令导入安装包:

Expand Down
69 changes: 64 additions & 5 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,54 @@ echo_with_date() {
echo "[`date '+%H:%M:%S'`]" $1
}

# 从码云获取最新版本号
get_latest_version_from_gitee() {
curl --retry 2 -s https://gitee.com/mirrors/wechatextension-formac/releases | grep -m 1 -i 'mirrors\/wechatextension-formac\/tree\/v' | sed -n 's/.*\/v\(.*\)" .*/\1/p'
}

# 从 GitHub 获取最新版本号
get_latest_version_from_github() {
curl --retry 2 -I -s https://github.com/MustangYM/WeChatExtension-ForMac/releases/latest | grep -i Location | sed -n 's/.*\/v\(.*\)/\1/p'
}

# 获取 GitHub 安装包的下载地址
get_download_url_from_github() {
echo https://github.com/MustangYM/WeChatExtension-ForMac/archive/v${1}.zip
}

# 获取码云安装包下载地址
get_download_url_from_gitee() {
echo https://gitee.com/mirrors/wechatextension-formac/repository/archive/v${1}?format=zip
}

args_string=$*

get_download_url() {
if [[ $args_string =~ "-g" ]]
then
get_download_url_from_github $1
else
get_download_url_from_gitee $1
fi
}

get_latest_version() {
if [[ $args_string =~ "-g" ]]
then
get_latest_version_from_github
else
get_latest_version_from_gitee
fi
}

# 保存一下 -n 参数,给 install 方法作为参数用
if [[ $* =~ "-n" ]]
then
has_n="-n"
else
has_n=""
fi

# select 提示语
PS3='你的选择:'

Expand Down Expand Up @@ -57,13 +105,16 @@ download() {
echo_with_date ${2}
fi
echo_with_date "开始下载微信小助手 v${1}……"
echo_with_date "如果下载速度很慢,建议通过其它方式下载安装包,然后使用 omw load 命令导入"
echo_with_date "详情请参阅文档 https://github.com/lmk123/oh-my-wechat#omw-load"
# 下载压缩包
curl --retry 2 -L -o ${1}.zip https://github.com/MustangYM/WeChatExtension-ForMac/archive/v${1}.zip
curl --retry 2 -L -o ${1}.zip $(get_download_url $1)
if [[ 0 -eq $? ]]; then
# 解压为同名文件夹
unzip -o -q ${1}.zip
# 如果是从码云下载的压缩包,则需要重命名一下文件名,确保跟 GitHub 的处理逻辑一致
if [[ ! $args_string =~ "-g" ]]
then
mv wechatextension-formac WeChatExtension-ForMac-${1}
fi
# 删除压缩包
rm ${1}.zip
echo_with_date "下载完成"
Expand Down Expand Up @@ -154,8 +205,16 @@ install() {
if [[ $1 == "-n" ]] && [[ -z ${downloaded_version} ]]; then
echo_with_date "未安装微信小助手,也没有下载过安装包,所以即使使用了 -n 参数,仍需要检查并下载新版本"
fi

if [[ $args_string =~ "-g" ]]
then
echo_with_date "由于使用了 -g 参数,将会从 GitHub 仓库检查更新及下载安装包"
echo_with_date "如果检查更新失败或下载速度很慢,建议使用 omw load 命令导入安装包"
echo_with_date "详情请参阅文档 https://github.com/lmk123/oh-my-wechat#omw-load"
fi

echo_with_date "正在查询新版本……"
latest_version=$(curl --retry 2 -I -s https://github.com/MustangYM/WeChatExtension-ForMac/releases/latest | grep -i Location | sed -n 's/.*\/v\(.*\)/\1/p')
latest_version=$(get_latest_version)
if [[ -z "$latest_version" ]]; then
echo_with_date "查询新版本时失败,请稍后重试"
exit 1
Expand Down Expand Up @@ -284,5 +343,5 @@ if [[ $1 == "un" ]]; then
exit 0
fi

install $1
install ${has_n}
open_wechat

0 comments on commit 9f362a7

Please sign in to comment.