|
12 | 12 | import re |
13 | 13 | import sys |
14 | 14 |
|
15 | | -SWIFTMODULE_BUNDLE_RE = re.compile( |
16 | | - r'key.filepath: ".*[/\\](.*)\.swiftmodule[/\\].*\.swiftmodule"') |
17 | | -SWIFTMODULE_RE = re.compile(r'key.filepath: ".*[/\\](.*)\.swiftmodule"') |
18 | | -SWIFT_RE = re.compile(r'key.filepath: ".*[/\\](.*)\.swift"') |
19 | | -PCM_RE = re.compile(r'key.filepath: ".*[/\\](.*)-[0-9A-Z]*\.pcm"') |
20 | | -HEADER_RE = re.compile(r' file=\\".*[/\\](.*)\.h\\"') |
| 15 | +# I'm sorry dear reader, unfortunately my knowledge of regex trumps my knowledge of |
| 16 | +# Python. This pattern allows us to clean up file paths by stripping them down to |
| 17 | +# just the relevant file name. |
| 18 | +RE = re.compile( |
| 19 | + # The key can either be 'filepath' or 'buffer_name'. Also apply this logic to |
| 20 | + # the file name in XML, which is written 'file=\"...\"'. |
| 21 | + r'(key\.(?:filepath|buffer_name): |file=)' |
| 22 | + |
| 23 | + # Open delimiter with optional escape. |
| 24 | + r'\\?"' |
| 25 | + |
| 26 | + # Lazily match characters until we hit a slash, then match any non-slash that |
| 27 | + # ends in the right file extension, capturing the result. |
| 28 | + r'.*?[/\\]+([^/\\]*\.(?:swiftmodule|swift|pcm|h))' |
| 29 | + |
| 30 | + # For swiftmodule bundles, we want to match against the directory name, so |
| 31 | + # optionally match the .swiftmodule filename here. The lazy matching of the |
| 32 | + # previous logic means we'll prefer to match the previous '.swiftmodule' as the |
| 33 | + # directory. |
| 34 | + r'(?:[/\\]+[^/\\]*\.swiftmodule)?' |
| 35 | + |
| 36 | + # Close delimiter with optional escape. |
| 37 | + r'\\?"' |
| 38 | +) |
21 | 39 |
|
22 | 40 | try: |
23 | 41 | for line in sys.stdin.readlines(): |
24 | | - line = re.sub(SWIFTMODULE_BUNDLE_RE, |
25 | | - r'key.filepath: \1.swiftmodule', line) |
26 | | - line = re.sub(SWIFTMODULE_RE, r'key.filepath: \1.swiftmodule', line) |
27 | | - line = re.sub(SWIFT_RE, r'key.filepath: \1.swift', line) |
28 | | - line = re.sub(PCM_RE, r'key.filepath: \1.pcm', line) |
29 | | - line = re.sub(HEADER_RE, r' file=\1.h', line) |
| 42 | + # We substitute in both the key and the matched filename. |
| 43 | + line = re.sub(RE, r'\1\2', line) |
30 | 44 | sys.stdout.write(line) |
31 | 45 | except KeyboardInterrupt: |
32 | 46 | sys.stdout.flush() |
0 commit comments