d0db0953cab4837a538e102f4d0c3fba647f0aba
[m6w6/btr] / share / btr / btr.sh
1 #!/bin/sh
2
3 function help {
4 echo "btr v0.3.0, (c) Michael Wallner <mike@php.net>"
5 echo
6 echo "Usage: $(basename $0) [-hyvqcC] [<options>]"
7 echo
8 echo " -h, --help Display this help"
9 echo " -y, --yes Always assume yes"
10 echo " -v, --verbose Be more verbose"
11 echo " -q, --quiet Be more quiet"
12 echo " -c, --clean Clean build"
13 echo " -C, --vcsclean Clean repo/branch"
14 echo
15 echo " Options:"
16 echo " -f, --config=<file> Read configuration from a file"
17 echo " -s, --source=<rules> Use the specified source ruleset"
18 echo " -b, --build=<rules> Use the specified build ruleset"
19 echo " -r, --report=<rules> Use the specifued report ruleset"
20 echo " -T, --test=<args> Provide test runner arguments"
21 echo " -B, --branch=<branch> Checkout this branch"
22 echo " -D, --directory=<directory> Use this directory as work root"
23 echo " -S, --suffix=<suffix> Append suffix to the build name"
24 echo
25 echo " Rules format:"
26 echo " type=argument e.g: git=git@github.com:m6w6/btr.git"
27 echo " irc=irc://btr@chat.freenode.org/#btr"
28 echo " mail=\"-c copy@to rcpt@to\""
29 echo " notify-send=\"-u low\""
30 echo
31 echo " Note though, that some rules do not use any argument."
32 echo
33 echo " Rulesets:"
34 for ruleset in source build report
35 do
36 printf " %10s: %s\n" $ruleset \
37 "$(find "$LIBDIR/$ruleset" -name '*.mk' -exec basename {} .mk \; | sort | xargs)"
38 done
39 echo
40 echo " Examples:"
41 echo
42 echo " Clone PHP's git, use PHP-5.5 branch, build with php ruleset and"
43 echo " run the test suite with valgrind (-m) on a debug build and report"
44 echo " the results with a simple OSD notification:"
45 echo " $ btr -s git=git@php.net:php/php-src.git -B PHP-5.5 \\"
46 echo " -b \"php=--enable-debug\" -T-m -r notify-send"
47 echo " See also php.example.conf"
48 echo
49 echo " Clone CURL's git (use master), build with GNU autotools"
50 echo " ruleset which runs 'make check' and mail the report to the"
51 echo " current user. Verbosely show all actions taken:"
52 echo " $ btr -v -s git=https://github.com/bagder/curl.git -b gnu -r mail"
53 echo " See also curl.example.conf"
54 echo
55 exit
56 }
57
58 function parseopts {
59 local shortoptions="hvqycCf:T:B:D:S:s:b:r:"
60 local longoptions="help,verbose,quiet,yes,clean,vcsclean,config:,test:,branch:,directory:,suffix:,source:,build:,report:"
61 local options=$(getopt \
62 --options "$shortoptions" \
63 --longoptions "$longoptions" \
64 -- "$@" \
65 )
66
67 if test $? -ne 0 ; then
68 help
69 fi
70
71 eval set -- "$options"
72
73 while test $# -gt 0
74 do
75 case "$1" in
76 -h|--help)
77 help
78 ;;
79 -v|--verbose)
80 QUIET=false
81 VERBOSE=true
82 ;;
83 -q|--quiet)
84 QUIET=true
85 VERBOSE=false
86 ;;
87 -y|--yes)
88 FORCEYES=true
89 ;;
90 -c|--clean)
91 BUILD_CLEAN=true
92 ;;
93 -C|--vcsclean)
94 SOURCE_CLEAN=true
95 ;;
96 ####
97 -f|--config)
98 source "$2"
99 shift
100 ;;
101 ####
102 -B|--branch)
103 BRANCH="$2"
104 shift
105 ;;
106 -D|--directory)
107 BTRDIR="$2"
108 shift
109 ;;
110 -S|--suffix)
111 SUFFIX="$2"
112 shift
113 ;;
114 -T|--test)
115 TEST_ARGS="$2"
116 shift
117 ;;
118 ####
119 -s|--source)
120 case "$2" in
121 git*)
122 test -z "$BRANCH" && BRANCH=master
123 ;;
124 svn*)
125 test -z "$BRANCH" && BRANCH=trunk
126 ;;
127 cvs*)
128 test -z "$BRANCH" && BRANCH=HEAD
129 ;;
130 esac
131 SOURCE_RULES="$(cut -d= -f1 <<<$2)"
132 SOURCE_ARGS="$(cut -s -d= -f2- <<<$2)"
133 shift
134 ;;
135 -b|--build)
136 BUILD_RULES="$(cut -d= -f1 <<<$2)"
137 BUILD_ARGS="$(cut -s -d= -f2- <<<$2)"
138 shift
139 ;;
140 -r|--report)
141 REPORT_RULES="$(cut -d= -f1 <<<$2)"
142 REPORT_ARGS="$(cut -s -d= -f2- <<<$2)"
143 shift
144 ;;
145 ####
146 --)
147 # legacy
148 if test "$2"
149 then
150 SOURCE_ARGS="$2"
151 fi
152 shift
153 ;;
154 esac
155 shift
156 done
157 }
158
159 function error {
160 echo "$@" >&2
161 exit
162 }
163
164 function setup {
165 if test -z "$SOURCE_RULES" -o -z "$BUILD_RULES" -o -z "$REPORT_RULES"
166 then
167 help
168 fi
169
170 if $VERBOSE
171 then
172 QUIET_FLAG=
173 SILENT_FLAG=
174 VERBOSE_FLAG="-v"
175 SAY="echo; echo"
176 elif $QUIET
177 then
178 QUIET_FLAG="-q"
179 SILENT_FLAG="-s"
180 VERBOSE_FLAG=
181 SAY="@true"
182 else
183 QUIET_FLAG=
184 SILENT_FLAG="-s"
185 VERBOSE_FLAG=
186 SAY="@echo"
187 fi
188
189 export QUIET VERBOSE FORCEYES QUIET_FLAG SILENT_FLAG VERBOSE_FLAG SAY
190
191 if test -z "$BTRDIR"
192 then
193 export BTRDIR="/tmp/btr"
194 else
195 export BTRDIR=$(realpath "$BTRDIR")
196 fi
197
198 mkdir -p "$BTRDIR" || error "Could not create $BTRDIR"
199
200
201 export SOURCE_RULES BUILD_RULES REPORT_RULES
202 test -z "$SOURCE_ARGS" || export SOURCE_ARGS
203 test -z "$SOURCE_CLEAN" || export SOURCE_CLEAN
204 test -z "$BUILD_ARGS" || export BUILD_ARGS
205 test -z "$BUILD_CLEAN" || export BUILD_CLEAN
206 test -z "$TEST_ARGS" || export TEST_ARGS
207 test -z "$REPORT_ARGS" || export REPORT_ARGS
208 REPO=$(basename $(sed -re 's~^.*[/:#]~~' <<<"$SOURCE_ARGS") .git)
209 SAFE_BRANCH=$(tr ":" "_" <<<$(basename "$BRANCH"))
210 export REPO BRANCH SAFE_BRANCH
211
212 if test -z "$SUFFIX"
213 then
214 export BUILD="$REPO@$SAFE_BRANCH"
215 else
216 export BUILD="$REPO@$SAFE_BRANCH-$SUFFIX"
217 fi
218
219 export CLEAN_DIR="btr+clean-$REPO"
220 export BRANCH_DIR="btr+branch-$REPO@$SAFE_BRANCH"
221 export BUILD_DIR="btr+build-$BUILD"
222 export CONFIG_REPORT="btr+config-$BUILD-$DATE"
223 export BUILD_REPORT="btr+build-$BUILD-$DATE"
224 export TEST_REPORT="btr+tests-$BUILD-$DATE"
225 export LAST_REPORT=$(basename $(ls -t "$BTRDIR/btr+tests-$BUILD"* 2>/dev/null | head -n1) 2>/dev/null)
226 export REPORT="btr+report-$BUILD-$DATE"
227 }
228
229 function show_conf {
230 echo
231 echo "Configuration:"
232 echo "=============="
233 echo
234 echo "BTRDIR = $BTRDIR"
235 echo "BINDIR = $BINDIR"
236 echo "LIBDIR = $LIBDIR"
237 echo
238 echo "SOURCE_RULES = $SOURCE_RULES"
239 echo "SOURCE_ARGS = $SOURCE_ARGS"
240 echo "SOURCE_CLEAN = $SOURCE_CLEAN"
241 echo "BUILD_RULES = $BUILD_RULES"
242 echo "BUILD_ARGS = $BUILD_ARGS"
243 echo "BUILD_CLEAN = $BUILD_CLEAN"
244 echo "TEST_ARGS = $TEST_ARGS"
245 echo "REPORT_RULES = $REPORT_RULES"
246 echo "REPORT_ARGS = $REPORT_ARGS"
247 echo
248 echo "REPO = $REPO"
249 echo "BRANCH = $BRANCH"
250 echo "SAFE_BRANCH = $SAFE_BRANCH"
251 echo
252 echo "CLEAN_DIR = $CLEAN_DIR"
253 echo "BRANCH_DIR = $BRANCH_DIR"
254 echo "BUILD_DIR = $BUILD_DIR"
255 echo "CONFIG_REPORT = $CONFIG_REPORT"
256 echo "BUILD_REPORT = $BUILD_REPORT"
257 echo "TEST_REPORT = $TEST_REPORT"
258 echo "LAST_REPORT = $LAST_REPORT"
259 echo
260 }
261
262 function confirm {
263 local CONTINUE
264 echo -n "$1 (y/N) "
265 read -r CONTINUE
266 case $CONTINUE in
267 y*|Y*)
268 echo
269 ;;
270 *)
271 exit -1
272 ;;
273 esac
274 }
275
276 # vim: noet