This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildtools.sh
142 lines (135 loc) · 5.83 KB
/
buildtools.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
#!/bin/bash
# Copyright (C) 2015-2016 Tim Janik, MPL-2.0
set -e # abort on errors
SCRIPTNAME=`basename $0`;
function die { e="$1" ; shift ; [ -n "$*" ] && echo "$SCRIPTNAME: $*" >&2 ; exit "${e:-127}" ; } # die <exitcode> [message]
# == config ==
mkconfig() # print shell variables describing package, version, commit id, monotonic revision
{
set -e # abort on errors
REVISIONSUFFIX="$1" # may be empty
# uses: .git/ $DISTRELEASE $TRAVIS_JOB_NUMBER
# ensure we're in the project directory and have full git history
SCRIPTPATH=`readlink -f $0`
SCRIPTDIR=`dirname "$SCRIPTPATH"`
pushd "$SCRIPTDIR" >/dev/null # cd PACKAGE/
# extract configure.ac:AC_INIT package and version
test -r configure.ac || die 7 "missing configure.ac"
test -z "$PACKAGE" &&
PACKAGE=`sed -nr "/^AC_INIT\b/{ s/^[^(]*\([ ]*([^, ]+).*/\1/; s/\[|]//g; p; }" configure.ac`
test -z "$UPSVERSION" &&
UPSVERSION=`sed -nr "/^AC_INIT\b/{ s/^[^,]*,[^0-9]*([0-9.]*).*/\1/; p; }" configure.ac`
[[ $UPSVERSION =~ ^[0-9.]+$ ]] || die 6 "failed to detect package version"
[[ $UPSVERSION =~ [13579]$ ]] && DEVELOPMENT=true || DEVELOPMENT=false # spot unreleased odd devel versions
# commit ID and changelog message
COMMITID=`git rev-parse HEAD`
if $DEVELOPMENT ; then
CHANGELOGMSG="Development snapshot, git commit $COMMITID"
else
CHANGELOGMSG="Release snapshot, git commit $COMMITID"
fi
# upstream version details
gitdir=`git rev-parse --git-dir`
if test -s "$gitdir/shallow" ; then
echo "$SCRIPTNAME: warning shallow repository, run: git fetch --unshallow" >&2
TOTAL_COMMITS='0'
else
TOTAL_COMMITS=`git rev-list --count HEAD` # count commits to provide a monotonically increasing revision
fi
if $DEVELOPMENT ; then
UPSDETAIL="~git$TOTAL_COMMITS" # sort *before* UPSDETAIL="" (pre-release candidates)
else
UPSDETAIL="+git$TOTAL_COMMITS" # sort *after* UPSDETAIL="" (post release patchlevel)
fi
# build revision parts
REVISIONSUFFIX="-${REVISIONSUFFIX:-0${DISTRELEASE:-local}}" # ensure suffix to avoid non-native-package-with-native-version
BUILDREV="$REVISIONSUFFIX"
test -z "$TRAVIS_JOB_NUMBER" || BUILDREV="$BUILDREV~travis${TRAVIS_JOB_NUMBER/*./}"
# complete version variants
UPLOADVERSION="$UPSVERSION$UPSDETAIL$REVISIONSUFFIX"
DEBVERSION="$UPSVERSION$UPSDETAIL$BUILDREV"
# print variables after all errors have been checked for
cat <<-__EOF
PACKAGE=$PACKAGE
UPSVERSION=$UPSVERSION
BUILDREV=$BUILDREV
DEVELOPMENT=$DEVELOPMENT
COMMITID=$COMMITID
CHANGELOGMSG="$CHANGELOGMSG"
__EOF
test $TOTAL_COMMITS = 0 || cat <<-__EOF
TOTAL_COMMITS=$TOTAL_COMMITS
UPSDETAIL=$UPSDETAIL
UPLOADVERSION=$UPLOADVERSION
DEBVERSION=$DEBVERSION
__EOF
popd >/dev/null # cd OLDPWD
}
# == ifdevel ==
ifdevel() # Usage: ifdevel <develvalue> [alternative]
{
mkconfig >/dev/null # PACKAGE, UPSVERSION, DEVELOPMENT, ...
if $DEVELOPMENT ; then
echo "$1"
else
echo "$2"
fi
}
# == bintrayup ==
bintrayup() # Usage: bintrayup <bintrayaccount> <packagepath> <packagedistribution> [packages...]
{
set +x # avoid printing authentication secrets
mkconfig >/dev/null # PACKAGE, UPSVERSION, ...
ACCNAME="$1"; PKGPATH="$2"; PKGDIST="$3" # BINTRAY_APITOKEN must be set by caller
test -n "$ACCNAME" || die 1 "missing bintray account"
test -n "$PKGPATH" || die 1 "missing package path"
test -n "$PKGDIST" || die 1 "missing distribution"
shift 3
# create new bintray versoin
REPOVERSION="$UPLOADVERSION" # echo "REPOVERSION=$REPOVERSION"
echo " REMOTE " "creating new version: $REPOVERSION"
curl -d "{ \"name\": \"$REPOVERSION\", \"released\": \"`date -I`\", \"desc\": \"Automatic CI Build\" }" \
-u"$ACCNAME:$BINTRAY_APITOKEN" "https://api.bintray.com/packages/$ACCNAME/$PKGPATH/versions" \
-H"Content-Type: application/json" -f && EX=$? || EX=$?
test $EX = 0 -o $EX = 22 # 22 indicates HTTP responses >= 400, the version likely already exists
# upload individual files
# NOTE: we cannot use "explode=1" b/c files may have different architectures, which
# need to be passed corrctly in the upload to regenerate the Packages index.
URL="https://api.bintray.com/content/$ACCNAME/$PKGPATH/$REPOVERSION"
OPTS="deb_distribution=$PKGDIST;deb_component=main;explode=0;override=0;publish=1"
ALLOK=0
for F in "$@" ; do
S="${F%.deb}"; A="${S##*_}"
test ! -z "$A" || continue
echo " REMOTE " "uploading: $F ($A)"
curl -T "$F" -u"$ACCNAME:$BINTRAY_APITOKEN" "$URL/`basename $F`;$OPTS;deb_architecture=$A" -f && EX=$? || EX=$?
ALLOK=$(($ALLOK + $EX))
done
test $ALLOK = 0 || die 2 "Some files failed to upload"
}
# == applyenv ==
applyenv() # Usage: applyenv <inputfile> [inputargs...]
{
# NOTE: avoid using unprefixed environment variables, they could become output substitutions
applyenv__input___="$1"; shift
test -r "$applyenv__input___" || { echo "$0: failed to read: $applyenv__input___"; exit 1; }
applyenv__sedprogram___=""
for applyenv__word___ in \
$(grep -oE '@[0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ]+@' "$applyenv__input___" | sort | uniq)
do
applyenv__varname___=${applyenv__word___//@/} # word=@EXAMPLE@ varname=EXAMPLE
if [[ ${!applyenv__varname___} == *\|* ]]; then
echo "$0: detected '|', skipping variable: \$$applyenv__varname___" >&2
elif test "$applyenv__word___" == "@0@"; then
applyenv__sedprogram___="$applyenv__sedprogram___; s|$applyenv__word___|${applyenv__input___}|g"
else
applyenv__sedprogram___="$applyenv__sedprogram___; s|$applyenv__word___|${!applyenv__varname___}|g"
fi
done
sed "$applyenv__sedprogram___" "$applyenv__input___"
}
# == commands ==
[[ "$1" != config ]] || { shift; mkconfig "$@" ; exit $? ; }
[[ "$1" != ifdevel ]] || { shift; ifdevel "$@" ; exit $? ; }
[[ "$1" != bintrayup ]] || { shift; bintrayup "$@" ; exit $? ; }
[[ "$1" != applyenv ]] || { shift; applyenv "$@" ; exit $? ; }