Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (C) 2021 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 15 | """Generates TypeScript files that import all subdirectories and |
| 16 | registers them with plugin registry. If you have three modules: |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 17 | - core/ |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 18 | - plugins/foo_plugin/ |
| 19 | - plugins/bar_plugin/ |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 20 | In general you would like the dependency to only go one way: |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 21 | - plugins/foo_plugin/ -> core/ |
| 22 | We want to avoid manually editing core/ for every plugin. |
| 23 | |
| 24 | This generates code like: |
| 25 | |
| 26 | import {pluginRegistry} from '../common/plugins'; |
| 27 | |
| 28 | import {plugin as fooPlugin} from '../plugins/foo_plugin'; |
| 29 | import {plugin as barPlugin} from '../plugins/bar_plugin'; |
| 30 | |
| 31 | pluginRegistry.register(fooPlugin); |
| 32 | pluginRegistry.register(barPlugin); |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 33 | """ |
| 34 | |
| 35 | from __future__ import print_function |
| 36 | |
| 37 | import os |
| 38 | import argparse |
| 39 | import subprocess |
| 40 | import sys |
| 41 | |
| 42 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 43 | UI_SRC_DIR = os.path.join(ROOT_DIR, 'ui', 'src') |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 44 | PLUGINS_PATH = os.path.join(UI_SRC_DIR, 'common', 'plugins') |
| 45 | |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 46 | |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 47 | def to_camel_case(s): |
| 48 | first, *rest = s.split('_') |
| 49 | return first + ''.join(x.title() for x in rest) |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 50 | |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 51 | |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 52 | def gen_imports(input_dir, output_path): |
| 53 | paths = [os.path.join(input_dir, p) for p in os.listdir(input_dir)] |
| 54 | paths = [p for p in paths if os.path.isdir(p)] |
| 55 | paths.sort() |
| 56 | |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 57 | output_dir = os.path.dirname(output_path) |
| 58 | rel_plugins_path = os.path.relpath(PLUGINS_PATH, output_dir) |
| 59 | |
| 60 | imports = [] |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 61 | registrations = [] |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 62 | for path in paths: |
Hector Dearman | ce83108 | 2022-06-23 15:10:04 +0100 | [diff] [blame] | 63 | rel_path = os.path.relpath(path, output_dir) |
| 64 | snake_name = os.path.basename(path) |
| 65 | camel_name = to_camel_case(snake_name) |
| 66 | imports.append(f"import {{plugin as {camel_name}}} from '{rel_path}';") |
| 67 | registrations.append(f"pluginRegistry.register({camel_name});") |
| 68 | |
| 69 | header = f"import {{pluginRegistry}} from '{rel_plugins_path}';" |
| 70 | import_text = '\n'.join(imports) |
| 71 | registration_text = '\n'.join(registrations) |
| 72 | |
| 73 | expected = f"{header}\n\n{import_text}\n\n{registration_text}" |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 74 | |
| 75 | with open(output_path, 'w') as f: |
| 76 | f.write(expected) |
| 77 | return True |
| 78 | |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 79 | |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 80 | def main(): |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 81 | parser = argparse.ArgumentParser( |
| 82 | description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 83 | parser.add_argument('INPUT') |
| 84 | parser.add_argument('--out', required=True) |
| 85 | args = parser.parse_args() |
| 86 | input_dir = args.INPUT |
| 87 | output_path = args.out |
| 88 | |
| 89 | if not os.path.isdir(input_dir): |
| 90 | print(f'INPUT argument {input_dir} must be a directory') |
| 91 | exit(1) |
| 92 | |
| 93 | output_dir = os.path.dirname(output_path) |
| 94 | if output_dir and not os.path.isdir(output_dir): |
| 95 | print(f'--out ({output_path}) parent directory ({output_dir}) must exist') |
| 96 | exit(1) |
| 97 | if os.path.isdir(output_path): |
| 98 | print(f'--out ({output_path}) should not be a directory') |
| 99 | exit(1) |
| 100 | |
| 101 | success = gen_imports(input_dir, output_path) |
| 102 | return 0 if success else 1 |
| 103 | |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame^] | 104 | |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 105 | if __name__ == '__main__': |
| 106 | exit(main()) |