xref: /aosp_15_r20/external/google-cloud-java/generation/set_owlbot_config.sh (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1#!/bin/bash
2
3# Adds module directory name into the paths in a OwlBot configuration file so
4# that the paths correctly reference the files under the modules in this monorepo.
5#
6# Usage:
7# set_owlbot_config.sh <.OwlBot.yaml path from the root>
8# Example:
9# $ set_owlbot_config.sh java-dataform/.OwlBot.yaml
10#
11# To apply the change to all OwlBot configuration files in all modules:
12# $ for F in `find . -maxdepth 2 -name '.OwlBot.yaml'`; do sh generation/set_owlbot_config.sh $F; done
13
14for F in `find . -maxdepth 2 -name '.OwlBot.yaml'`;
15do
16
17OWLBOT_FILE=$F
18
19if [ -z "${OWLBOT_FILE}" ]; then
20  echo "Please specify file name"
21  exit 1
22fi
23
24if [ ! -r "${OWLBOT_FILE}" ]; then
25  echo "File not found"
26  exit 1
27fi
28
29dir_name=$(dirname "${OWLBOT_FILE}")
30module_name=$(basename "${dir_name}")
31
32if [ ! -d "${module_name}" ]; then
33  echo "module ${module_name} does not exist"
34  exit 1
35fi
36
37# For deep-remove-regex and deep-preserve-regex fields
38sed -i.bak "s|\"/grpc-google|\"/${module_name}/grpc-google|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
39sed -i.bak "s|\"/proto-google|\"/${module_name}/proto-google|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
40sed -i.bak "s|\"/google-\.\*|\"/${module_name}/google-.*|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
41sed -i.bak "s|\"/google-cloud|\"/${module_name}/google-cloud|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
42sed -i.bak "s|\"/samples|\"/${module_name}/samples|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
43
44# In monorepo, the staging directory structure tells the destination module to
45# which the OwlBot Java postprocessor copies the files.
46if grep --quiet 'owl-bot-staging/$1' "${OWLBOT_FILE}"; then
47  sed -i.bak "s|owl-bot-staging|owl-bot-staging/${module_name}|" "${OWLBOT_FILE}" && rm "${OWLBOT_FILE}".bak
48fi
49
50# This section is specifically around the generated snippet directories
51# If snippets are already being copied, skip
52if ! grep -q samples/snippets/generated ${OWLBOT_FILE}; then
53# Insert into `deep-remove-regex:` section
54deep_remove_regex="- \"\/${module_name}\/samples\/snippets\/generated\""
55entry_before_deep_remove_regex="${module_name}\/google-.*\/src"
56sed -i.bak "/${entry_before_deep_remove_regex}/a ${deep_remove_regex}" ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
57
58
59# Insert into `deep-copy-regex:` section
60proto_path=$(grep -oPm1 '(?<=source: ").*(?=\(v.*\))' "${OWLBOT_FILE}")
61deep_copy_regex="- source: \"${proto_path}(v.*)/.*-java/samples/snippets/generated\"\n  dest: \"/owl-bot-staging/${module_name}/\$1/samples/snippets/generated\""
62
63entry_before_deep_copy_regex="dest: \"\/owl-bot-staging\/${module_name}\/\$1\/google-"
64
65# echo ${proto_path}
66sed -i.bak "/${entry_before_deep_copy_regex}/a ${deep_copy_regex}" ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
67
68# Remove duplicate lines
69perl -i -ne 'if ( /^\s*#/ ) { print } else { print if ! $SEEN{$_}++}' ${OWLBOT_FILE}
70
71# Add back new lines between sections
72sed -i.bak  's/deep-copy-regex/\n&/g'  ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
73sed -i.bak  's/deep-remove-regex/\n&/g'  ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
74sed -i.bak  's/deep-preserve-regex/\n&/g'  ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
75sed -i.bak  's/api-name/\n&/g'  ${OWLBOT_FILE} && rm "${OWLBOT_FILE}".bak
76
77fi
78
79done
80