#!/usr/bin/env python3 # Copyright (C) 2021 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Generates TypeScript files that import all subdirectories. This is useful for plugins/extensions. If you have two modules: - core/ - plugins/myplugin/ In general you would like the dependency to only go one way: - plugins/myplugin/ -> core/ But you still need some index file to import all plugins for the sake of bundling. This avoids having to manually edit core/ for every plugin you add. """ from __future__ import print_function import os import argparse import subprocess import sys ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) UI_SRC_DIR = os.path.join(ROOT_DIR, 'ui', 'src') def gen_imports(input_dir, output_path): paths = [os.path.join(input_dir, p) for p in os.listdir(input_dir)] paths = [p for p in paths if os.path.isdir(p)] paths.sort() lines = [] for path in paths: rel_path = os.path.relpath(path, os.path.dirname(output_path)) lines.append(f"import '{rel_path}';") expected = '\n'.join(lines) with open(output_path, 'w') as f: f.write(expected) return True def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('INPUT') parser.add_argument('--out', required=True) args = parser.parse_args() input_dir = args.INPUT output_path = args.out if not os.path.isdir(input_dir): print(f'INPUT argument {input_dir} must be a directory') exit(1) output_dir = os.path.dirname(output_path) if output_dir and not os.path.isdir(output_dir): print(f'--out ({output_path}) parent directory ({output_dir}) must exist') exit(1) if os.path.isdir(output_path): print(f'--out ({output_path}) should not be a directory') exit(1) success = gen_imports(input_dir, output_path) return 0 if success else 1 if __name__ == '__main__': exit(main())