-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
180 lines (142 loc) · 3.35 KB
/
run.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
#!/bin/bash
# 初始化上下文。有参数的
initContext(){
# dist目录
source_dir=dist
# 获取参数,beta环境和pro环境
if [ $# -gt 0 ] && [ $1 = 'beta' ];then
# beta环境远程仓库地址,
git_url=git@beta/example.git
# 本地根目录存放打包代码
dest=".deploy/beta"
# npm 的脚本名,对应package.json定义的script
node_script=build_beta
else
# pro环境远程仓库地址
git_url=git@pro/example.git
# 本地根目录
dest=".deploy/pro"
# npm 的脚本名,对应package.json定义的script
node_script=build_pro
fi
}
# 初始化git目录,pull最新代码
init(){
echo +++init start;
if [ ! -d $dest ]; then
git clone $git_url $dest
fi
# 记录现在的目录位置,pwd获取本地路径
cur=`pwd`
# 进入git目录
cd $dest
# git checkout .
git add .
git stash
# reset为线上最新版本,要先pull一下再reset。
git pull origin master
git reset --hard origin/master
# 然后再pull一下
git pull origin master
# 回到原来的目录
cd $cur
echo ---init end;
}
# 重置dist目录
resetDist(){
echo +++resetDist start
rsync -a --delete --exclude='.git' $dest/. ./dist
echo ---resetDist end
}
# 构建
build(){
echo +++build start
npm run $node_script
echo ---build end
}
# 检查是否成功
checkBuild(){
if [[ ! -f $source_dir/index.html || ! -d $source_dir/static ]]; then
echo error
else
echo ok
fi
}
# 到$dest目录
cpCode(){
echo +++cpCode start
# 复制代码,所有文件包含隐藏文件
rsync -r --delete --exclude='.git' $source_dir/. $dest
echo ---cpCode end
}
# 提交到远程git仓库
commit(){
echo +++commit start
# 记录现在的目录位置,最后要回来的
cur=`pwd`
# 进入git目录
cd $dest
# 提交的字符串
commit_str="commited in `date '+%Y-%m-%d_%H:%M:%S'`"
git add .
git commit -am "${commit_str}"
git push origin master
# 回到原来的目录
cd $cur
echo ---commit end
}
# 显示帮助信息
help(){
# 微信h5版本
echo ----微信h5版本--------
echo ./run.sh build "#"构建代码
echo ./run.sh init "#"初始化git仓库
echo ./run.sh commit "#"提交到git
echo ./run.sh "#"执行全部任务
echo ./run.sh hello "#"hello
echo ./run.sh test "#"test
echo ./run.sh beta "#"一键构建和提交beta版本
# app内嵌版本
echo ----app内嵌版本--------
echo ./run.sh app "#"一键构建和提交app版本
echo ----帮助信息--------
echo ./run.sh help "#"帮助
}
# 测试用的
test(){
echo "a test empty task"
}
# 入口
if [[ $# -lt 1 || $1 = 'app' || $1 = 'beta' ]]; then
# 无参数则打pro包,否则打相应类型的包
if [ $# -lt 1 ];then
type=pro
else
type=$1
fi
echo ===\>准备构建${type}版
initContext $type && init && resetDist
# 构建代码
buildRes=$(build)
# 检查构建结果
echo -e "$buildRes"
if [[ $buildRes =~ "ERROR" ]]; then
echo "$(tput setaf 1)xxx\>build error,task abort$(tput sgr0)"
else
# 代码构建成功才继续。
checkRes=$(checkBuild)
if [ $checkRes == "ok" ];then
cpCode && commit
echo "$(tput setaf 2)===\>task complete$(tput sgr0)"
else
echo "$(tput setaf 1)xxx\>build error,task abort$(tput sgr0)"
fi
fi
elif [ $1 ]; then
# 参数不是包类型的,当中函数处理
echo ===\>准备执行${1}函数
initContext beta
func=$1
$func
echo ===\>task complete
fi