-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautify_rro.sh
283 lines (234 loc) · 10.7 KB
/
beautify_rro.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
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
#!/usr/bin/bash
#
# Copyright (C) 2022 The LineageOS Project
#
# SPDX-License-Identifier: Apache-2.0
#
DEBUG=0
if [[ ${DEBUG} != 0 ]]; then
log="/dev/tty"
else
log="/dev/null"
fi
if [[ -z ${1} ]]; then
echo usage: beautify_rro.sh /path/to/rro_source [/path/to/original_source]
exit
fi
ANDROID_ROOT="../../.."
RRO_DIR="${1}"
SRC_DIR=""
targetPackage=$(sed -n "s/.*targetPackage=\"\([a-z.]\+\)\".*/\1/gp" ${RRO_DIR}/AndroidManifest.xml)
if [[ ! -z ${2} ]]; then
SRC_DIR=${2}
else
echo "Guessing source for $targetPackage" > ${log}
case "${targetPackage}" in
"android")
SRC_DIR=${ANDROID_ROOT}/frameworks/base/core/res
;;
"com.android.systemui")
SRC_DIR=${ANDROID_ROOT}/frameworks/base/packages/SystemUI
;;
"com.android.providers.settings")
SRC_DIR=${ANDROID_ROOT}/frameworks/base/packages/SettingsProvider
;;
*)
SRC_DIR=$(rg "package=\"${targetPackage}\"" ${ANDROID_ROOT}/packages/ | grep -v install-in-user-type | sed "s/://g" | awk '{print $1}' | sed "s/\(..\/..\/..\/[a-zA-Z0-9]\+\/[a-zA-Z0-9]\+\/[a-zA-Z0-9]\+\).*/\1/g" | head -1)
;;
esac
echo "Guessed source: $SRC_DIR" > ${log}
fi
if [[ -z ${SRC_DIR} ]] || [[ ! -d ${SRC_DIR} ]]; then
echo "Could not find source for $targetPackage, last guess: ${SRC_DIR}"
exit
else
echo "Using source: $SRC_DIR" > ${log}
fi
# Create a temporary working directory
TMPDIR=$(mktemp -d)
function get_src_path () {
# Allow space between "name" and "="
# Ignore symbols.xml and overlayable.xml files since these don't contain the actual values
# Also ignore values-mcc as these folders include carrier specific things
name_search="$(echo ${name} | sed "s/name=/name[ ]*=/g")"
src_path=$(grep -rG "${name_search}" ${SRC_DIR} | grep -v symbols.xml | grep -v overlayable.xml | sed "s/://g" | awk '{print $1}' | grep -v "\-mcc" | LC_ALL=c sort | head -1)
}
function add_aosp_comments () {
local file=${1}
# Create a backup
cp ${file} ${TMPDIR}/$(basename ${file}).bak
for name in $(grep -r "name=" ${file} | sed "s/translatable=\"false\"/ /g" | sed "s/[<>]/ /g" | sed "s/.*\(name=\"[-._a-Z0-9]\+\"\).*/\1/g" | sed "s/\"/\\\\\"/g"); do
if ! grep -qr ${name} ${SRC_DIR}; then
echo "[$(basename ${RRO_DIR})] Resource $(echo ${name} | sed "s/.*\"\([a-Z0-9_.]\+\)\\\.*/\1/g") not found in $(echo ${SRC_DIR} | sed "s/$(echo ${ANDROID_ROOT}/ | sed "s/\//\\\\\//g")//g")"
continue
fi
get_src_path
if [[ ! -f ${src_path} ]]; then
echo "src_path: $src_path does not exist, skipping" > ${log}
continue
fi
# Is the string translatable?
if [[ ! -z $(sed -n "/${name_search}/p" ${src_path} | sed -n "/translatable=\"false\"/p") ]] && [[ -z $(sed -n "/${name}/p" ${file} | sed -n "/translatable=\"false\"/p") ]]; then
sed -i "s/${name_search}/${name} translatable=\"false\"/g" ${file}
fi
line=$(sed -n "/.*${name_search}.*/=" ${src_path} | head -1)
if [[ -z $(sed -n "$(expr ${line} - 1)p" ${src_path} | sed -n "/.*-->.*/p") ]]; then
echo "Did not find ending string before ${name} in ${src_path}" > ${log}
continue
fi
line=$(sed -n "/.*${name}.*/=" ${file} | head -1)
if [[ ! -z $(sed -n "$(expr ${line} - 1)p" ${file} | sed -n "/.*-->.*/p") ]]; then
echo "There is already a comment for ${name} in ${file}, skipping" > ${log}
continue
fi
# Drop everything after our item
sed "/${name_search}/q" ${src_path} > "${TMPDIR}"/before.txt
# Search for the last "<!--" before the item and write from there up to the item
sed -n "$(sed -n /\<\!--/= "${TMPDIR}"/before.txt | tail -1),\$p" "${TMPDIR}"/before.txt | head -n -1 > "${TMPDIR}"/comment.txt
# Add empty line above comment, skip if this is the first value in this file
line=$(sed -n "/.*${name}.*/=" ${file} | head -1)
if [[ ! ${line} -eq $(grep -Pn -m 1 "<[-A-Za-z]+ name=" ${file} | grep -Po "^[0-9]+") ]]; then
sed -i '1s/^/\n/' "${TMPDIR}"/comment.txt
fi
# Insert the comment above the item
sed -i "$(expr ${line} - 1) r ${TMPDIR}/comment.txt" ${file}
done
if ! xmllint --format ${file} &> /dev/null; then
echo "We broke ${file}. Restoring backup"
cp ${TMPDIR}/$(basename ${file}).bak ${file}
fi
rm ${TMPDIR}/$(basename ${file}).bak
}
function init_file () {
local name=${1}
if [[ -f ${folder}/${name} ]]; then
return
fi
printf -- "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" >> ${folder}/${name}
printf -- "<!--\n" >> ${folder}/${name}
printf -- " Copyright (C) $(date +%Y) The LineageOS Project\n" >> ${folder}/${name}
printf -- " SPDX-License-Identifier: Apache-2.0\n" >> ${folder}/${name}
printf -- "-->\n" >> ${folder}/${name}
printf -- "<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n" >> ${folder}/${name}
}
function update_header () {
local name=${1}
# Use xliff document 1.2 namespace
sed -i "s/.*<resources.*/<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">/g" ${folder}/${name}
if [[ -z $(sed -n "/?xml/p" ${folder}/${name}) ]]; then
sed -i "1 i\<?xml version=\"1.0\" encoding=\"utf-8\"?>" ${folder}/${name}
fi
if [[ -z $(sed -n "/Copyright (C)/p" ${folder}/${name}) ]]; then
sed -i "2 i\-->" ${folder}/${name}
sed -i "2 i\ SPDX-License-Identifier: Apache-2.0" ${folder}/${name}
sed -i "2 i\ Copyright (C) $(date +%Y) The LineageOS Project" ${folder}/${name}
sed -i "2 i\<!--" ${folder}/${name}
fi
}
function open_resource_file () {
local name=${1}
sed -i "/<\/resources>/d" ${folder}/${name}
}
function close_resource_file () {
local name=${1}
if xmllint --format ${file} &> /dev/null || [[ ! -z $(tail -n 1 ${folder}/${name} | sed -n "/<\/resources>/p") ]]; then
return
fi
printf "</resources>\n" >> ${folder}/${name}
}
function trim_file () {
local name=${1}
sed -ni "/[ ]*<[a-Z0-9/].*/p" ${folder}/${name}
}
for folder in $(find ${RRO_DIR}/res -maxdepth 1 -mindepth 1 -type d); do
# Prepare files
for file in $(find ${folder} -maxdepth 1 -mindepth 1 -type f -name "*.xml"); do
# Merge arrays into one line
xml_pp -s record_c ${file} | sponge ${file}
trim_file $(basename ${file})
# Merge strings into one line
sed -z "s/\\n/\\\n/g" ${file} | sed -z "s/>\\\n/>\n/g" | sponge ${file}
open_resource_file $(basename ${file})
done
# Move the resources into files matching the aosp location
for file in $(find ${folder} -maxdepth 1 -mindepth 1 -type f -name "*.xml"); do
# Don't move elements in files that don't contain resources
if [[ -z $(sed -n "/^<resources/p" ${file}) ]]; then
continue
fi
for name in $(grep -r "name=" ${file} | sed "s/[<>]/ /g" | sed "s/.*\(name=\"[-._a-Z0-9]\+\"\).*/\1/g" | sed "s/\"/\\\\\"/g"); do
if ! grep -qr ${name} ${SRC_DIR}; then
echo "[$(basename ${RRO_DIR})] Resource $(echo ${name} | sed "s/.*\"\([a-Z0-9_.]\+\)\\\.*/\1/g") not found in $(echo ${SRC_DIR} | sed "s/$(echo ${ANDROID_ROOT}/ | sed "s/\//\\\\\//g")//g")"
continue
fi
get_src_path
if [[ ! -f ${src_path} ]]; then
echo "src_path: $src_path does not exist, skipping" > ${log}
continue
fi
destination_filename=$(basename ${src_path})
if [[ $(basename ${file}) == ${destination_filename} ]]; then
continue
fi
# Create file if necessary
init_file ${destination_filename}
# Move the string into the file
sed -n "/${name}/p" ${file} >> ${folder}/${destination_filename}
sed -i "/${name}/d" ${file}
done
done
# Sort the files
for file in $(find ${folder} -maxdepth 1 -mindepth 1 -type f -name "*.xml"); do
close_resource_file $(basename ${file})
update_header $(basename ${file})
if ! xmllint --format ${file} &> /dev/null; then
echo "${file} is not a valid XML, broke the rro"
continue
fi
# Don't sort files that don't contain resources
if [[ ! -z $(sed -n "/^<resources/p" ${file}) ]]; then
if [[ -z $(sed -n "/name=\"/p" ${file}) ]]; then
echo "${file} is empty after moving resources, remove it" > ${log}
rm ${file}
continue
fi
for name in $(grep -r "name=" ${file} | sed "s/[<>]/ /g" | sed "s/\///g" | sed "s/.*\(name=\"[-._a-Z0-9]\+\"\).*/\1/g" | sed "s/\"/\\\\\"/g"); do
get_src_path
if [[ ! -f ${src_path} ]]; then
line=0
else
line=$(grep -Pn -m 1 "${name}" ${src_path} | grep -Po "^[0-9]+")
fi
# Temporary add line number as prefix to the line to sort it
sed -i "s/\(.*${name}.*\)/${line}\1/g" ${file}
done
# Sort the resources according to their line numbers in aosp
first_real_line=$(grep -Pn -m 1 "<[-A-Za-z]+ name=" ${file} | grep -Po "^[0-9]+")
(head -n $(expr ${first_real_line} - 1) ${file} && (tail -n+${first_real_line} ${file} | head -n -1) | LC_ALL=c sort -n && tail -n 1 ${file}) | sponge ${file}
# Drop the line number prefix again
sed -i "s/[0-9]\+\( .*\)/\1/g" ${file}
fi
# Expand arrays again
XMLLINT_INDENT=" " xmllint --format ${file} | sponge ${file}
add_aosp_comments ${file}
# Replace "\> with " \> to follow the recommended style
sed -i "s/\"\/>/\" \/>/g" ${file}
done
done
# Add copyright to AndroidManifest.xml and Android.bp
if [[ ! -z $(head -n 1 ${RRO_DIR}/AndroidManifest.xml | sed -n "/<manifest/p") ]]; then
sed -i "1 i\-->" ${RRO_DIR}/AndroidManifest.xml
sed -i "1 i\ SPDX-License-Identifier: Apache-2.0" ${RRO_DIR}/AndroidManifest.xml
sed -i "1 i\ Copyright (C) $(date +%Y) The LineageOS Project" ${RRO_DIR}/AndroidManifest.xml
sed -i "1 i\<!--" ${RRO_DIR}/AndroidManifest.xml
fi
if [[ ! -z $(head -n 1 ${RRO_DIR}/Android.bp | sed -n "/runtime_resource_overlay/p") ]]; then
sed -i '1i\\' ${RRO_DIR}/Android.bp
sed -i "1 i\\/\/" ${RRO_DIR}/Android.bp
sed -i "1 i\\/\/ SPDX-License-Identifier: Apache-2.0" ${RRO_DIR}/Android.bp
sed -i "1 i\\/\/" ${RRO_DIR}/Android.bp
sed -i "1 i\\/\/ Copyright (C) $(date +%Y) The LineageOS Project" ${RRO_DIR}/Android.bp
sed -i "1 i\\/\/" ${RRO_DIR}/Android.bp
fi
# Clear the temporary working directory
rm -rf "${TMPDIR}"