|
| 1 | +import Foundation |
| 2 | + |
| 3 | +func log(_ text: String) { |
| 4 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 5 | + print(text) |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +let knownImportsToIgnore = [ |
| 10 | + "Darwin", |
| 11 | + "Dispatch", |
| 12 | + "Foundation", |
| 13 | + "XCTest", |
| 14 | + "Network", |
| 15 | +] |
| 16 | + |
| 17 | +let packageNameForDependency = [ |
| 18 | + "ArgumentParser": "swift-argument-parser", |
| 19 | +] |
| 20 | + |
| 21 | +let jsonEncoder = JSONEncoder() |
| 22 | +jsonEncoder.outputFormatting = [.prettyPrinted, .sortedKeys] |
| 23 | + |
| 24 | +let importStatementExpression = try NSRegularExpression( |
| 25 | + pattern: "^(@testable )?import (.*)$", |
| 26 | + options: [.anchorsMatchLines] |
| 27 | +) |
| 28 | + |
| 29 | +struct ModuleDescription { |
| 30 | + let name: String |
| 31 | + let deps: [String] |
| 32 | + let path: String |
| 33 | + let isTest: Bool |
| 34 | +} |
| 35 | + |
| 36 | +func generate(at url: URL, isTestTarget: Bool) throws -> [ModuleDescription] { |
| 37 | + guard let enumerator = FileManager().enumerator( |
| 38 | + at: url, |
| 39 | + includingPropertiesForKeys: nil, |
| 40 | + options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles] |
| 41 | + ) else { |
| 42 | + print("Failed to create file enumerator for url '\(url)'") |
| 43 | + return [] |
| 44 | + } |
| 45 | + |
| 46 | + var result = [ModuleDescription]() |
| 47 | + |
| 48 | + while let moduleFolderUrl = enumerator.nextObject() as? URL { |
| 49 | + let moduleEnumerator = FileManager().enumerator( |
| 50 | + at: moduleFolderUrl, |
| 51 | + includingPropertiesForKeys: [.isRegularFileKey], |
| 52 | + options: [.skipsHiddenFiles] |
| 53 | + ) |
| 54 | + let moduleName = moduleFolderUrl.lastPathComponent |
| 55 | + log("Analyzing \(moduleName)") |
| 56 | + |
| 57 | + var importedModuleNames = Set<String>() |
| 58 | + |
| 59 | + while let moduleFile = moduleEnumerator?.nextObject() as? URL { |
| 60 | + if ["md"].contains(moduleFile.pathExtension) { |
| 61 | + continue |
| 62 | + } |
| 63 | + log(" Analyzing \(moduleFile.lastPathComponent)") |
| 64 | + guard try moduleFile.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile == true else { |
| 65 | + log(" Skipping \(moduleFile.lastPathComponent): is not regular file") |
| 66 | + continue |
| 67 | + } |
| 68 | + let fileContents = try String(contentsOf: moduleFile) |
| 69 | + .split(separator: "\n") |
| 70 | + .filter { !$0.starts(with: "//") } |
| 71 | + for line in fileContents { |
| 72 | + let matches = importStatementExpression.matches(in: String(line), options: [], range: NSMakeRange(0, line.count)) |
| 73 | + guard matches.count == 1 else { |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + let importedModuleName = (line as NSString).substring(with: matches[0].range(at: 2)) |
| 78 | + importedModuleNames.insert(importedModuleName) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + let path = moduleFolderUrl.path.dropFirst(moduleFolderUrl.deletingLastPathComponent().deletingLastPathComponent().path.count + 1) |
| 83 | + let dependencies = importedModuleNames.filter { !knownImportsToIgnore.contains($0) }.sorted() |
| 84 | + |
| 85 | + let isTestHelper = moduleFolderUrl.path.hasSuffix("TestHelpers") |
| 86 | + |
| 87 | + result.append( |
| 88 | + ModuleDescription(name: moduleName, deps: dependencies, path: String(path), isTest: isTestTarget && !isTestHelper) |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + return result |
| 93 | +} |
| 94 | + |
| 95 | +func generatePackageSwift(raplacementForTargets: [String]) throws { |
| 96 | + log("Loading template") |
| 97 | + var templateContents = try String(contentsOf: URL(fileURLWithPath: "PackageTemplate.swift.txt")) |
| 98 | + templateContents = templateContents.replacingOccurrences( |
| 99 | + of: "<__TARGETS__>", |
| 100 | + with: raplacementForTargets.map { " \($0)" }.joined(separator: "\n") |
| 101 | + ) |
| 102 | + |
| 103 | + let packageSwiftPath = URL(fileURLWithPath: "Package.swift") |
| 104 | + |
| 105 | + if ProcessInfo.processInfo.environment["ON_CI"] != nil { |
| 106 | + log("Checking for Package.swift consistency") |
| 107 | + let existingContents = try String(contentsOf: packageSwiftPath) |
| 108 | + if existingContents != templateContents { |
| 109 | + print("\(#file):\(#line): ON_CI is set, and Package.swift differs. Please update and commit Package.swift!") |
| 110 | + exit(1) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + log("Saving Package.swift") |
| 115 | + try templateContents.write(to: packageSwiftPath, atomically: true, encoding: .utf8) |
| 116 | +} |
| 117 | + |
| 118 | +func main() throws { |
| 119 | + var moduleDescriptions = [ModuleDescription]() |
| 120 | + moduleDescriptions.append( |
| 121 | + contentsOf: try generate(at: URL(fileURLWithPath: "Sources"), isTestTarget: false) |
| 122 | + ) |
| 123 | + moduleDescriptions.append( |
| 124 | + contentsOf: try generate(at: URL(fileURLWithPath: "Tests"), isTestTarget: true) |
| 125 | + ) |
| 126 | + |
| 127 | + var generatedTargetStatements = [String]() |
| 128 | + let sortedModuleDescriptions: [ModuleDescription] = moduleDescriptions.sorted { $0.name < $1.name } |
| 129 | + for moduleDescription in sortedModuleDescriptions { |
| 130 | + generatedTargetStatements.append(".\(!moduleDescription.isTest ? "target" : "testTarget")(") |
| 131 | + generatedTargetStatements.append(" // MARK: \(moduleDescription.name)") |
| 132 | + generatedTargetStatements.append(" name: " + "\"\(moduleDescription.name)\"" + ",") |
| 133 | + generatedTargetStatements.append(" dependencies: [") |
| 134 | + for dependency in moduleDescription.deps { |
| 135 | + if let packageName = packageNameForDependency[dependency] { |
| 136 | + generatedTargetStatements.append(" .product(name: \"\(dependency)\", package: \"\(packageName)\"),") |
| 137 | + } else { |
| 138 | + generatedTargetStatements.append(" \"\(dependency)\",") |
| 139 | + } |
| 140 | + } |
| 141 | + generatedTargetStatements.append(" ],") |
| 142 | + generatedTargetStatements.append(" path: " + "\"" + moduleDescription.path + "\"") |
| 143 | + generatedTargetStatements.append("),") |
| 144 | + } |
| 145 | + try generatePackageSwift(raplacementForTargets: generatedTargetStatements) |
| 146 | +} |
| 147 | + |
| 148 | +try main() |
0 commit comments