|
19 | 19 | ### Apache RAT license check script ### |
20 | 20 | # This script downloads Apache RAT and runs it to check the license headers of the source files. |
21 | 21 |
|
22 | | -set -e # Exit immediately if a command exits with a non-zero status. |
| 22 | +set -e |
23 | 23 |
|
24 | | -# Some variables |
25 | 24 | ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" |
26 | | -TEMP_DIR="${ROOT_DIR}/temp" |
| 25 | +TMP_DIR="/tmp" |
| 26 | + |
27 | 27 | RAT_VERSION="0.16.1" |
28 | | -RAT_JAR="${TEMP_DIR}/apache-rat-${RAT_VERSION}.jar" |
| 28 | +RAT_JAR="${TMP_DIR}/apache-rat-${RAT_VERSION}.jar" |
29 | 29 |
|
30 | 30 |
|
31 | 31 | cd "${ROOT_DIR}" |
32 | | -mkdir -p "${TEMP_DIR}" |
33 | 32 |
|
34 | 33 | # Set Java command |
35 | 34 | if [ -x "${JAVA_HOME}/bin/java" ]; then |
|
39 | 38 | fi |
40 | 39 |
|
41 | 40 |
|
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}..." |
| 41 | +# Download Apache RAT jar |
| 42 | +echo "Downloading Apache RAT ${RAT_VERSION}..." |
48 | 43 |
|
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 |
| 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" |
57 | 46 |
|
58 | | - mv "${JAR_PART}" "${RAT_JAR}" |
| 47 | +if command -v curl &> /dev/null; then |
| 48 | + curl -L --silent "${RAT_URL}" -o "${JAR_PART}" && mv "${JAR_PART}" "${RAT_JAR}" |
| 49 | +elif command -v wget &> /dev/null; then |
| 50 | + wget --quiet "${RAT_URL}" -O "${JAR_PART}" && mv "${JAR_PART}" "${RAT_JAR}" |
| 51 | +else |
| 52 | + echo "Neither curl nor wget found." |
| 53 | + exit 1 |
| 54 | +fi |
59 | 55 |
|
| 56 | +unzip -tq "${RAT_JAR}" > /dev/null |
| 57 | +if [ $? -ne 0 ]; then |
| 58 | + echo "Downloaded Apache RAT jar is invalid" |
| 59 | + exit 1 |
| 60 | +fi |
60 | 61 |
|
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 |
| 62 | +echo "Downloaded Apache RAT ${RAT_VERSION} successfully." |
68 | 63 |
|
69 | | - echo "Downloaded Apache RAT ${RAT_VERSION} successfully." |
70 | | -fi |
71 | 64 |
|
72 | 65 | # Run Apache RAT |
73 | 66 | echo "Running Apache license check, this may take a while..." |
| 67 | +${java_cmd} -jar ${RAT_JAR} -d ${ROOT_DIR} -E "${ROOT_DIR}/.license-ignore" > "${TEMP_DIR}/rat-report.txt" |
74 | 68 |
|
75 | | -"${java_cmd}" -jar "${RAT_JAR}" -E "${ROOT_DIR}/.license-ignore" -d "${ROOT_DIR}" > "${TEMP_DIR}/rat-report.txt" |
76 | 69 |
|
77 | | -if grep -q "??" "${TEMP_DIR}/rat-report.txt"; then |
| 70 | +# Check the result |
| 71 | +if [ $? -ne 0 ]; then |
| 72 | + echo "RAT exited abnormally" |
| 73 | + exit 1 |
| 74 | +elif grep -q "??" "${TEMP_DIR}/rat-report.txt"; then |
78 | 75 | echo >&2 "Could not find Apache license headers in the following files:" |
79 | 76 | grep "??" "${TEMP_DIR}/rat-report.txt" >&2 |
80 | 77 | exit 1 |
81 | 78 | else |
82 | 79 | echo "Apache license check passed." |
83 | 80 | fi |
84 | | - |
85 | | -# Clean up |
86 | | -rm -rf "${TEMP_DIR}" |
0 commit comments