Use common fail() instead of exit
[aquarium] / make-dist.sh
1 #!/usr/bin/env bash
2
3 # For debugging
4 clear
5 # set -x
6 LOG=build.log
7
8 fail() {
9     echo "failed while $1"
10     echo "last log lines are"
11     tail -n 15 $LOG
12     echo "see file $LOG for details"
13     cleanup
14     exit 1
15 }
16
17 cleanup() {
18     git checkout master 2>>$LOG 1>>$LOG
19     if [ -f stashed ]; then
20         git stash pop 2>>$LOG 1>>$LOG
21         rm stashed
22     fi
23 }
24
25 # Check if there are pending changes
26 status=`git status --porcelain|grep -v "??"`
27 if [ ! -z "$status" ]; then
28     echo "The following files are not committed:"
29     echo 
30     echo $status
31     echo
32     echo "Stashing them"
33     git stash save 2>&1 >>$LOG
34     touch stashed
35 fi
36
37 # Get latest tag
38 tag=`git tag |tail -n 1`
39
40 DIR="$tag"
41
42 # Creating dist dirs
43 mkdir -p $DIR
44 mkdir -p $DIR/bin
45 mkdir -p $DIR/lib
46 mkdir -p $DIR/conf
47 mkdir -p $DIR/logs
48
49 echo "Checking out $tag"
50 git checkout $tag 2>>$LOG 1>>$LOG || fail "checking out"
51
52 echo "Building $tag"
53 mvn clean install -DskipTests=true >>build.log || fail "building project"
54
55 echo "Collecting dependencies"
56 mvn dependency:copy-dependencies >> build.log ||  fail "collecting dependencies"
57 cp target/dependency/*.jar $DIR/lib || fail "copying dependencies"
58
59 echo "Copying Aquarium classes"
60 cp target/$tag.jar $DIR/lib || fail "copying $tag.jar"
61
62 echo "Copying scripts and config files"
63 cp aquarium.sh $DIR/bin || fail "copying aquarium.sh"
64
65 echo "Creating archive"
66 tar zcvf $tag.tar.gz $DIR >> build.log 2>&1 || fail "creating archive"
67
68 echo "Cleaning up"
69 rm -Rf $DIR
70 cleanup
71 rm $LOG
72
73 echo "File $tag.tar.gz created succesfully"
74 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :