Raphael Moll | b595e19 | 2013-01-07 12:13:57 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 3 | set -e # fail on errors |
| 4 | |
Raphael Moll | f445003 | 2013-02-20 16:30:21 -0800 | [diff] [blame] | 5 | if [[ $(uname) == "Darwin" ]]; then |
| 6 | PROG_DIR=$(dirname "$0") |
| 7 | else |
| 8 | PROG_DIR=$(readlink -f $(dirname "$0")) |
| 9 | fi |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 10 | cd "$PROG_DIR" |
| 11 | |
| 12 | DRY="echo" # default to dry mode unless -f is specified |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 13 | MK_MERGE_MSG="1" # 1 to update the MERGE_MSG, empty to do not generate it |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 14 | MERGE_MSG="" # msg to generate |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 15 | JAR_DETECT="" |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 16 | NAMES_FILTER="" |
Raphael Moll | b595e19 | 2013-01-07 12:13:57 -0800 | [diff] [blame] | 17 | |
Raphael Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 18 | while [[ -n "$1" ]]; do |
| 19 | if [[ "$1" == "-f" ]]; then |
| 20 | DRY="" |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 21 | elif [[ "$1" == "-m" ]]; then |
| 22 | MK_MERGE_MSG="" |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 23 | elif [[ "$1" == "-u" ]]; then |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 24 | 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 Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 29 | else |
| 30 | echo "Unknown argument: $1" |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 31 | echo "Usage: $0 [-f] [-m] [-u | -o name1.jar ... nameN.jar]" |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 32 | echo " -f: actual do thing. Default is dry-run." |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 33 | echo " -m: do NOT generate a .git/MERGE_MSG" |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 34 | echo " -u: detect and git-revert unchanged JAR files" |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 35 | echo " -o: only keep the given *leaf* filenames (.jar can be omitted)" |
Raphael Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 36 | exit 1 |
| 37 | fi |
| 38 | shift |
| 39 | done |
| 40 | |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 41 | if [[ $JAR_DETECT == "only" && -z "$NAMES_FILTER" ]]; then |
| 42 | echo "Error: -o must be followed by names of files to keep." |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 46 | function update() { |
Raphael Moll | b595e19 | 2013-01-07 12:13:57 -0800 | [diff] [blame] | 47 | echo |
Raphael Moll | cba8e11 | 2013-02-04 15:21:35 -0800 | [diff] [blame] | 48 | local repo=$1 |
Raphael Moll | cba8e11 | 2013-02-04 15:21:35 -0800 | [diff] [blame] | 49 | |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 50 | echo "# Build tools/$repo" |
| 51 | |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 52 | local SHA1=$( cd ../../tools/$repo ; git show-ref --head --hash HEAD ) |
| 53 | MERGE_MSG="$MERGE_MSG |
| 54 | tools/$repo: @ $SHA1" |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 55 | |
Xavier Ducrohet | 2c3da1f | 2013-05-08 11:17:10 -0700 | [diff] [blame] | 56 | ( $DRY cd ../../tools/$repo && $DRY ./gradlew clean publishLocal pushDistribution ) |
Raphael Moll | b595e19 | 2013-01-07 12:13:57 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 59 | function 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 |
| 66 | Update SDK prebuilts. |
Xavier Ducrohet | 33a2949 | 2013-08-28 18:13:41 -0700 | [diff] [blame] | 67 | |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 68 | Origin: |
| 69 | $MERGE_MSG |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 70 | |
Raphael Moll | 98411e2 | 2013-02-15 13:07:19 -0800 | [diff] [blame] | 71 | EOMSG |
| 72 | } |
| 73 | |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 74 | function preserve_jars() { |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 75 | 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 Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 82 | done |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 83 | echo "# Copied $N files to" $(basename $JAR_TMP_DIR) |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | function revert_unchanged_jars() { |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 87 | 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 Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 93 | if [[ $dst_hash == $src_hash ]]; then |
| 94 | echo "# Revert unchanged file $i" |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 95 | $DRY cp "$dstf" "$i" |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 96 | else |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 97 | echo "!--> Keep changed file $i" |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 98 | fi |
| 99 | done |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 100 | if [[ -d $JAR_TMP_DIR ]]; then |
| 101 | echo "# Cleanup" $(basename $JAR_TMP_DIR) |
| 102 | rm -rf $JAR_TMP_DIR |
| 103 | fi |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 106 | function 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 Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 131 | |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 132 | function 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 Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | if [[ -n $JAR_DETECT ]]; then preserve_jars; fi |
Raphael Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 148 | for r in base swt; do |
Raphael Moll | 4061792 | 2013-04-03 09:45:23 -0700 | [diff] [blame] | 149 | update $r |
Raphael Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 150 | done |
Raphael Moll | 2163050 | 2013-04-12 17:56:52 -0700 | [diff] [blame] | 151 | if [[ -n $MK_MERGE_MSG ]]; then merge_msg; fi |
Raphael Moll | f0bc00d | 2013-04-16 14:27:12 -0700 | [diff] [blame] | 152 | if [[ $JAR_DETECT == "auto" ]]; then |
| 153 | revert_unchanged_jars |
| 154 | elif [[ $JAR_DETECT == "only" ]]; then |
| 155 | revert_filter_jars |
| 156 | fi |
Raphael Moll | 5e46fc9 | 2013-01-15 18:06:01 -0800 | [diff] [blame] | 157 | if [[ -n $DRY ]]; then |
| 158 | echo |
| 159 | echo "## WARNING: DRY MODE. Run with -f to actually copy files." |
| 160 | fi |
| 161 | |