-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrelease.sh
executable file
·163 lines (138 loc) · 4.73 KB
/
release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash -i
# Copyright 2025 Google Inc. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
readonly SCRIPT_NAME=$(basename "$0")
readonly MAVEN_PROJECTS="parent main shaded unshaded externs"
deploy_to_sonatype=true
sonatype_auto_release=false
bazel_executable=""
bazel_bin=$(pwd)/bazel-bin
bazel_define_flag=""
usage() {
echo ""
echo "${SCRIPT_NAME}: Build and release script for Closure Compiler."
echo ""
echo "Usage: ${SCRIPT_NAME} [--no-deploy] [--sonatype-auto-release] [--help]"
echo ""
echo "Options:"
echo " --help Print this help output and exit."
echo " --no-deploy Prepare all the maven artifacts but skip the deployment to Sonatype."
echo " --sonatype-auto-release Automatically release the staging repository on Sonatype after deployment."
echo ""
echo "Use the environment variable RELEASE_NUM to specify the release number:"
echo " RELEASE_NUM=v1234567890 ./${SCRIPT_NAME}"
}
parse_arguments() {
while [[ "$#" -gt 0 ]]; do
case "$1" in
--no-deploy)
deploy_to_sonatype=false
shift
;;
--sonatype-auto-release)
sonatype_auto_release=true
shift
;;
--help)
usage
exit 0
;;
*)
echo "Error: unexpected option '$1'"
usage
exit 1
;;
esac
done
}
check_prerequisites() {
if [[ ! -f "MODULE.bazel" ]]; then
echo "Error: This script must be run from the root of the Bazel repository (where MODULE.bazel exists)."
exit 1
fi
if ! command -v mvn &> /dev/null; then
echo "Error: Maven is not installed or not in your PATH."
echo "Please install Maven to proceed."
exit 1
fi
# Check for Bazel or Bazelisk
if command -v bazelisk &> /dev/null; then
bazel_executable="bazelisk"
elif command -v bazel &> /dev/null; then
bazel_executable="bazel"
else
echo "Error: Neither Bazel nor Bazelisk is installed or in your PATH."
echo "Please install either Bazel or Bazelisk to proceed."
exit 1
fi
# TODO: consider passing the release number as argument ?
# Check if the global variable RELEASE_NUM exists
if [[ -z "${RELEASE_NUM}" ]]; then
echo "Warning: Global variable RELEASE_NUM is not set. Creating a snapshot release."
else
echo "Creating release ${RELEASE_NUM}."
bazel_define_flag="--define=COMPILER_VERSION=${RELEASE_NUM}"
fi
}
build_and_prepare() {
local project="$1"
local bazel_target=":compiler_${project}_bundle"
local mvn_artifact_id="closure-compiler-${project}"
if [[ "${project}" == "shaded" ]]; then
mvn_artifact_id="closure-compiler"
fi
echo "Processing project: ${project}"
# Build the bundle jar file using Bazel or Bazelisk
echo "Building Bazel target: ${bazel_target}"
"${bazel_executable}" build "${bazel_target}" ${bazel_define_flag}
# Prepare the artifact for Maven
cd "${mvn_temp_wd}"
echo "Extracting bundle content: ${bazel_bin}/${mvn_artifact_id}_bundle.jar to ${mvn_temp_wd}"
jar xf "${bazel_bin}/${mvn_artifact_id}_bundle.jar"
# If a main JAR file exist, extract its content in the `classes` directory
# maven will repackage everything in its own jar.
if [[ -f "${mvn_artifact_id}.jar" ]]; then
echo "Extracting: ${mvn_artifact_id}.jar in the classes directory."
mkdir "${mvn_artifact_id}-classes"
jar xf "${mvn_artifact_id}.jar" -C "${mvn_artifact_id}-classes"
rm "${mvn_artifact_id}.jar"
fi
cd - > /dev/null # Go back to the original directory
}
package_and_deploy() {
cd "${mvn_temp_wd}"
if [[ "${deploy_to_sonatype}" == true ]]; then
echo "Deploying Maven artifacts to Sonatype..."
mvn -f closure-compiler-parent.pom.xml deploy "-DautoReleaseAfterClose=${sonatype_auto_release}"
rm -rf "${mvn_temp_wd}"
else
echo "Packaging Maven artifacts..."
mvn -f closure-compiler-parent.pom.xml package
echo "Maven artifacts created in: ${mvn_temp_wd}"
fi
cd - > /dev/null # Go back to the original directory
}
# --- Main Script ---
parse_arguments "$@"
check_prerequisites
echo "Starting build and release process."
mvn_temp_wd=$(mktemp -d)
for project in ${MAVEN_PROJECTS}; do
build_and_prepare "${project}" "${mvn_temp_wd}"
echo ""
done
package_and_deploy
echo "Build and release process completed."
exit 0