summaryrefslogtreecommitdiffstats
path: root/pkg/default.java.do
blob: 16bf44072434cd77844c665f2f6f0d061d098d7f (plain)
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
#!/usr/bin/env bash
set -e
src=${1%.java}.aar
redo-ifchange "$src" "../ids.rc"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
7z e -tzip -o"$tmp" "$src" AndroidManifest.xml R.txt >/dev/null
if [[ -r "$tmp/AndroidManifest.xml" ]]; then
	package=$(xmllint --xpath 'string(/manifest/@package)' "$tmp/AndroidManifest.xml")
fi
if [[ ! $package ]] || [[ ! -r $tmp/R.txt ]]; then
	printf '//\n' >"$3"
	exit 0
fi
. ../ids.rc
. <(sed -n 's/^int[^ ]* \([a-z]*\) \([A-Za-z0-9_]*\) .*$/names_\1+=(\2)/p' "$tmp/R.txt")
mapfile -t types < <(sed -n 's/^int[^ ]* \([a-z]*\) .*/\1/p' "$tmp/R.txt" | sort -u)
exec >"$3"
printf '// generated by %s\n' "$0"
printf 'package %s;\npublic final class R {\n' "$package"
for typ in "${types[@]}"; do
	declare -n names="names_$typ"
	printf 'public static final class %s {\n' "$typ"
	for name in "${names[@]}"; do
		if [[ ${ids[$typ:$name]} ]]; then
			printf 'public static final %s;\n' "${ids[$typ:$name]}"
		fi
	done
	printf '}\n'
done
printf '}\n'
# vim:ft=bash