73a8b3c53238666c1f92e5b0af8a0d23232aef95
[dbsrgits/DBIx-Class.git] / maint / travis-ci_scripts / common.bash
1 #!/bin/bash
2
3 set -e
4
5 echo_err() { echo "$@" 1>&2 ; }
6
7 if [[ "$TRAVIS" != "true" ]] ; then
8   echo_err "Running this script makes no sense outside of travis-ci"
9   exit 1
10 fi
11
12 tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
13
14 run_or_err() {
15   echo_err -n "$(tstamp) $1 ... "
16
17   LASTEXIT=0
18   START_TIME=$SECONDS
19   LASTOUT=$( bash -c "$2" 2>&1 ) || LASTEXIT=$?
20   DELTA_TIME=$(( $SECONDS - $START_TIME ))
21
22   if [[ "$LASTEXIT" != "0" ]] ; then
23     echo_err -e "FAILED !!! (after ${DELTA_TIME}s)\nCommand executed:\n$2\nSTDOUT+STDERR:\n$LASTOUT"
24     return $LASTEXIT
25   else
26     echo_err "done (took ${DELTA_TIME}s)"
27   fi
28 }
29
30 extract_prereqs() {
31   # hack-hack-hack
32   LASTEXIT=0
33   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
34     || LASTEXIT=$?
35
36   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
37   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
38
39   if [[ "$LASTEXIT" != "0" ]] || [[ -n "$ERR" ]] ; then
40     echo_err "$(echo -e "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:\n$ERR\n$OUT")"
41     exit 1
42   fi
43
44   # throw away non-children (what was in $@), throw away ascii art, convert to modnames
45   perl -p -e 's/^[a-z].+//i; s/^[^a-z]+//i; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT"
46 }
47
48 parallel_installdeps_notest() {
49   if [[ -z "$@" ]] ; then return; fi
50
51   # flatten list into one string
52   MODLIST=$(echo "$@")
53
54   # The reason we do things so "non-interactively" is that xargs -P will have the
55   # latest cpanm instance overwrite the buildlog. There seems to be no way to
56   # specify a custom buildlog, hence we just collect the verbose output
57   # and display it in case of failure
58   run_or_err "Installing (without testing) $MODLIST" \
59     "echo $MODLIST | xargs -n 1 -P $NUMTHREADS cpanm --verbose --no-interactive --notest --no-man-pages"
60 }
61
62
63 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }