• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3YELLOW="\033[1;33m"
4NOCOLOR="\033[0m"
5BLUE="\033[1;34m"
6RED="\033[1;91m"
7
8# TODO(optedoblivion): Check for 'git' and 'clang' binary
9
10function check_environment {
11    if [[ -z "${ANDROID_BUILD_TOP}" ]] || [[ -z "${ANDROID_HOST_OUT}" ]] ; then
12      echo -e "${RED}ANDROID_BUILD_TOP${NOCOLOR} or ${RED}ANDROID_HOST_OUT${NOCOLOR} is not set for host run"
13      echo -e "Navigate to android root and run:"
14      echo -e "${YELLOW}"
15      echo -e ". build/envsetup.sh"
16      echo -e "lunch <fish>"
17      echo -e "${NOCOLOR}"
18      echo
19      exit 1
20    fi
21}
22
23QUOTES=(
24    "It's a-me, Martino!"
25    "Hello!"
26    "Wahoo!"
27    "Oh yeah!"
28    "Martino time!"
29    "Lucky!"
30    "Hui hew! Just what I needed!"
31    "Spin! Hammer! Fire! Jump!"
32    "Yiiiiiipeee!"
33    "Yeah, ha ha ha!"
34    "Waha!"
35    "Let's-a go!"
36    "Here we go!"
37    "Yes! I'm the winner!"
38    "Luigi!"
39    "Way to go!"
40    "Here I go!"
41    "Mama Mia!"
42)
43
44VERBOSE=false
45
46# Controller
47CONTROLLER=false
48CONTROLLER_FILES=(
49    "system/gd/hci/controller.h"
50    "system/gd/hci/controller.cc"
51    "system/gd/hci/controller.cc"
52    "system/gd/hci/controller_mock.h"
53    "system/gd/hci/controller_test.cc"
54)
55CONTROLLER_FIND_PATTERNS=(
56    " LeRand(LeRandCallback cb);"
57    "Controller::impl::le_rand_cb<LeRandCompleteView>, cb));"
58    "impl::le_rand, cb);"
59    " (LeRandCallback cb));"
60    " le_rand_set.get_future().wait();"
61)
62CONTROLLER_CODE_TEMPLATES=(
63    "  virtual void :CamelApiName:();"
64    "  }\\\n\\\n  void :snake_api_name:() {\\\n    \\\\\/\\\\\/TODO(WHOAMI): Implement HCI Call"
65    "}\\\n\\\nvoid Controller:::CamelApiName:() {\\\n  CallOn(impl_.get(), \\\\\&impl:::snake_api_name:);"
66    "  MOCK_METHOD(void, :CamelApiName:, ());"
67    "}\\\n\\\nTEST_F(ControllerTest, :CamelApiName:Test) {\\\n  controller->:CamelApiName:();"
68)
69CONTROLLER_REPLACEMENT_PATTERNS=(
70    "FIRST\n\nSECOND"
71    "FIRST\nSECOND"
72    "FIRST\nSECOND"
73    "FIRST\nSECOND"
74    "FIRST\nSECOND"
75)
76
77# Controller shim
78CONTROLLER_SHIM=false
79CONTROLLER_SHIM_FILES=(
80    "system/device/include/controller.h"
81    "system/main/shim/controller.cc"
82    "system/main/shim/controller.cc"
83    "system/test/mock/mock_device_controller.cc"
84    "system/test/mock/mock_device_controller.cc"
85)
86CONTROLLER_SHIM_FIND_PATTERNS=(
87    " (\*le_rand)(LeRandCallback);"
88    "  bluetooth::shim::GetController()->LeRand(cb);"
89    " controller_le_rand,"
90    " le_rand(LeRandCallback cb) { return BTM_SUCCESS; }"
91    " le_rand,"
92)
93CONTROLLER_SHIM_CODE_TEMPLATES=(
94    "  uint8_t (*:snake_api_name:)(void);"
95    "  return BTM_SUCCESS;\\\n}\\\n\\\nstatic uint8_t controller_:snake_api_name:() {\\\n  bluetooth::shim::GetController()->:CamelApiName:();"
96    "    .:snake_api_name: = controller_:snake_api_name:"
97    "tBTM_STATUS :snake_api_name:() { return BTM_SUCCESS; }"
98    "    :snake_api_name:,"
99)
100CONTROLLER_SHIM_REPLACEMENT_PATTERNS=(
101    "FIRST\nSECOND"
102    "FIRST\nSECOND"
103    "FIRST\nSECOND"
104    "FIRST\nSECOND"
105    "FIRST\nSECOND"
106)
107
108## Files length must match templates and replacement pattern lengths!
109# BTM
110BTM_SHIM=false
111BTM_SHIM_FILES=(
112    "system/main/shim/btm_api.h"
113    "system/main/shim/btm_api.cc"
114    "system/test/mock/mock_main_shim_btm_api.cc"
115)
116BTM_SHIM_FIND_PATTERNS=(
117    "TM_STATUS BTM_LeRand(LeRandCallback);"
118    "ontroller_get_interface()->le_rand(cb);"
119    " bluetooth::shim::BTM_LeRand(LeRandCallback cb) {"
120)
121BTM_SHIM_CODE_TEMPLATES=(
122    "\\\\\/*******************************************************************************\\\n *\\\n * Function        BTM_:CamelApiName:\\\n *\\\n * Description    :API_DESCRIPTION:\\\n *\\\n * Parameters\\\n *\\\n *******************************************************************************\\\\\/\\\ntBTM_STATUS BTM_:CamelApiName:(void);"
123    "  return BTM_SUCCESS;\\\n}\\\n\\\ntBTM_STATUS bluetooth::shim::BTM_:CamelApiName:() {\\\n  \\\\\/\\\\\/PLUMB: controller_get_interface()->:snake_api_name:();"
124    "  inc_func_call_count(__func__);\\\n  return BTM_SUCCESS;\\\n}\\\n\\\ntBTM_STATUS bluetooth::shim::BTM_:CamelApiName:() {"
125)
126BTM_SHIM_REPLACEMENT_PATTERNS=(
127    "FIRST\n\nSECOND"
128    "FIRST\nSECOND"
129    "FIRST\nSECOND"
130)
131
132# BTA
133BTA=false
134BTA_FILES=(
135    # External BTA API
136    "system/bta/include/bta_api.h"
137    "system/bta/dm/bta_dm_api.cc"
138    # internal BTA API
139    "system/bta/dm/bta_dm_int.h"
140    "system/bta/dm/bta_dm_act.cc"
141)
142BTA_FIND_PATTERNS=(
143    "extern void BTA_DmLeRand(LeRandCallback cb);"
144    "do_in_main_thread(FROM_HERE, base::Bind(bta_dm_le_rand, cb));"
145    "extern void bta_dm_le_rand(LeRandCallback cb);"
146    "ooth::shim::BTM_LeRand(cb);"
147)
148BTA_CODE_TEMPLATES=(
149    "\\\\\/*******************************************************************************\\\n *\\\n * Function        BTA_Dm:CamelApiName:\\\n *\\\n * Description    :API_DESCRIPTION:\\\n *\\\n * Parameters\\\n *\\\n *******************************************************************************\\\\\/\\\nextern void BTA_Dm:CamelApiName:();"
150    "}\\\n\\\nvoid BTA_Dm:CamelApiName:() {\\\n  APPL_TRACE_API(\"BTA_Dm:CamelApiName:\");\\\n  do_in_main_thread(FROM_HERE, base::Bind(bta_dm_:snake_api_name:));"
151    "extern void bta_dm_:snake_api_name:();"
152    "}\\\n\\\n\\\\\/*******************************************************************************\\\n *\\\n * Function        BTA_Dm:CamelApiName:\\\n *\\\n * Description    :API_DESCRIPTION:\\\n *\\\n * Parameters\\\n *\\\n *******************************************************************************\\\\\/\\\nvoid bta_dm_:snake_api_name:() {\\\n  \\\\\/\\\\\/PLUMB: bluetooth::shim::BTM_:CamelApiName:();"
153)
154BTA_REPLACEMENT_PATTERNS=(
155    "FIRST\n\nSECOND"
156    "FIRST\nSECOND"
157    "FIRST\nSECOND"
158    "FIRST\nSECOND"
159)
160
161# BTIF
162BTIF=false
163BTIF_FILES=(
164    # BTIF DM Layer
165    "system/btif/include/btif_dm.h"
166    "system/btif/src/btif_dm.cc"
167    # BTIF Layer
168    "system/include/hardware/bluetooth.h"
169    "system/btif/src/bluetooth.cc"
170    "system/btif/src/bluetooth.cc"
171    "system/service/hal/fake_bluetooth_interface.cc"
172    # Yes double it for two replacements
173    "system/test/mock/mock_bluetooth_interface.cc"
174    "system/test/mock/mock_bluetooth_interface.cc"
175)
176BTIF_FIND_PATTERNS=(
177    # BTIF DM Layer
178    "oid btif_dm_le_rand(LeRandCallback callback);"
179    "_dm_le_rand(callback);"
180    # BTIF Layer
181    "} bt_interface_t;"
182    " void dump("
183    " le_rand,"
184    " le_rand "
185    "EXPORT_SYMBOL"
186    " le_rand,"
187)
188BTIF_CODE_TEMPLATES=(
189    # BTIF DM Layer
190    " void btif_dm_:snake_api_name:();"
191    "}\\\n\\\nvoid btif_dm_:snake_api_name:() {\\\n  \\\\\/\\\\\/PLUMB: BTA_Dm:CamelApiName:();"
192    # BTIF Layer
193    " \\\n\\\\\/**\\\n *\\\n * :API_DESCRIPTION:\\\n *\\\n *\\\\\/\\\nint (*:snake_api_name:)();"
194    " int :snake_api_name:() {\\\n  if(!interface_ready()) return BT_STATUS_NOT_READY;\\\n  do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_:snake_api_name:));\\\n  return BT_STATUS_SUCCESS;\\\n}\\\n\\\nstatic"
195    "\ \ \ \ :snake_api_name:,"
196    "*\\\\\/\\\n\ \ \ \ nullptr, \\\\\/* :snake_api_name: "
197    "static int :snake_api_name:() { return 0; }\\\n\\\nE"
198    "\ \ \ \ :snake_api_name:,"
199)
200BTIF_REPLACEMENT_PATTERNS=(
201    # BTIF DM Layer
202    "FIRST\nSECOND"
203    "FIRST\nSECOND"
204    # BTIF Layer
205    "SECOND\nFIRST"
206    "SECONDFIRST"
207    "FIRST\nSECOND"
208    "FIRSTSECOND"
209    "SECONDFIRST"
210    "FIRST\nSECOND"
211)
212
213# Topshim
214TOPSHIM=false
215TOPSHIM_FILES=(
216    # Topshim API
217    "system/gd/rust/topshim/src/btif.rs"
218    "system/gd/rust/topshim/facade/src/adapter_service.rs"
219    # Topshim Test API
220    "system/blueberry/facade/topshim/facade.proto"
221    "system/blueberry/tests/gd/rust/topshim/facade/automation_helper.py"
222)
223TOPSHIM_FIND_PATTERNS=(
224    # Topshim API
225    " le_rand)"
226    ".le_rand();"
227    # Topshim Test API
228    " LeRand(google.protobuf.Empty) returns (google.protobuf.Empty) {}"
229    " self.adapter_stub.LeRand(empty_proto.Empty())"
230)
231TOPSHIM_CODE_TEMPLATES=(
232    # Topshim API
233    "    }\\\n\\\n    pub fn :snake_api_name:(\\\\\&self) -> i32 {\\\n        ccall!(self, :snake_api_name:)"
234    "        ctx.spawn(async move {\\\n            sink.success(Empty::default()).await.unwrap();\\\n        })\\\n    }\\\n\\\n    fn :snake_api_name:(\\\\\&mut self, ctx: RpcContext<'_>, _req: Empty, sink: UnarySink<Empty>) {\\\n        self.btif_intf.lock().unwrap().:snake_api_name:();"
235    # Topshim Test API
236    "  rpc :CamelApiName:(google.protobuf.Empty) returns (google.protobuf.Empty) {}"
237    "    async def :snake_api_name:(self):\\\n        await self.adapter_stub.:CamelApiName:(empty_proto.Empty())"
238)
239TOPSHIM_REPLACEMENT_PATTERNS=(
240    "FIRST\nSECOND"
241    "FIRST\nSECOND"
242    "FIRST\nSECOND"
243    "FIRST\n\nSECOND"
244)
245
246function help_menu {
247    echo
248    echo -e "${YELLOW}Help menu${NOCOLOR}"
249    echo -e "==================================="
250    echo -e "${BLUE}  --controller${NOCOLOR}"
251    echo -e "    Adds plumbing for the GD Controller Layer for the API."
252    echo -e "    This includes test file changes required to build."
253    echo -e "${BLUE}  --controller-shim${NOCOLOR}"
254    echo -e "    Adds plumbing for the GD Controller Shim Layer for the API."
255    echo -e "    This includes test file changes required to build."
256    echo -e "    Will autoplumb to ONLY the GD controller if --controller flag is set. (as opposed to legacy controller btu_hcif)"
257    echo -e "${BLUE}  --btm${NOCOLOR}"
258    echo -e "    Adds plumbing for the BTM Shim Layer for the given API."
259    echo -e "    Will autoplumb to ONLY the controller shim if --controller-shim flag is set. vs directly to legacy btu_hcif"
260    echo -e "${BLUE}  --bta${NOCOLOR}"
261    echo -e "    Adds plumbing for the BTA Layer for the given API."
262    echo -e "    Will autoplumb to BTM if --btm set."
263    echo -e "${BLUE}  --btif${NOCOLOR}"
264    echo -e "    Adds plumbing for the BTIF Layer for the API."
265    echo -e "    This currently includes JNI as it is a requirement for Android to build."
266    echo -e "    Will autoplumb to BTA if --bta set."
267    echo -e "${BLUE}  --topshim${NOCOLOR}"
268    echo -e "    Adds plumbing for the topshim to BTIF Layer for the API."
269    echo -e "    This will also include testing APIs callable from python tests."
270    echo -e "    Will autoplumb to BTIF if --btif set."
271    echo -e "${BLUE}  --verbose${NOCOLOR}"
272    echo -e "    Prints verbose logging."
273    echo
274    echo -e "Usage: $0 [--controller|--controller-shim|--btm|--bta|--btif|--topshim] [CamelCaseApiName] [snake_case_api_name] (description)"
275    echo -e "        ${YELLOW}e.g."
276    echo -e "         $0 --controller --btm ClearEventMask clear_event_mask \"Clear out the event mask\""
277    echo -e "         $0 --controller --btm --bta --btif --topshim ClearEventMaskclear_event_mask \"Clear out the event mask\" ${NOCOLOR}"
278    echo
279}
280
281## Start parsing arguments here
282POSITIONAL=()
283function parse_options {
284    while [[ $# -gt 0 ]]
285    do
286    key="$1"
287    case $key in
288        --verbose)
289            VERBOSE=true
290            shift
291            ;;
292        --help)
293            help_menu
294            shift
295            exit 0
296            ;;
297        --controller)
298            CONTROLLER=true
299            shift
300            ;;
301        --controller-shim)
302            CONTROLLER_SHIM=true
303            shift
304            ;;
305        --btm)
306            # Actually we skip BTM here and just use the BTM Shim
307            BTM_SHIM=true
308            shift
309            ;;
310        --bta)
311            BTA=true
312            shift
313            ;;
314        --btif)
315            BTIF=true
316            shift
317            ;;
318        --topshim)
319            TOPSHIM=true
320            shift
321            ;;
322        --*)
323            echo "$0: unrecognized argument: '$1'"
324            echo "Try '$0 --help' for more information"
325            exit 1
326            shift
327            ;;
328        *)
329            POSITIONAL+=("${1}")
330            shift
331            ;;
332    esac
333    done
334    set -- "${POSITIONAL[@]}"
335}
336
337function show_mario {
338    MSG="$(mario_message)I'm plumbing the '${1}'"
339    echo
340    echo -e "${RED} ⠀⠀⠀      ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣠⣤⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  "341    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⡾⣻⣿⣿⣿⣿⣯⣍⠛⠻⢷⣦⣀⠀⠀⠀⠀⠀⠀⠀  "
342    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⠟⢁⣾⠟⠋⣁⣀⣤⡉⠻⣷⡀⠀⠙⢿⣷⣄⠀⠀⠀⠀⠀  "343    echo -e "${NOCOLOR}⠀⠀⠀⠀⠀⠀⠀⢀⡀${RED}⠀⠀⠀⠀⠀⠀⣰⣿⠏⠀⠀⢸⣿⠀⠼⢋⣉⣈⡳⢀⣿⠃⠀⠀⠀⠙⣿⣦⡀⠀⠀⠀  "344    echo -e "${NOCOLOR}⠀⠀⠀⠀⠀⠀⢰⡿⠿⣷⡀⠀${RED}⠀⠀⣼⣿⠃⠀⠀⣀⣤⡿⠟⠛⠋⠉⠉⠙⢛⣻⠶⣦⣄⡀⠀⠘⣿⣷⡀⠀⠀  "345    echo -e "${NOCOLOR}⢠⣾⠟⠳⣦⣄⢸⡇⠀⠈⣷⡀${RED}⠀⣼⣿⡏⢀⣤⡾${NOCOLOR}⢋⣵⠿⠻⢿⠋⠉⠉⢻⠟⠛⠻⣦⣝${RED}⠻⣷⣄⠸⣿⣿${NOCOLOR}⠀⠀     ( ${MSG} )"346    echo -e "⠘⣧⠀⠀⠀⠙⢿⣿⠀⠀⢸⣷${RED}⠀⣿⣿⣧⣾⣏${NOCOLOR}⡴⠛⢡⠖⢛⣲⣅⠀⠀⣴⣋⡉⠳⡄⠈⠳${RED}⢬⣿⣿⣿⡿${NOCOLOR}⠀⠀    O"347    echo -e "⠀⠘⠷⣤⣀⣀⣀⣽⡶⠛⠛⠛⢷⣿⣿⣿⣿⣏⠀⠀⡏⢰⡿⢿⣿⠀⠀⣿⠻⣿⠀⡷⠀⣠⣾⣿⡿⠛⠷⣦⠀   o"348    echo -e "⠀⠀⢀⣾⠟⠉⠙⣿⣤⣄⠀⢀⣾⠉⠀⢹⣿⣿⣷⠀⠹⡘⣷⠾⠛⠋⠉⠛⠻⢿⡴⢃⣄⣻⣿⣿⣷⠀⠀⢹⡇  ."349    echo -e "⠀⠀⢸⡇⠈⠉⠛⢦⣿⡏⠀⢸⣧⠀⠈⠻⣿⡿⢣⣾⣦⣽⠃⠀⠀⠀⠀⠀⠀⠀⣷⣾⣿⡇⠉⢿⡇⠀⢀⣼⠇  "350    echo -e "⠀⠀⠘⣷⡠⣄⣀⣼⠇⠀⠀⠀⠻⣷⣤⣀⣸⡇⠀⠹⣿⣿⣦⣀⠀⠀⠀⠀⢀⣴⣿⣿⡟⠀⠀⢸⣷⣾⡿⠃⠀  "351    echo -e "⠀⠀⠀⠈⠻⢦⣍⣀⣀⣀⡄⠀⣰⣿⡿⠿⢿⣇⠀⠀⠉⠛⠻⣿⣿⡷⠾⣿⣿⡿⠉⠁⠀⠀⢀⣾⠋⠁⠀⠀⠀  "
352    echo -e "⠀⠀⠀⠀⠀⠀⠈⠉⠉⠙⠿⢿⣿⣇⠀⠀⠈⢿⣧⣄⠀⠀⠀⢹⣷⣶⣶⣾⣿⡇⠀⠀⣀⣴⡿⣧⣄⡀⠀⠀⠀  "353    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣷⡀⠀⠀⠙⢿⣿⣶⣤⡀⠻⢤⣀⡤⠞⢀⣴⣿⣿⠟⢷⡀⠙⠻⣦⣄⠀  "354    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⣦⠀⢠⡟⠁⠙⢻⣿⠷⠶⣶⠶⠾⠛⠙⣿⠇⠀⠀⢻⡄⠀⠀⠙⢷⡀ "
355    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⡀⣿⠁⣤⣤⡄⢻⡶⠶⠛⠛⠛⠛⠛⣿⢠⣾⣷⣆⢻⡀⠀⠀⠈⣷ "
356    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⢸⣿⣿⣿⡈⢿⡀⠀⠀⠀⠀⠀⡿⢸⣿⣿⣿⢸⡇⠀⠀⠀⡟ "
357    echo -e "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠉⠈⠉⠉⠉⠁⠈⠁⠀⠀⠀⠀⠈⠁⠈⠉⠉⠉⠀⠁⠀⠀⠈⠁ "
358    echo
359}
360
361function mario_done {
362    echo -e "${YELLOW}Done.${NOCOLOR}"
363}
364
365function mario_message {
366    I=$((0 + $RANDOM % ${#QUOTES[@]}))
367    echo -en "${BLUE}${QUOTES[$I]}! ${BLUE}${2}${YELLOW}${1} ${NOCOLOR}"
368}
369
370# TODO: Pass in which patterns and templates to use
371function plumbit {
372    layer="$1"
373    shift
374    camel_api_name="$1"
375    shift
376    snake_api_name="$1"
377    shift
378    api_description="$1"
379    shift
380    files_array=($@)
381
382    WORKING_CODE_TEMPLATES=()
383    WORKING_PATTERNS=()
384    AUTOPLUMB=false
385    if [ "$layer" == "controller" ]; then
386        WORKING_PATTERNS=("${CONTROLLER_FIND_PATTERNS[@]}")
387        WORKING_CODE_TEMPLATES=("${CONTROLLER_CODE_TEMPLATES[@]}")
388        WORKING_REPLACEMENTS=("${CONTROLLER_REPLACEMENT_PATTERNS[@]}")
389    elif [ "$layer" == "controller_shim" ]; then
390        WORKING_PATTERNS=("${CONTROLLER_SHIM_FIND_PATTERNS[@]}")
391        WORKING_CODE_TEMPLATES=("${CONTROLLER_SHIM_CODE_TEMPLATES[@]}")
392        WORKING_REPLACEMENTS=("${CONTROLLER_SHIM_REPLACEMENT_PATTERNS[@]}")
393        AUTOPLUMB=$CONTROLLER
394    elif [ "$layer" == "btm_shim" ]; then
395        WORKING_PATTERNS=("${BTM_SHIM_FIND_PATTERNS[@]}")
396        WORKING_CODE_TEMPLATES=("${BTM_SHIM_CODE_TEMPLATES[@]}")
397        WORKING_REPLACEMENTS=("${BTM_SHIM_REPLACEMENT_PATTERNS[@]}")
398        AUTOPLUMB=$CONTROLLER_SHIM
399    elif [ "$layer" == "bta" ]; then
400        WORKING_PATTERNS=("${BTA_FIND_PATTERNS[@]}")
401        WORKING_CODE_TEMPLATES=("${BTA_CODE_TEMPLATES[@]}")
402        WORKING_REPLACEMENTS=("${BTA_REPLACEMENT_PATTERNS[@]}")
403        AUTOPLUMB=$BTM_SHIM
404    elif [ "$layer" == "btif" ]; then
405        WORKING_PATTERNS=("${BTIF_FIND_PATTERNS[@]}")
406        WORKING_CODE_TEMPLATES=("${BTIF_CODE_TEMPLATES[@]}")
407        WORKING_REPLACEMENTS=("${BTIF_REPLACEMENT_PATTERNS[@]}")
408        AUTOPLUMB=$BTA
409    elif [ "$layer" == "topshim" ]; then
410        WORKING_PATTERNS=("${TOPSHIM_FIND_PATTERNS[@]}")
411        WORKING_CODE_TEMPLATES=("${TOPSHIM_CODE_TEMPLATES[@]}")
412        WORKING_REPLACEMENTS=("${TOPSHIM_REPLACEMENT_PATTERNS[@]}")
413        AUTOPLUMB=$BTIF
414    fi
415
416    for index in ${!files_array[@]}; do
417        CODE=$(echo "${WORKING_CODE_TEMPLATES[$index]}" | sed "s/:CamelApiName:/$camel_api_name/g" | sed "s/:snake_api_name:/$snake_api_name/g" | sed "s/WHOAMI/$(whoami)/g" | sed "s/:API_DESCRIPTION:/${api_description}/g")
418        if [ "$AUTOPLUMB" == true ]; then
419            CODE=$(echo "${CODE}" | sed "s/PLUMB:/ Autoplumbed\\\\\\\n /g")
420        fi
421        PATTERN="${WORKING_PATTERNS[$index]}"
422        REPLACEMENT=$(echo ${WORKING_REPLACEMENTS[$index]} | sed s/FIRST/"\\${PATTERN}"/g | sed s/SECOND/"${CODE}"/g)
423        if [ "$VERBOSE" == true ]; then
424            echo sed -i "s/\\${PATTERN}/\\${REPLACEMENT}/g" "${files_array[$index]}"
425        fi
426        sed -i "s/\\${PATTERN}/\\${REPLACEMENT}/g" "${files_array[$index]}"
427    done
428}
429
430CL_COUNT=0
431
432function commitit {
433    mario_message "${1}" "Committing the code..."
434    git commit -qam "${2} ${1} API"
435    mario_done
436    let CL_COUNT=$CL_COUNT+1
437}
438
439function clangit {
440    FORMATTER="${ANDROID_BUILD_TOP}/tools/repohooks/tools/clang-format.py"
441    FIX="--fix"
442    CLANG_FORMAT="--clang-format ${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-stable/bin/clang-format"
443    GIT_CLANG_FORMAT="--git-clang-format ${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-stable/bin/git-clang-format"
444    COMMIT="--commit"
445    STYLE="--style file"
446    EXTENSIONS="--extensions c,h,cc,cpp,hpp"
447    HASH="$1"
448    CMD="${FORMATTER} ${FIX} ${CLANG_FORMAT} ${GIT_CLANG_FORMAT} ${COMMIT} ${HASH} ${STYLE} ${EXTENSIONS}"
449    $(${CMD})
450}
451
452function rustfmtit {
453    echo "rusty rust"
454#    FORMATTER="${ANDROID_BUILD_TOP}/prebuilts/rust/linux-x86/stable/rustfmt"
455#    CONFIG="'--config-path=rustfmt.toml'"
456#    FILE=""
457#    CMD="${FORMATTER} ${CONFIG} ${FILE}"
458#    $(${CMD})
459}
460
461
462function formatit {
463    mario_message "${1}" "Formatting the code..."
464    hash="$(git log -n 1 --pretty=oneline | awk '{ print $1 }')"
465    clangit $hash
466    rustfmtit $hash
467    git commit -a --amend --no-edit
468    mario_done
469}
470
471function controller {
472    if [ "$CONTROLLER" == false ]; then
473        return
474    fi
475    mario_message "Controller" "Plumbing the '${1}' API..."
476    plumbit "controller" "${1}" "${2}" "${3}" "${CONTROLLER_FILES[@]}"
477    mario_done
478    commitit "Controller" "${3}"
479    formatit "Controller"
480}
481
482function controller_shim {
483    if [ "$CONTROLLER_SHIM" == false ]; then
484        return
485    fi
486    mario_message "Controller shim" "Plumbing the '${1}' API..."
487    plumbit "controller_shim" "${1}" "${2}" "${3}" "${CONTROLLER_SHIM_FILES[@]}"
488    mario_done
489    commitit "Controller shim" "${3}"
490    formatit "Controller shim"
491}
492
493function btm_shim {
494    if [ "$BTM_SHIM" == false ]; then
495        return
496    fi
497    mario_message "BTM" "Plumbing the '${1}' API..."
498    plumbit "btm_shim" "${1}" "${2}" "${3}" "${BTM_SHIM_FILES[@]}"
499    mario_done
500    commitit "BTM" "${3}"
501    formatit "BTM"
502}
503
504function bta {
505    if [ "$BTA" == false ]; then
506        return
507    fi
508    mario_message "BTA" "Plumbing the '${1}' API..."
509    plumbit "bta" "${1}" "${2}" "${3}" "${BTA_FILES[@]}"
510    mario_done
511    commitit "BTA" "${3}"
512    formatit "BTA"
513}
514
515function btif {
516    if [ "$BTIF" == false ]; then
517        return
518    fi
519    mario_message "BTIF" "Plumbing the '${1}' API..."
520    plumbit "btif" "${1}" "${2}" "${3}" "${BTIF_FILES[@]}"
521    mario_done
522    commitit "BTIF" "${3}"
523    formatit "BTIF"
524}
525
526function topshim {
527    if [ "$TOPSHIM" == false ]; then
528        return
529    fi
530    mario_message "Topshim" "Plumbing the '${1}' API..."
531    plumbit "topshim" "${1}" "${2}" "${3}" "${TOPSHIM_FILES[@]}"
532    mario_done
533    commitit "Topshim" "${3}"
534    formatit "Topshim"
535}
536
537function main {
538    check_environment
539    parse_options $@
540    if [ "${#POSITIONAL[@]}" -lt 3 ]; then
541        echo -e "${RED}Error: Invalid argument count for API Names!${NOCOLOR}"
542        help_menu
543        exit 1
544    fi
545    camel_api_name="${POSITIONAL[0]}"
546    snake_api_name="${POSITIONAL[1]}"
547    api_description="${POSITIONAL[@]:2}"
548    show_mario "${camel_api_name} API that ${api_description}!"
549    controller "${camel_api_name}" "${snake_api_name}" "${api_description}"
550    controller_shim "${camel_api_name}" "${snake_api_name}" "${api_description}"
551    btm_shim "${camel_api_name}" "${snake_api_name}" "${api_description}"
552    bta "${camel_api_name}" "${snake_api_name}" "${api_description}"
553    btif "${camel_api_name}" "${snake_api_name}" "${api_description}"
554    topshim "${camel_api_name}" "${snake_api_name}" "${api_description}"
555    git rebase -i HEAD~${CL_COUNT} -x 'git commit --amend'
556}
557
558main $@
559#/usr/local/google/home/optedoblivion/workspace/AOSP/prebuilts/rust/linux-x86/stable/rustfmt '--config-path=rustfmt.toml' system/gd/rust/topshim/facade/src/adapter_service.rs
560