blob: b644dfea5784895b4f11e32b67d5860a0688c0f9 [file] [log] [blame]
Hector Dearman07ed69a2021-08-23 11:12:25 +01001#!/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 Dearmance831082022-06-23 15:10:04 +010015"""Generates TypeScript files that import all subdirectories and
16registers them with plugin registry. If you have three modules:
Hector Dearman07ed69a2021-08-23 11:12:25 +010017- core/
Hector Dearmance831082022-06-23 15:10:04 +010018- plugins/foo_plugin/
19- plugins/bar_plugin/
Hector Dearman07ed69a2021-08-23 11:12:25 +010020In general you would like the dependency to only go one way:
Hector Dearmance831082022-06-23 15:10:04 +010021- plugins/foo_plugin/ -> core/
22We want to avoid manually editing core/ for every plugin.
23
24This generates code like:
25
26import {pluginRegistry} from '../common/plugins';
27
28import {plugin as fooPlugin} from '../plugins/foo_plugin';
29import {plugin as barPlugin} from '../plugins/bar_plugin';
30
31pluginRegistry.register(fooPlugin);
32pluginRegistry.register(barPlugin);
Hector Dearman07ed69a2021-08-23 11:12:25 +010033"""
34
35from __future__ import print_function
36
37import os
38import argparse
39import subprocess
40import sys
41
42ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
43UI_SRC_DIR = os.path.join(ROOT_DIR, 'ui', 'src')
Hector Dearmance831082022-06-23 15:10:04 +010044PLUGINS_PATH = os.path.join(UI_SRC_DIR, 'common', 'plugins')
45
Hector Dearmanb1989b02022-07-05 20:11:07 +010046
Hector Dearmance831082022-06-23 15:10:04 +010047def to_camel_case(s):
48 first, *rest = s.split('_')
49 return first + ''.join(x.title() for x in rest)
Hector Dearman07ed69a2021-08-23 11:12:25 +010050
Hector Dearmanb1989b02022-07-05 20:11:07 +010051
Hector Dearman07ed69a2021-08-23 11:12:25 +010052def 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 Dearmance831082022-06-23 15:10:04 +010057 output_dir = os.path.dirname(output_path)
58 rel_plugins_path = os.path.relpath(PLUGINS_PATH, output_dir)
59
60 imports = []
Hector Dearmanb1989b02022-07-05 20:11:07 +010061 registrations = []
Hector Dearman07ed69a2021-08-23 11:12:25 +010062 for path in paths:
Hector Dearmance831082022-06-23 15:10:04 +010063 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 Dearman07ed69a2021-08-23 11:12:25 +010074
75 with open(output_path, 'w') as f:
76 f.write(expected)
77 return True
78
Hector Dearmanb1989b02022-07-05 20:11:07 +010079
Hector Dearman07ed69a2021-08-23 11:12:25 +010080def main():
Hector Dearmanb1989b02022-07-05 20:11:07 +010081 parser = argparse.ArgumentParser(
82 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
Hector Dearman07ed69a2021-08-23 11:12:25 +010083 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 Dearmanb1989b02022-07-05 20:11:07 +0100104
Hector Dearman07ed69a2021-08-23 11:12:25 +0100105if __name__ == '__main__':
106 exit(main())