Deno安装脚本的解读
install.sh源码 deno的完整安装脚本如下: 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
install.sh源码
deno
的完整安装脚本如下:
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
|
#!/bin/sh
# 当命令的返回值为非零状态时,则立即退出脚本的执行
set -e
# 定义变量os
case $(uname -s) in
Darwin) os="osx" ;;
*) os="linux" ;;
esac
# 定义变量arch
case $(uname -m) in
x86_64) arch="x86_64" ;;
*) arch="other" ;;
esac
# 如果上面得到的 arch 变量不是 x86_64,则退出安装
if [ "$arch" = "other" ]; then
echo "Unsupported architecture $(uname -m). Only x64 binaries are available."
exit
fi
# $# 的意思是,传递给该脚本的参数个数
# 可以通过 $1 $2 ... 按照位置拿到这些参数
# 下面这块代码的目的,是拿到要下载的安装包地址
if [$# -eq 0 ]; then
# 按照readme的安装说明,无参代表安装最新版
# 注意下面管道符的使用,用来流式处理命令的结果
deno_asset_path=$(curl -sSf https://github.com/denoland/deno/releases |
grep -o "/denoland/deno/releases/download/.*/deno_${os}_x64\\.gz" |
head -n 1)
if [ ! "$deno_asset_path" ]; then exit 1; fi
deno_uri="https://github.com${deno_asset_path}"
else
# 安装指定版本,版本号通过参数 $1 获取
deno_uri="https://github.com/denoland/deno/releases/download/${1}/deno_${os}_x64.gz"
fi
# 定义deno二进制文件的安装路径和可执行文件路径
# 根据 issue#40,这个路径在将来可能会发生变化
# https://github.com/denoland/deno_install/issues/40
bin_dir="$HOME/.deno/bin"
exe="$bin_dir/deno"
# 如果目录还不存在,则创建
if [ ! -d "$bin_dir" ]; then
mkdir -p "$bin_dir"
fi
# 下载安装包
# curl 的 -o 选项表示写入上面定义的文件路径中
# curl 的 -# 选项表示显示进度
curl -fL# -o "$exe.gz" "$deno_uri"
# 下载完二进制文件之后,进行解压
gunzip -df "$exe.gz"
# 添加执行权限
chmod +x "$exe"
echo "Deno was installed successfully to $exe"
# 测试 deno 是否在 PATH 中定义
if command -v deno >/dev/null; then
echo "Run 'deno --help' to get started"
else
echo "Manually add the directory to your \$HOME/.bash_profile (or similar)"
echo " export PATH=\"$bin_dir:\$PATH\""
echo "Run '$exe --help' to get started"
fi
|
关于set -e
的作用,参考shell脚本中set -e选项作用范围
关于command
命令的解释,参考shell command命令
注意:
关于curl
命令的-L
选项
1
2
3
4
|
$ curl -h
...
-L, --location Follow redirects
...
|
该选项可以跟随url的重定向,所以对于github上的资源下载是很重要的
我自己在调试的时候,为了简单,没有使用该选项,所以导致下载了错误的文件
Comments