Home | History | Annotate | Download | only in devtools
      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 Origin:
     68 $MERGE_MSG
     69 
     70 EOMSG
     71 }
     72 
     73 function preserve_jars() {
     74   JAR_TMP_DIR=`mktemp -d -t prebuilt_update_tmp.XXXXXXXX`
     75   N=0
     76   for i in `find . -type f  | grep -v "^\./\."` ; do
     77     tmpf=`echo $i | tr "./" "__"`
     78     dstf="$JAR_TMP_DIR/$tmpf"
     79     cp "$i" "$dstf"
     80     N=$((N+1))
     81   done
     82   echo "# Copied $N files to" $(basename $JAR_TMP_DIR)
     83 }
     84 
     85 function revert_unchanged_jars() {
     86   local i tmpf dstf tmp_hash local_hash
     87   for i in `find . -type f  | grep -v "^\./\."` ; do
     88     tmpf=`echo $i | tr "./" "__"`
     89     dstf="$JAR_TMP_DIR/$tmpf"
     90     tmp_hash=`get_hash $dstf`
     91     local_hash=`get_hash $i`
     92     if [[ $dst_hash == $src_hash ]]; then
     93       echo "# Revert unchanged file $i"
     94       $DRY cp "$dstf" "$i"
     95     else
     96       echo "!--> Keep changed file $i"
     97     fi
     98   done
     99   if [[ -d $JAR_TMP_DIR ]]; then
    100     echo "# Cleanup" $(basename $JAR_TMP_DIR)
    101     rm -rf $JAR_TMP_DIR
    102   fi
    103 }
    104 
    105 function revert_filter_jars() {
    106   local i j tmpf dstf keep
    107   for i in `find . -type f  | grep -v "^\./\."` ; do
    108     tmpf=`echo $i | tr "./" "__"`
    109     dstf="$JAR_TMP_DIR/$tmpf"
    110     if ! diff -q $dstf $i 1>/dev/null ; then
    111       j=$(basename "$i")
    112       for f in $NAMES_FILTER; do
    113         if [[ "$j" == "$f" || "$j" == "$f.jar" ]]; then
    114           echo "!--> Keep changed file $i"
    115           i=""
    116           break
    117         fi
    118       done
    119       if [[ -f "$i" ]]; then
    120         echo "# Revert file $i"
    121         $DRY cp "$dstf" "$i"
    122       fi
    123     fi
    124   done
    125   if [[ -d $JAR_TMP_DIR ]]; then
    126     echo "# Cleanup" $(basename $JAR_TMP_DIR)
    127     rm -rf $JAR_TMP_DIR
    128   fi
    129 }
    130 
    131 function get_hash() {
    132   # $1: the file to hash
    133   if [[ "${1: -3}" == "jar" ]]; then
    134     # Explanation:
    135     # - unzip -v prints a "verbose" list of a zip's content including each file path, size, timestamp and CRC32
    136     # - we don't want the timestamp so we use sed to first remove the time (12:34) and the date (13-14-15).
    137     # - finally get a md5 of the zip output.
    138     # if the md5 changes, the zip's content has changed (new file, different content size, different CRC32)
    139     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
    140   else
    141     md5sum -b "$1" | cut -d " " -f 1
    142   fi
    143 }
    144 
    145 
    146 if [[ -n $JAR_DETECT ]]; then preserve_jars; fi
    147 for r in base swt; do
    148   update $r
    149 done
    150 if [[ -n $MK_MERGE_MSG ]]; then merge_msg; fi
    151 if [[ $JAR_DETECT == "auto" ]]; then
    152   revert_unchanged_jars
    153 elif [[ $JAR_DETECT == "only" ]]; then
    154   revert_filter_jars
    155 fi
    156 if [[ -n $DRY ]]; then
    157   echo
    158   echo "## WARNING: DRY MODE. Run with -f to actually copy files."
    159 fi
    160 
    161