1+ #
2+ # For all .proto files, strip the validation parameters & replace inline
3+ #
4+
5+ root=$( pwd)
6+
7+ # 1. hack: first add a couple newlines after all "];"
8+ find " ${root} " /service/protos/src/main/proto -name " *.proto" -type f -exec sh -c " awk '{gsub(/];/, \" ];\n\n\" ); print}' {} > tmp && mv tmp {}" \;
9+
10+ # 2. strip everything between square brackets [...] ignoring lines starting with //
11+ find " ${root} " /service/protos/src/main/proto -name " *.proto" -type f -exec sh -c " awk '!/^[[:space:]]*\/\// {gsub(/ \[.*\]/, \"\" );} {print}' {} > tmp && mv tmp {}" \;
12+
13+ find " ${root} " /service/protos/src/main/proto -name " *.proto" -type f | while read -r file; do
14+ awk '
15+ BEGIN { in_repeated = 0; buffer = "" }
16+ {
17+ if ($0 ~ /^[[:space:]]*(repeated|bytes|[A-Za-z0-9_]+).*=.*\[/) {
18+ in_repeated = 1
19+ buffer = $0
20+ } else if (in_repeated) {
21+ buffer = buffer " " $0
22+ }
23+
24+ if (in_repeated && $0 ~ /;/) {
25+ gsub(/\[.*\]/, "", buffer)
26+ print buffer
27+ in_repeated = 0
28+ buffer = ""
29+ } else if (!in_repeated && $0 !~ /^[[:space:]]*option[[:space:]]*\(validate\.required\)[[:space:]]*=[[:space:]]*true;/) {
30+ print $0
31+ }
32+ }
33+ ' " $file " > " ${file} .tmp" && mv " ${file} .tmp" " $file "
34+ done
35+
36+ # 3. add a newline after all trailing } brackets
37+ # find "${root}"/service/protos/src/main/proto -name "*.proto" -type f -exec sh -c "awk '{gsub(/}/, \"}\n\"); print}' {} > tmp && mv tmp {}" \;
38+
39+ # 4. strip validate import statement
40+ find " ${root} " /service/protos/src/main/proto -name " *.proto" -type f -exec sh -c " awk -v RS='' '{gsub(/import \" validate\/validate.proto\" ;/, \"\" ); print}' {} > tmp && mv tmp {}" \;
41+
42+ # 5. add a newline after all trailing } brackets
43+ # find "${root}"/service/protos/src/main/proto -name "*.proto" -type f -exec sh -c "awk '{gsub(/}/, \"}\n\"); print}' {} > tmp && mv tmp {}" \;
0 commit comments