1+ #! /bin/bash
2+
3+ # If generated samples directory doesn't exist yet, add it to the owlbot config
4+
5+ # Usage:
6+ # add_snippetdirectory_owlbot.sh <.OwlBot.yaml path from the root>
7+ # Example:
8+ # $ add_snippetdirectory_owlbot.sh java-dataform/.OwlBot.yaml
9+ #
10+ # To apply the change to all OwlBot configuration files in all modules:
11+ # $ for F in `find . -maxdepth 2 -name '.OwlBot.yaml'`; do sh generation/add_snippetdirectory_owlbot.sh $F; done
12+
13+ OWLBOT_FILE=$1
14+
15+ if [ -z " ${OWLBOT_FILE} " ]; then
16+ echo " Please specify file name"
17+ exit 1
18+ fi
19+
20+ if [ ! -r " ${OWLBOT_FILE} " ]; then
21+ echo " File not found"
22+ exit 1
23+ fi
24+
25+ dir_name=$( dirname " ${OWLBOT_FILE} " )
26+ module_name=$( basename " ${dir_name} " )
27+
28+ if [ ! -d " ${module_name} " ]; then
29+ echo " module ${module_name} does not exist"
30+ exit 1
31+ fi
32+
33+
34+
35+ # If snippets are already being copied, exit out
36+ if grep -q samples/snippets/generated ${OWLBOT_FILE} ; then
37+ echo " module ${module_name} already copies generated snippets"
38+ exit 1
39+ fi
40+
41+
42+ # Insert into `deep-remove-regex:` section
43+ deep_remove_regex=" - \" \/${module_name} \/samples\/snippets\/generated\" "
44+ entry_before_deep_remove_regex=" ${module_name} \/google-.*\/src"
45+ sed -i " /${entry_before_deep_remove_regex} /a ${deep_remove_regex} " ${OWLBOT_FILE}
46+
47+ # Insert into `deep-copy-regex:` section
48+ deep_copy_regex=" - source: \" /google/cloud/${dir_name} /(v.*)/.*-java/samples/snippets/generated\" \n dest: \" /owl-bot-staging/${module_name} /\$ 1/samples/snippets/generated\" "
49+
50+ entry_before_deep_copy_regex=" dest: \" \/owl-bot-staging\/${module_name} \/\$ 1\/google-"
51+
52+ sed -i " /${entry_before_deep_copy_regex} /a ${deep_copy_regex} " ${OWLBOT_FILE}
53+
54+ # Remove duplicate lines
55+ perl -i -ne ' if ( /^\s*#/ ) { print } else { print if ! $SEEN{$_}++}' ${OWLBOT_FILE}
56+
57+ # Add back new lines between sections
58+ sed -i ' s/deep-copy-regex/\n&/g' ${OWLBOT_FILE}
59+ sed -i ' s/deep-remove-regex/\n&/g' ${OWLBOT_FILE}
60+ sed -i ' s/deep-preserve-regex/\n&/g' ${OWLBOT_FILE}
61+ sed -i ' s/api-name/\n&/g' ${OWLBOT_FILE}
0 commit comments