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