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. 15"""Generates TypeScript files that import all subdirectories and 16registers them with plugin registry. If you have three modules: 17- core/ 18- plugins/foo_plugin/ 19- plugins/bar_plugin/ 20In general you would like the dependency to only go one way: 21- 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); 33""" 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') 44PLUGINS_PATH = os.path.join(UI_SRC_DIR, 'common', 'plugins') 45 46 47def to_camel_case(s): 48 first, *rest = s.split('_') 49 return first + ''.join(x.title() for x in rest) 50 51 52def 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 57 output_dir = os.path.dirname(output_path) 58 rel_plugins_path = os.path.relpath(PLUGINS_PATH, output_dir) 59 60 imports = [] 61 registrations = [] 62 for path in paths: 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}" 74 75 with open(output_path, 'w') as f: 76 f.write(expected) 77 return True 78 79 80def main(): 81 parser = argparse.ArgumentParser( 82 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 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 104 105if __name__ == '__main__': 106 exit(main()) 107