1#!/bin/bash
2# Force stop on first error.
3set -e
4if [ $# -ne 2 -a $# -ne 3 ]; then
5    echo "$0 <error prone version> <error prone javac version> [checkerframework version]" >&2
6    exit 1;
7fi
8if [ -z "$ANDROID_BUILD_TOP" ]; then
9    echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2
10    exit 1
11fi
12EP_VERSION="$1"
13JAVAC_VERSION="$2"
14# checkerframework
15CF_VERSION="$3"
16JAR_REPO="https://oss.sonatype.org/service/local/repositories/releases/content/com/google/errorprone"
17EP_JAR_URL="${JAR_REPO}/error_prone_core/${EP_VERSION}/error_prone_core-${EP_VERSION}-with-dependencies.jar"
18EP_ANNO_JAR_URL="${JAR_REPO}/error_prone_annotations/${EP_VERSION}/error_prone_annotations-${EP_VERSION}.jar"
19JAVAC_JAR_URL="${JAR_REPO}/javac/${JAVAC_VERSION}/javac-${JAVAC_VERSION}.jar"
20JAVAC_SOURCES_JAR_URL="${JAR_REPO}/javac/${JAVAC_VERSION}/javac-${JAVAC_VERSION}-sources.jar"
21CF_DATAFLOW_JAR_URL="http://repo1.maven.org/maven2/org/checkerframework/dataflow/${CF_VERSION}/dataflow-${CF_VERSION}.jar"
22CF_DATAFLOW_SOURCES_JAR_URL="http://repo1.maven.org/maven2/org/checkerframework/dataflow/${CF_VERSION}/dataflow-${CF_VERSION}-sources.jar"
23CF_JAVACUTIL_JAR_URL="http://repo1.maven.org/maven2/org/checkerframework/javacutil/${CF_VERSION}/javacutil-${CF_VERSION}.jar"
24CF_JAVACUTIL_SOURCES_JAR_URL="http://repo1.maven.org/maven2/org/checkerframework/javacutil/${CF_VERSION}/javacutil-${CF_VERSION}-sources.jar"
25TOOLS_DIR=$(dirname $0)
26
27function update_jar {
28    typeset VERSION="$1" JAR_URL="$2" DIR="$3" JAR_FILE="$4"
29    typeset JAR_URL_PREFIX=$(dirname $(dirname ${JAR_URL}))
30
31    # Update the version and binary JAR URL.
32    perl -pi -e "s|version: .*|version: \"${VERSION}\"|; s|\"${JAR_URL_PREFIX}.*\"|\"${JAR_URL}\"|" "$DIR/METADATA"
33
34    # Update the last upgrade date
35    perl -pi -e "s|last_upgrade_date.*|last_upgrade_date { year: $(date +%Y) month: $(date +%-m) day: $(date +%-d)}|" "$DIR/METADATA"
36
37    # Get the JAR.
38    wget ${JAR_URL} -O ${DIR}/$(basename ${JAR_URL})
39    wget ${JAR_URL}.sha1 -O ${DIR}/$(basename ${JAR_URL}).sha1
40    wget ${JAR_URL}.asc -O ${DIR}/$(basename ${JAR_URL}).asc
41}
42
43rm -f error_prone/*.jar*
44rm -f javac/*.jar*
45
46update_jar "${EP_VERSION}" "${EP_JAR_URL}" "${TOOLS_DIR}/error_prone"
47update_jar "${EP_VERSION}" "${EP_ANNO_JAR_URL}" "${TOOLS_DIR}/error_prone"
48update_jar "${JAVAC_VERSION}" "${JAVAC_SOURCES_JAR_URL}" "${TOOLS_DIR}/javac"
49update_jar "${JAVAC_VERSION}" "${JAVAC_JAR_URL}" "${TOOLS_DIR}/javac"
50
51# Update the versions for soong
52perl -pi -e "\
53    s|\"(external/error_prone/javac/javac).*\"|\"\\1-${JAVAC_VERSION}.jar\"|;\
54    s|\"(external/error_prone/error_prone/error_prone_core).*\"|\"\\1-${EP_VERSION}-with-dependencies.jar\"|;\
55    s|\"(external/error_prone/error_prone/error_prone_annotations).*\"|\"\\1-${EP_VERSION}.jar\"|;\
56" "$TOOLS_DIR/soong/error_prone.go"
57
58if [ "${CF_VERSION}" != '' ]; then
59  rm -f checkerframework/*.jar*
60  update_jar "${CF_VERSION}" "${CF_DATAFLOW_JAR_URL}" "${TOOLS_DIR}/checkerframework"
61  update_jar "${CF_VERSION}" "${CF_DATAFLOW_SOURCES_JAR_URL}" "${TOOLS_DIR}/checkerframework"
62  update_jar "${CF_VERSION}" "${CF_JAVACUTIL_JAR_URL}" "${TOOLS_DIR}/checkerframework"
63  update_jar "${CF_VERSION}" "${CF_JAVACUTIL_SOURCES_JAR_URL}" "${TOOLS_DIR}/checkerframework"
64  perl -pi -e "\
65    s|\"(external/error_prone/checkerframework/dataflow).*\"|\"\\1-${CF_VERSION}.jar\"|;\
66    s|\"(external/error_prone/checkerframework/javacutil).*\"|\"\\1-${CF_VERSION}.jar\"|;\
67  " "$TOOLS_DIR/soong/error_prone.go"
68fi
69