1717# limitations under the License.
1818
1919# ## Apache RAT license check script ###
20- # This script downloads Apache RAT and runs it to check the license headers of the source files.
20+ set -e
2121
22- set -e # Exit immediately if a command exits with a non-zero status.
23-
24- # Some variables
2522ROOT_DIR=" $( cd " $( dirname " $0 " ) /.." && pwd) "
26- TEMP_DIR=" ${ROOT_DIR} /temp"
23+ TMP_DIR=" $( mktemp -d) "
24+
2725RAT_VERSION=" 0.16.1"
28- RAT_JAR=" ${TEMP_DIR } /apache-rat-${RAT_VERSION} .jar"
26+ RAT_JAR=" ${TMP_DIR } /apache-rat-${RAT_VERSION} .jar"
2927
3028
3129cd " ${ROOT_DIR} "
32- mkdir -p " ${TEMP_DIR} "
3330
3431# Set Java command
3532if [ -x " ${JAVA_HOME} /bin/java" ]; then
3936fi
4037
4138
42- # Download Apache RAT jar file if not exists
43- if [ ! -f " ${RAT_JAR} " ]; then
44- RAT_URL=" https://repo1.maven.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION} /apache-rat-${RAT_VERSION} .jar"
45- JAR_PART=" ${RAT_JAR} .part"
46-
47- echo " Downloading Apache RAT ${RAT_VERSION} ..."
39+ # Download Apache RAT jar
40+ RAT_URL=" https://repo1.maven.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION} /apache-rat-${RAT_VERSION} .jar"
41+ JAR_PART=" ${RAT_JAR} .part"
4842
49- if command -v curl & > /dev/null; then
50- curl -L --silent " ${RAT_URL} " -o " ${JAR_PART} "
51- elif command -v wget & > /dev/null; then
52- wget --quiet " ${RAT_URL} " -O " ${JAR_PART} "
53- else
54- echo " Neither curl nor wget found."
55- exit 1
56- fi
43+ echo " Downloading Apache RAT ${RAT_VERSION} ..."
5744
58- mv " ${JAR_PART} " " ${RAT_JAR} "
5945
46+ if command -v curl & > /dev/null; then
47+ curl -L --silent " ${RAT_URL} " -o " ${JAR_PART} " && mv " ${JAR_PART} " " ${RAT_JAR} "
48+ elif command -v wget & > /dev/null; then
49+ wget --quiet " ${RAT_URL} " -O " ${JAR_PART} " && mv " ${JAR_PART} " " ${RAT_JAR} "
50+ else
51+ echo " Neither curl nor wget found."
52+ exit 1
53+ fi
6054
61- # TODO: Strange phenomenon:its integrity cannot be verified, but it still works normally. (Ignore the check for now)
62- # Check if the JAR file is valid
63- # if ! unzip -tq "${RAT_JAR}" &> /dev/null; then
64- # rm "${RAT_JAR}"
65- # echo "Download ${RAT_JAR} failed or the file is not a valid JAR."
66- # exit 1
67- # fi
6855
69- echo " Downloaded Apache RAT ${RAT_VERSION} successfully."
56+ # Check if the JAR file is valid
57+ unzip -tq " ${RAT_JAR} " & > /dev/null
58+ if [ $? -ne 0 ]; then
59+ rm " ${RAT_JAR} "
60+ echo " Download ${RAT_JAR} failed or the file is not a valid JAR."
61+ exit -1
7062fi
7163
64+ echo " Downloaded Apache RAT ${RAT_VERSION} successfully."
65+
7266# Run Apache RAT
7367echo " Running Apache license check, this may take a while..."
7468
75- " ${java_cmd} " -jar " ${RAT_JAR} " -E " ${ROOT_DIR} /.license-ignore" -d " ${ROOT_DIR} " > " ${TEMP_DIR} /rat-report.txt"
69+ " ${java_cmd} " -jar " ${RAT_JAR} " -E " ${ROOT_DIR} /.license-ignore" -d " ${ROOT_DIR} " > " ${TEMP_DIR} /rat-report.txt"
7670
77- if grep -q " ??" " ${TEMP_DIR} /rat-report.txt" ; then
78- echo >&2 " Could not find Apache license headers in the following files:"
79- grep " ??" " ${TEMP_DIR} /rat-report.txt" >&2
80- exit 1
81- else
82- echo " Apache license check passed."
71+ if [ $? -ne 0 ]; then
72+ echo " RAT exited abnormally"
73+ exit 1
8374fi
8475
85- # Clean up
86- rm -rf " ${TEMP_DIR} "
76+ ERRORS=" $( grep " ??" " ${TEMP_DIR} /rat-report.txt" ) "
77+
78+
79+ if [ -n " $ERRORS " ]; then
80+ echo " Could not find Apache license headers in the following files:"
81+ echo " $ERRORS "
82+ exit 1
83+ else
84+ echo " Apache license check passed."
85+ fi
0 commit comments