blob: 1f10a38ff70569b03b81c87db67ab46c19947510 [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.
15
16"""Generates TypeScript files that import all subdirectories.
17This is useful for plugins/extensions. If you have two modules:
18- core/
19- plugins/myplugin/
20In general you would like the dependency to only go one way:
21- plugins/myplugin/ -> core/
22But you still need some index file to import all plugins for the sake of
23bundling. This avoids having to manually edit core/ for every
24plugin you add.
25"""
26
27from __future__ import print_function
28
29import os
30import argparse
31import subprocess
32import sys
33
34ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
35UI_SRC_DIR = os.path.join(ROOT_DIR, 'ui', 'src')
36
37def gen_imports(input_dir, output_path):
38 paths = [os.path.join(input_dir, p) for p in os.listdir(input_dir)]
39 paths = [p for p in paths if os.path.isdir(p)]
40 paths.sort()
41
42 lines = []
43 for path in paths:
44 rel_path = os.path.relpath(path, os.path.dirname(output_path))
45 lines.append(f"import '{rel_path}';")
46 expected = '\n'.join(lines)
47
48 with open(output_path, 'w') as f:
49 f.write(expected)
50 return True
51
52def main():
53 parser = argparse.ArgumentParser(description=__doc__,
54 formatter_class=argparse.RawDescriptionHelpFormatter)
55 parser.add_argument('INPUT')
56 parser.add_argument('--out', required=True)
57 args = parser.parse_args()
58 input_dir = args.INPUT
59 output_path = args.out
60
61 if not os.path.isdir(input_dir):
62 print(f'INPUT argument {input_dir} must be a directory')
63 exit(1)
64
65 output_dir = os.path.dirname(output_path)
66 if output_dir and not os.path.isdir(output_dir):
67 print(f'--out ({output_path}) parent directory ({output_dir}) must exist')
68 exit(1)
69 if os.path.isdir(output_path):
70 print(f'--out ({output_path}) should not be a directory')
71 exit(1)
72
73 success = gen_imports(input_dir, output_path)
74 return 0 if success else 1
75
76if __name__ == '__main__':
77 exit(main())