blob: ffa7c3be9dc521df0c131aa55c8b5f7a7e1d6caf [file] [log] [blame]
Raphael Mollb595e192013-01-07 12:13:57 -08001#!/bin/bash
2
Raphael Moll98411e22013-02-15 13:07:19 -08003set -e # fail on errors
4
Raphael Mollf4450032013-02-20 16:30:21 -08005if [[ $(uname) == "Darwin" ]]; then
6 PROG_DIR=$(dirname "$0")
7else
8 PROG_DIR=$(readlink -f $(dirname "$0"))
9fi
Raphael Moll98411e22013-02-15 13:07:19 -080010cd "$PROG_DIR"
11
12DRY="echo" # default to dry mode unless -f is specified
Raphael Moll98411e22013-02-15 13:07:19 -080013MK_MERGE_MSG="1" # 1 to update the MERGE_MSG, empty to do not generate it
Raphael Moll40617922013-04-03 09:45:23 -070014MERGE_MSG="" # msg to generate
Raphael Moll21630502013-04-12 17:56:52 -070015JAR_DETECT=""
Raphael Mollf0bc00d2013-04-16 14:27:12 -070016NAMES_FILTER=""
Raphael Mollb595e192013-01-07 12:13:57 -080017
Raphael Moll5e46fc92013-01-15 18:06:01 -080018while [[ -n "$1" ]]; do
19 if [[ "$1" == "-f" ]]; then
20 DRY=""
Raphael Moll98411e22013-02-15 13:07:19 -080021 elif [[ "$1" == "-m" ]]; then
22 MK_MERGE_MSG=""
Raphael Moll21630502013-04-12 17:56:52 -070023 elif [[ "$1" == "-u" ]]; then
Raphael Mollf0bc00d2013-04-16 14:27:12 -070024 JAR_DETECT="auto"
25 elif [[ "$1" == "-o" ]]; then
26 JAR_DETECT="only"
27 elif [[ $JAR_DETECT == "only" && $1 =~ ^[a-z]+ ]]; then
28 NAMES_FILTER="$NAMES_FILTER $1"
Raphael Moll5e46fc92013-01-15 18:06:01 -080029 else
30 echo "Unknown argument: $1"
Raphael Mollf0bc00d2013-04-16 14:27:12 -070031 echo "Usage: $0 [-f] [-m] [-u | -o name1.jar ... nameN.jar]"
Raphael Moll40617922013-04-03 09:45:23 -070032 echo " -f: actual do thing. Default is dry-run."
Raphael Moll98411e22013-02-15 13:07:19 -080033 echo " -m: do NOT generate a .git/MERGE_MSG"
Raphael Moll21630502013-04-12 17:56:52 -070034 echo " -u: detect and git-revert unchanged JAR files"
Raphael Mollf0bc00d2013-04-16 14:27:12 -070035 echo " -o: only keep the given *leaf* filenames (.jar can be omitted)"
Raphael Moll5e46fc92013-01-15 18:06:01 -080036 exit 1
37 fi
38 shift
39done
40
Raphael Mollf0bc00d2013-04-16 14:27:12 -070041if [[ $JAR_DETECT == "only" && -z "$NAMES_FILTER" ]]; then
42 echo "Error: -o must be followed by names of files to keep."
43 exit 1
44fi
45
Raphael Moll40617922013-04-03 09:45:23 -070046function update() {
Raphael Mollb595e192013-01-07 12:13:57 -080047 echo
Raphael Mollcba8e112013-02-04 15:21:35 -080048 local repo=$1
Raphael Mollcba8e112013-02-04 15:21:35 -080049
Raphael Mollf0bc00d2013-04-16 14:27:12 -070050 echo "# Build tools/$repo"
51
Raphael Moll40617922013-04-03 09:45:23 -070052 local SHA1=$( cd ../../tools/$repo ; git show-ref --head --hash HEAD )
53 MERGE_MSG="$MERGE_MSG
54tools/$repo: @ $SHA1"
Raphael Moll98411e22013-02-15 13:07:19 -080055
Xavier Ducrohet2c3da1f2013-05-08 11:17:10 -070056 ( $DRY cd ../../tools/$repo && $DRY ./gradlew clean publishLocal pushDistribution )
Raphael Mollb595e192013-01-07 12:13:57 -080057}
58
Raphael Moll98411e22013-02-15 13:07:19 -080059function merge_msg() {
60 local dst=.git/MERGE_MSG
61 if [[ -n $DRY ]]; then
62 echo "The following would be output to $dst (use -m to prevent this):"
63 dst=/dev/stdout
64 fi
65 cat >> $dst <<EOMSG
66Update SDK prebuilts.
Xavier Ducrohet33a29492013-08-28 18:13:41 -070067
Raphael Moll40617922013-04-03 09:45:23 -070068Origin:
69$MERGE_MSG
Raphael Moll98411e22013-02-15 13:07:19 -080070
Raphael Moll98411e22013-02-15 13:07:19 -080071EOMSG
72}
73
Raphael Moll21630502013-04-12 17:56:52 -070074function preserve_jars() {
Raphael Mollf0bc00d2013-04-16 14:27:12 -070075 JAR_TMP_DIR=`mktemp -d -t prebuilt_update_tmp.XXXXXXXX`
76 N=0
77 for i in `find . -type f | grep -v "^\./\."` ; do
78 tmpf=`echo $i | tr "./" "__"`
79 dstf="$JAR_TMP_DIR/$tmpf"
80 cp "$i" "$dstf"
81 N=$((N+1))
Raphael Moll21630502013-04-12 17:56:52 -070082 done
Raphael Mollf0bc00d2013-04-16 14:27:12 -070083 echo "# Copied $N files to" $(basename $JAR_TMP_DIR)
Raphael Moll21630502013-04-12 17:56:52 -070084}
85
86function revert_unchanged_jars() {
Raphael Mollf0bc00d2013-04-16 14:27:12 -070087 local i tmpf dstf tmp_hash local_hash
88 for i in `find . -type f | grep -v "^\./\."` ; do
89 tmpf=`echo $i | tr "./" "__"`
90 dstf="$JAR_TMP_DIR/$tmpf"
91 tmp_hash=`get_hash $dstf`
92 local_hash=`get_hash $i`
Raphael Moll21630502013-04-12 17:56:52 -070093 if [[ $dst_hash == $src_hash ]]; then
94 echo "# Revert unchanged file $i"
Raphael Mollf0bc00d2013-04-16 14:27:12 -070095 $DRY cp "$dstf" "$i"
Raphael Moll21630502013-04-12 17:56:52 -070096 else
Raphael Mollf0bc00d2013-04-16 14:27:12 -070097 echo "!--> Keep changed file $i"
Raphael Moll21630502013-04-12 17:56:52 -070098 fi
99 done
Raphael Mollf0bc00d2013-04-16 14:27:12 -0700100 if [[ -d $JAR_TMP_DIR ]]; then
101 echo "# Cleanup" $(basename $JAR_TMP_DIR)
102 rm -rf $JAR_TMP_DIR
103 fi
Raphael Moll21630502013-04-12 17:56:52 -0700104}
105
Raphael Mollf0bc00d2013-04-16 14:27:12 -0700106function revert_filter_jars() {
107 local i j tmpf dstf keep
108 for i in `find . -type f | grep -v "^\./\."` ; do
109 tmpf=`echo $i | tr "./" "__"`
110 dstf="$JAR_TMP_DIR/$tmpf"
111 if ! diff -q $dstf $i 1>/dev/null ; then
112 j=$(basename "$i")
113 for f in $NAMES_FILTER; do
114 if [[ "$j" == "$f" || "$j" == "$f.jar" ]]; then
115 echo "!--> Keep changed file $i"
116 i=""
117 break
118 fi
119 done
120 if [[ -f "$i" ]]; then
121 echo "# Revert file $i"
122 $DRY cp "$dstf" "$i"
123 fi
124 fi
125 done
126 if [[ -d $JAR_TMP_DIR ]]; then
127 echo "# Cleanup" $(basename $JAR_TMP_DIR)
128 rm -rf $JAR_TMP_DIR
129 fi
130}
Raphael Moll21630502013-04-12 17:56:52 -0700131
Raphael Mollf0bc00d2013-04-16 14:27:12 -0700132function get_hash() {
133 # $1: the file to hash
134 if [[ "${1: -3}" == "jar" ]]; then
135 # Explanation:
136 # - unzip -v prints a "verbose" list of a zip's content including each file path, size, timestamp and CRC32
137 # - we don't want the timestamp so we use sed to first remove the time (12:34) and the date (13-14-15).
138 # - finally get a md5 of the zip output.
139 # if the md5 changes, the zip's content has changed (new file, different content size, different CRC32)
140 unzip -v $1 | sed -n -e "/[0-9][0-9]:[0-9][0-9]/s/[0-9][0-9]:[0-9][0-9]// ; s/[0-9][0-9]-[0-9][0-9]-[0-9][0-9]//p" | md5sum -b | cut -d " " -f 1
141 else
142 md5sum -b "$1" | cut -d " " -f 1
143 fi
Raphael Moll21630502013-04-12 17:56:52 -0700144}
145
146
147if [[ -n $JAR_DETECT ]]; then preserve_jars; fi
Raphael Moll5e46fc92013-01-15 18:06:01 -0800148for r in base swt; do
Raphael Moll40617922013-04-03 09:45:23 -0700149 update $r
Raphael Moll5e46fc92013-01-15 18:06:01 -0800150done
Raphael Moll21630502013-04-12 17:56:52 -0700151if [[ -n $MK_MERGE_MSG ]]; then merge_msg; fi
Raphael Mollf0bc00d2013-04-16 14:27:12 -0700152if [[ $JAR_DETECT == "auto" ]]; then
153 revert_unchanged_jars
154elif [[ $JAR_DETECT == "only" ]]; then
155 revert_filter_jars
156fi
Raphael Moll5e46fc92013-01-15 18:06:01 -0800157if [[ -n $DRY ]]; then
158 echo
159 echo "## WARNING: DRY MODE. Run with -f to actually copy files."
160fi
161