-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkt
executable file
·175 lines (161 loc) · 5.19 KB
/
mkt
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
#!/usr/bin/env bash
# vim:ts=4:sw=4:expandtab
############################################################
# NAME
# mkt - a toy for run target's stop command
#
# SYNOPSIS
# mkt
# mkt [ options ] [ targets ] ...
#
# debug=on mkt ...
#
# OPTIONS
# -C DIRECTORY Change to DIRECTORY before doing anything.
# -f FILE Read FILE as a makefile.
# -h Print this message and exit.
#
# -m MODULENAME Use MODULENAME as module-name for each target.
# -d DEPLOYPATH Use DEPLOYPATH as deploy-path for each target.
#
# AUTHORS
# neiku project <[email protected]>
#
# SEE ALSO
# mkxrc_commands
# mkxrc_targets
# mkxrc_modules
#
# VERSION
# 2015/11/21: 简单封装mkrun,支持关闭
# 命令行或者makefile中OUTPUT定义的target
# 2015/12/03: 支持目录递归查找target
# 2016/02/16: 支持自定义默认target变量名(key => output-name)
# (默认使用OUTPUT做为默认target变量名)
# 2016/05/01: 支持自定义命令行
# 支持透传-m/-d选项给mkrun
# 2017/01/16: 支持跨平台
# - 通过env程序+PATH变量动态查询bash,控制权
# 、准确性都得到保障
#
############################################################
# help
function help()
{
echo "Usage: mk [ options ] [ targets ] ..."
echo "Options:"
echo " -C DIRECTORY Change to DIRECTORY before doing anything."
echo " -f FILE Read FILE as a makefile."
echo " -h Print this message and exit."
echo ""
echo " -m MODULENAME Use MODULENAME as module-name for each target."
echo " -d DEPLOYPATH Use DEPLOYPATH as deploy-path for each target."
echo ""
echo "Report bugs to <[email protected]>"
}
# mkt -C {makedir} -f {makefile} -m {MODULENAME} -d {DEPLOYPATH} {cmdline_targets}
progname="mkt"
makedir=""
makefile=""
cmdline_options_m=""
cmdline_options_d=""
cmdline_targets=""
cmdline_forwards=""
# parse cmdline
mklog debug "origin-args:[$@]"
temp=$(getopt -o "C:f:hm:d:" --long "" -n "$progname" -- "$@")
if [ $? != 0 ] ; then
echo "`help`" >&2
exit 1
fi
eval set -- "$temp"
mklog debug "parsed-args:[$temp]"
while true
do
case "$1" in
-C) makedir="$2" ; shift 2 ;;
-f) makefile="$2" ; shift 2 ;;
-h) echo "`help`" >&2; exit 0;;
-m) cmdline_options_m="$2" ; shift 2 ;;
-d) cmdline_options_d="$2" ; shift 2 ;;
--) shift ; break ;;
*) echo "parse options error!" >&2 ; exit 1 ;;
esac
done
cmdline_targets="$@"
mklog debug "makedir:[$makedir], makefile:[$makefile]," \
"cmdline_targets:[$cmdline_targets]," \
"cmdline_options_m:[$cmdline_options_m], cmdline_options_d:[$cmdline_options_d]"
# cmdline forwards
if [ -n "$cmdline_options_m" ] ; then
cmdline_forwards="$cmdline_forwards -m$cmdline_options_m"
fi
if [ -n "$cmdline_options_d" ] ; then
cmdline_forwards="$cmdline_forwards -d$cmdline_options_d"
fi
# wrapper
succ_exit() { [ -n "$makedir" ] && echo "$progname: Leaving directory '$makedir'"; exit 0; }
fail_exit() { [ -n "$makedir" ] && echo "$progname: Leaving directory '$makedir'"; exit 1; }
# go into make directory if need
if [ -n "$makedir" ] ; then
if [ ! -d "$makedir" ] ; then
mklog error "check directory fail, directory:[$makedir]"
exit 1
fi
echo "$progname: Entering directory '$makedir'"
cd "$makedir" || fail_exit
fi
# get makefile from cmdline(by user) or make(auto load)
if [ -z "$makefile" ] ; then
makefile="`make $cmdline_targets -n -p 2>/dev/null \
| grep '^MAKEFILE_LIST' \
| head -n1 \
| awk '{printf $3}'`"
if [ -z "$makefile" ] ; then
mklog error "makefile not found, make args:[$cmdline_targets]"
fail_exit
fi
fi
if [ ! -f "$makefile" ] ; then
mklog error "check makefile fail, makefile:[$makefile]"
fail_exit
fi
mklog debug "makefile:[$makefile]"
# get make's internal database
makedata="`make -f $makefile $cmdline_targets -n -p 2>/dev/null`"
# maybe mkt for sub directorys
submakedirs="`echo "$makedata" | grep '^DIRS =' | tail -n1 | cut -c8-`"
mklog debug "submakedirs=$submakedirs"
if [ -n "$submakedirs" ] ; then
for subdir in $submakedirs
do
if [ "${subdir:0:1}" = "/" ] ; then
mkt -C $subdir $cmdline_forwards
else
mkt -C `pwd`/$subdir $cmdline_forwards
fi
done
mklog debug "$progname for directorys end"
succ_exit
fi
# get targets makefile if need
if [ -z "$cmdline_targets" ] ; then
# default targets from $output var in makefile
output="`mkm get config output-name OUTPUT`"
length="$((${#output} + 4))"
makefile_targets="`echo "$makedata" | grep "^$output =" | cut -c$length-`"
if [ -z "$makefile_targets" ] ; then
mklog error "targets not found, make args:[$cmdline_options]," \
"output-name:[$output], length:[$length]"
fail_exit
fi
cmdline_targets="$makefile_targets"
fi
# run target's stop command
mkrun stop $cmdline_targets $cmdline_forwards
if [ $? -ne 0 ] ; then
mklog error "mkrun stop fail, target:[$cmdline_targets], forwards:[$cmdline_forwards]"
fail_exit
fi
# all done
succ_exit