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