ab5c29404a7fffb3635cfb8bade7f5bcf16c688b
[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   # once --verbose is set, --no-verbose can't disable it
32   # do this by hand
33   ORIG_CPANM_OPT="$PERL_CPANM_OPT"
34   PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose//' )"
35
36   # hack-hack-hack
37   LASTEXIT=0
38   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
39     || LASTEXIT=$?
40
41   PERL_CPANM_OPT="$ORIG_CPANM_OPT"
42
43   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
44   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
45
46   if [[ "$LASTEXIT" != "0" ]] || [[ -n "$ERR" ]] ; then
47     echo_err "$(echo -e "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:\n$ERR\n$OUT")"
48     exit 1
49   fi
50
51   # throw away non-children (what was in $@), throw away ascii art, convert to modnames
52   perl -p -e 's/^[a-z].+//i; s/^[^a-z]+//i; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT"
53 }
54
55 parallel_installdeps_notest() {
56   if [[ -z "$@" ]] ; then return; fi
57
58   # flatten list into one string
59   MODLIST=$(echo "$@")
60
61   # The reason we do things so "non-interactively" is that xargs -P will have the
62   # latest cpanm instance overwrite the buildlog. There seems to be no way to
63   # specify a custom buildlog, hence we just collect the verbose output
64   # and display it in case of failure
65   run_or_err "Installing (without testing) $MODLIST" \
66     "echo $MODLIST | xargs -n 1 -P $NUMTHREADS cpanm --notest --no-man-pages"
67 }
68
69
70 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }