|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import json |
| 6 | +import re |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def replace_once(path, old, new): |
| 12 | + text = path.read_text(encoding="utf-8") |
| 13 | + count = text.count(old) |
| 14 | + if count != 1: |
| 15 | + raise RuntimeError("Expected exactly one match in {0}, found {1}".format(path, count)) |
| 16 | + path.write_text(text.replace(old, new), encoding="utf-8") |
| 17 | + |
| 18 | + |
| 19 | +def replace_regex_once(path, pattern, replacement): |
| 20 | + text = path.read_text(encoding="utf-8") |
| 21 | + text, count = re.subn(pattern, replacement, text, count=1) |
| 22 | + if count != 1: |
| 23 | + raise RuntimeError("Expected exactly one regex match in {0}, found {1}".format(path, count)) |
| 24 | + path.write_text(text, encoding="utf-8") |
| 25 | + |
| 26 | + |
| 27 | +def append_once(path, marker, block): |
| 28 | + text = path.read_text(encoding="utf-8") |
| 29 | + if marker in text: |
| 30 | + return |
| 31 | + path.write_text(text.rstrip() + "\n\n" + block + "\n", encoding="utf-8") |
| 32 | + |
| 33 | + |
| 34 | +def patch_url_session(sources_dir): |
| 35 | + url_session = sources_dir / "URLSessionImplementations.swift" |
| 36 | + |
| 37 | + replace_once( |
| 38 | + url_session, |
| 39 | + "import Foundation\n#if !os(macOS)\nimport MobileCoreServices\n#endif", |
| 40 | + "import Foundation\n" |
| 41 | + "#if canImport(FoundationNetworking)\n" |
| 42 | + "import FoundationNetworking\n" |
| 43 | + "#endif\n" |
| 44 | + "#if canImport(MobileCoreServices)\n" |
| 45 | + "import MobileCoreServices\n" |
| 46 | + "#endif", |
| 47 | + ) |
| 48 | + |
| 49 | + replace_once( |
| 50 | + url_session, |
| 51 | + " } else {\n" |
| 52 | + " if let uti = UTTypeCreatePreferredIdentifierForTag", |
| 53 | + " } else {\n" |
| 54 | + " #if canImport(MobileCoreServices)\n" |
| 55 | + " if let uti = UTTypeCreatePreferredIdentifierForTag", |
| 56 | + ) |
| 57 | + |
| 58 | + replace_once( |
| 59 | + url_session, |
| 60 | + " return mimetype as String\n" |
| 61 | + " }\n" |
| 62 | + " return \"application/octet-stream\"", |
| 63 | + " return mimetype as String\n" |
| 64 | + " }\n" |
| 65 | + " #endif\n" |
| 66 | + " return \"application/octet-stream\"", |
| 67 | + ) |
| 68 | + |
| 69 | + replace_regex_once( |
| 70 | + url_session, |
| 71 | + r"\n #else\n return \"application/octet-stream\"\s*\n #endif", |
| 72 | + "\n #endif", |
| 73 | + ) |
| 74 | + |
| 75 | + replace_once(url_session, "private class SessionDelegate", "private final class SessionDelegate") |
| 76 | + |
| 77 | + |
| 78 | +def patch_extensions(sources_dir): |
| 79 | + replace_once( |
| 80 | + sources_dir / "Extensions.swift", |
| 81 | + "extension String: CodingKey {", |
| 82 | + "extension Swift.String: Swift.CodingKey {", |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def patch_date_formatter(sources_dir): |
| 87 | + append_once( |
| 88 | + sources_dir / "OpenISO8601DateFormatter.swift", |
| 89 | + "extension OpenISO8601DateFormatter: @unchecked Sendable", |
| 90 | + "#if compiler(>=5.5)\n" |
| 91 | + "extension OpenISO8601DateFormatter: @unchecked Sendable {}\n" |
| 92 | + "#endif", |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def patch_client_version(sources_dir, package_version): |
| 97 | + replace_regex_once( |
| 98 | + sources_dir / "AsposeBarcodeCloudClient.swift", |
| 99 | + r'public static let defaultSdkVersion = "[^"]+"', |
| 100 | + 'public static let defaultSdkVersion = "{0}"'.format(package_version), |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def main(target_dir, config_path): |
| 105 | + config = json.loads(Path(config_path).read_text(encoding="utf-8")) |
| 106 | + package_version = config.get("packageVersion") |
| 107 | + if not package_version: |
| 108 | + raise RuntimeError("config-swift.json must define packageVersion") |
| 109 | + |
| 110 | + sources_dir = Path(target_dir) / "Sources" / "AsposeBarcodeCloud" |
| 111 | + patch_url_session(sources_dir) |
| 112 | + patch_extensions(sources_dir) |
| 113 | + patch_date_formatter(sources_dir) |
| 114 | + patch_client_version(sources_dir, package_version) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + if len(sys.argv) != 3: |
| 119 | + print("Usage: patch-swift-generated.py <target-dir> <config-swift.json>", file=sys.stderr) |
| 120 | + sys.exit(2) |
| 121 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments