-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathi18n.sh
More file actions
executable file
·288 lines (244 loc) · 8.15 KB
/
Copy pathi18n.sh
File metadata and controls
executable file
·288 lines (244 loc) · 8.15 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env bash
#
# Copyright 2022 Red Hat
#
# 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
#
# https://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.
#
# -------------------------------------------------------
#
# Generates i18n resource bundles for a translation service:
# - Full ZIP: all properties files and HTML previews
# - Delta ZIP: new/changed English base strings and
# previews since a given tag or commit
#
# -------------------------------------------------------
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
VERSION=0.0.1
STAGING_DIR=""
# Change into the script's directory
# Using relative paths is safe!
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
readonly script_dir
cd "${script_dir}"
RESOURCES_BASE="resources/src/main/resources/org/jboss/hal/resources"
OUTPUT_DIR="target/i18n"
usage() {
cat <<EOF
USAGE:
$(basename "${BASH_SOURCE[0]}") [FLAGS] [tag-or-commit]
FLAGS:
-h, --help Prints help information
-v, --version Prints version information
--no-color Uses plain text output
ARGS:
[tag-or-commit] Optional git tag or commit hash to diff against (e.g., v3.7.14)
If provided, a delta ZIP is generated in addition to the full ZIP.
OUTPUT:
${OUTPUT_DIR}/hal-i18n-full-YYYY-MM-DD.zip All English i18n resources (always)
${OUTPUT_DIR}/hal-i18n-delta-<tag>.zip Changes since <tag-or-commit> (if provided)
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
if [[ -n "${STAGING_DIR}" && -d "${STAGING_DIR}" ]]; then
rm -rf "${STAGING_DIR}"
fi
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
# shellcheck disable=SC2034
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
version() {
msg "${BASH_SOURCE[0]} $VERSION"
exit 0
}
parse_params() {
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --version) version ;;
--no-color) NO_COLOR=1 ;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
ARGS=("$@")
[[ ${#ARGS[@]} -le 1 ]] || die "Too many arguments. See --help."
TAG_REF="${ARGS[0]-}"
return 0
}
check_preconditions() {
command -v git >/dev/null 2>&1 || die "git is required but not found"
command -v zip >/dev/null 2>&1 || die "zip is required but not found"
[[ -f "pom.xml" ]] || die "Must be run from the project root (pom.xml not found)"
[[ -d ".git" ]] || die "Not a git repository"
if [[ -n "${TAG_REF}" ]]; then
git rev-parse --verify "${TAG_REF}^{commit}" >/dev/null 2>&1 \
|| die "Tag or commit '${TAG_REF}' not found in git history"
fi
[[ -d "${RESOURCES_BASE}" ]] || die "Resources directory not found: ${RESOURCES_BASE}"
}
prepare_output_dir() {
rm -rf "${OUTPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"
}
generate_full_zip() {
local staging
staging=$(mktemp -d)
STAGING_DIR="${staging}"
mkdir -p "${staging}/properties"
for f in "${RESOURCES_BASE}"/*.properties; do
local basename
basename=$(basename "${f}")
# Skip translated variants (e.g., Constants_fr.properties, Messages_pt_BR.properties, Messages_zh_Hans.properties)
if [[ "${basename}" =~ _[a-zA-Z]+\.properties$ ]]; then
continue
fi
cp "${f}" "${staging}/properties/"
done
# Copy only English previews (exclude translated variants like _fr.html, _ja.html, _zh_cn.html)
mkdir -p "${staging}/previews"
find "${RESOURCES_BASE}/previews" -name '*.html' | while IFS= read -r f; do
local basename
basename=$(basename "${f}")
if [[ "${basename}" =~ _[a-zA-Z]+\.html$ ]]; then
continue
fi
local rel_path
rel_path="${f#"${RESOURCES_BASE}/previews/"}"
local subdir
subdir=$(dirname "${rel_path}")
mkdir -p "${staging}/previews/${subdir}"
cp "${f}" "${staging}/previews/${subdir}/"
done
local zip_name="hal-i18n-full-$(date +%Y-%m-%d).zip"
(cd "${staging}" && zip -r -q "${script_dir}/${OUTPUT_DIR}/${zip_name}" .)
local prop_count html_count
prop_count=$(find "${staging}/properties" -name '*.properties' | wc -l | tr -d ' ')
html_count=$(find "${staging}/previews" -name '*.html' | wc -l | tr -d ' ')
rm -rf "${staging}"
STAGING_DIR=""
msg " ${GREEN}Created${NOFORMAT} ${OUTPUT_DIR}/${zip_name}"
msg " ${prop_count} properties files, ${html_count} HTML preview files"
FULL_ZIP="${zip_name}"
}
generate_delta_zip() {
local staging
staging=$(mktemp -d)
STAGING_DIR="${staging}"
local has_content=false
local delta_key_count=0
# Extract new/changed keys from English base properties
mkdir -p "${staging}/properties"
for base_file in Constants.properties Messages.properties; do
local diff_lines
diff_lines=$(git diff "${TAG_REF}"..HEAD -- "${RESOURCES_BASE}/${base_file}" \
| grep '^+[a-zA-Z]' \
| sed 's/^+//' || true)
if [[ -n "${diff_lines}" ]]; then
echo "${diff_lines}" > "${staging}/properties/${base_file}"
local count
count=$(echo "${diff_lines}" | wc -l | tr -d ' ')
delta_key_count=$((delta_key_count + count))
has_content=true
fi
done
# Find changed English-only HTML previews (exclude translation suffixes)
local changed_previews
changed_previews=$(git diff --name-only "${TAG_REF}"..HEAD -- "${RESOURCES_BASE}/previews/" \
| grep -v '_fr\.html$' \
| grep -v '_ja\.html$' \
| grep -v '_zh_cn\.html$' || true)
local preview_count=0
if [[ -n "${changed_previews}" ]]; then
while IFS= read -r file; do
if [[ -f "${file}" ]]; then
local rel_path
rel_path="${file#"${RESOURCES_BASE}/previews/"}"
local subdir
subdir=$(dirname "${rel_path}")
mkdir -p "${staging}/previews/${subdir}"
cp "${file}" "${staging}/previews/${subdir}/"
preview_count=$((preview_count + 1))
has_content=true
fi
done <<< "${changed_previews}"
fi
if [[ "${has_content}" == false ]]; then
msg " ${CYAN}No changes${NOFORMAT} since ${TAG_REF} - delta ZIP not created"
rm -rf "${staging}"
STAGING_DIR=""
return
fi
# Sanitize tag for filename (replace / with -)
local safe_tag
safe_tag=$(echo "${TAG_REF}" | tr '/' '-')
local zip_name="hal-i18n-delta-${safe_tag}.zip"
(cd "${staging}" && zip -r -q "${script_dir}/${OUTPUT_DIR}/${zip_name}" .)
rm -rf "${staging}"
STAGING_DIR=""
msg " ${GREEN}Created${NOFORMAT} ${OUTPUT_DIR}/${zip_name}"
msg " ${delta_key_count} changed/new property keys, ${preview_count} changed HTML preview files"
DELTA_ZIP="${zip_name}"
}
print_summary() {
msg ""
msg "${GREEN}Summary${NOFORMAT}"
msg "─────────────────────────────────────────"
if [[ -n "${FULL_ZIP-}" ]]; then
local size
size=$(du -h "${OUTPUT_DIR}/${FULL_ZIP}" | cut -f1 | tr -d ' ')
msg " Full: ${OUTPUT_DIR}/${FULL_ZIP} (${size})"
fi
if [[ -n "${DELTA_ZIP-}" ]]; then
local size
size=$(du -h "${OUTPUT_DIR}/${DELTA_ZIP}" | cut -f1 | tr -d ' ')
msg " Delta: ${OUTPUT_DIR}/${DELTA_ZIP} (${size})"
fi
msg ""
}
parse_params "$@"
setup_colors
msg ""
msg "Generating i18n resource bundles..."
if [[ -n "${TAG_REF}" ]]; then
msg " Reference: ${CYAN}${TAG_REF}${NOFORMAT}"
fi
msg ""
check_preconditions
prepare_output_dir
FULL_ZIP=""
DELTA_ZIP=""
msg "Full ZIP (English base resources):"
generate_full_zip
if [[ -n "${TAG_REF}" ]]; then
msg "Delta ZIP (changes since ${TAG_REF}):"
generate_delta_zip
fi
print_summary