|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def fatal(msg): |
| 12 | + print(msg, file=sys.stderr) |
| 13 | + sys.exit(1) |
| 14 | + |
| 15 | + |
| 16 | +def find_swift_files(path): |
| 17 | + for parent, dirs, files in os.walk(path, topdown=True): |
| 18 | + for filename in files: |
| 19 | + if not filename.endswith('.swift'): |
| 20 | + continue |
| 21 | + yield filename |
| 22 | + |
| 23 | + |
| 24 | +def main(arguments): |
| 25 | + parser = argparse.ArgumentParser( |
| 26 | + description='Generate an output file map for the given directory') |
| 27 | + parser.add_argument('-o', dest='output_dir', |
| 28 | + help='Directory to which the file map will be emitted') |
| 29 | + parser.add_argument('input_dir', help='a directory of swift files') |
| 30 | + args = parser.parse_args(arguments) |
| 31 | + |
| 32 | + if not args.output_dir: |
| 33 | + fatal("output directory is required") |
| 34 | + |
| 35 | + # Create the output directory if it doesn't already exist. |
| 36 | + if not os.path.isdir(args.output_dir): |
| 37 | + os.makedirs(args.output_dir) |
| 38 | + |
| 39 | + output_path = os.path.join(args.output_dir, 'output.json') |
| 40 | + |
| 41 | + if not os.path.isdir(args.input_dir): |
| 42 | + fatal("input directory does not exist, or is not a directory") |
| 43 | + |
| 44 | + swift_files = find_swift_files(args.input_dir) |
| 45 | + if not swift_files: |
| 46 | + fatal("no swift files in the given input directory") |
| 47 | + |
| 48 | + all_records = {} |
| 49 | + for swift_file in swift_files: |
| 50 | + file_name = os.path.splitext(swift_file)[0] |
| 51 | + all_records['./' + swift_file] = { |
| 52 | + 'object': './' + file_name + '.o', |
| 53 | + 'swift-dependencies': './' + file_name + '.swiftdeps', |
| 54 | + } |
| 55 | + all_records[""] = { |
| 56 | + 'swift-dependencies': './main-buildrecord.swiftdeps' |
| 57 | + } |
| 58 | + |
| 59 | + with open(output_path, 'w') as f: |
| 60 | + json.dump(all_records, f) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + sys.exit(main(sys.argv[1:])) |
0 commit comments