mirror of
https://github.com/TelegramMessenger/Telegram-iOS.git
synced 2026-07-06 03:33:41 +02:00
SwiftTL: main.swift branches on ParsedSchema
Flat schemas keep the existing generate(...) pipeline. Layered
schemas iterate resolveLayeredTypes and write one
{apiPrefix}Layer{N}.swift per layer via generateLayered.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
803bf23269
commit
57bfe559ea
1 changed files with 55 additions and 46 deletions
|
|
@ -44,54 +44,63 @@ guard let data = try? String(contentsOfFile: schemeFilePath) else {
|
|||
exit(1)
|
||||
}
|
||||
|
||||
do {
|
||||
let parsedData = try DescriptionParser.parse(data: data)
|
||||
let resolvedTypes = try Resolver.resolveTypes(constructors: parsedData.constructors)
|
||||
var resolvedFunctions = try Resolver.resolveFunctions(types: resolvedTypes, functionDescriptions: parsedData.functions)
|
||||
|
||||
resolvedFunctions.append(Resolver.Function(name: QualifiedName(namespace: "help", value: "test"), id: 0xc0e202f7, arguments: [], result: .boxedType(QualifiedName(namespace: nil, value: "Bool"))))
|
||||
|
||||
var constructorOrder: [(typeName: QualifiedName, constructorName: String)] = []
|
||||
var typeOrder: [(types: [(typeName: QualifiedName, constructorNames: [String])], functions: [QualifiedName])] = []
|
||||
|
||||
let sortedTypes = resolvedTypes.sorted(by: { $0.name < $1.name })
|
||||
do {
|
||||
let parsedSchema = try DescriptionParser.parse(data: data)
|
||||
|
||||
for type in sortedTypes {
|
||||
for constructor in type.constructors.values.sorted(by: { $0.name < $1.name }) {
|
||||
constructorOrder.append((type.name, constructor.name.value))
|
||||
}
|
||||
}
|
||||
|
||||
var totalConstructorCount = 0
|
||||
var currentConstructorCount = 0
|
||||
for type in sortedTypes {
|
||||
if typeOrder.isEmpty || currentConstructorCount >= 32 {
|
||||
typeOrder.append(([], []))
|
||||
currentConstructorCount = 0
|
||||
}
|
||||
|
||||
typeOrder[typeOrder.count - 1].types.append((type.name, type.constructors.values.sorted(by: { $0.name < $1.name }).map(\.name.value)))
|
||||
|
||||
currentConstructorCount += type.constructors.count
|
||||
totalConstructorCount += type.constructors.count
|
||||
|
||||
if totalConstructorCount > 40 {
|
||||
}
|
||||
}
|
||||
|
||||
typeOrder.append(([], []))
|
||||
for function in resolvedFunctions.sorted(by: { $0.name < $1.name }) {
|
||||
typeOrder[typeOrder.count - 1].functions.append(function.name)
|
||||
}
|
||||
|
||||
try FileManager.default.createDirectory(at: URL(fileURLWithPath: outputDirectoryPath), withIntermediateDirectories: true, attributes: nil)
|
||||
|
||||
let generatedFiles = try CodeGenerator.generate(apiPrefix: apiPrefix, types: resolvedTypes, functions: resolvedFunctions, constructorOrder: constructorOrder, typeOrder: typeOrder)
|
||||
|
||||
for (name, fileData) in generatedFiles {
|
||||
let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(name).path
|
||||
let _ = try? FileManager.default.removeItem(atPath: filePath)
|
||||
try fileData.write(toFile: filePath, atomically: true, encoding: .utf8)
|
||||
|
||||
switch parsedSchema {
|
||||
case let .flat(constructors, functions):
|
||||
let resolvedTypes = try Resolver.resolveTypes(constructors: constructors)
|
||||
var resolvedFunctions = try Resolver.resolveFunctions(types: resolvedTypes, functionDescriptions: functions)
|
||||
|
||||
resolvedFunctions.append(Resolver.Function(name: QualifiedName(namespace: "help", value: "test"), id: 0xc0e202f7, arguments: [], result: .boxedType(QualifiedName(namespace: nil, value: "Bool"))))
|
||||
|
||||
var constructorOrder: [(typeName: QualifiedName, constructorName: String)] = []
|
||||
var typeOrder: [(types: [(typeName: QualifiedName, constructorNames: [String])], functions: [QualifiedName])] = []
|
||||
|
||||
let sortedTypes = resolvedTypes.sorted(by: { $0.name < $1.name })
|
||||
|
||||
for type in sortedTypes {
|
||||
for constructor in type.constructors.values.sorted(by: { $0.name < $1.name }) {
|
||||
constructorOrder.append((type.name, constructor.name.value))
|
||||
}
|
||||
}
|
||||
|
||||
var totalConstructorCount = 0
|
||||
var currentConstructorCount = 0
|
||||
for type in sortedTypes {
|
||||
if typeOrder.isEmpty || currentConstructorCount >= 32 {
|
||||
typeOrder.append(([], []))
|
||||
currentConstructorCount = 0
|
||||
}
|
||||
typeOrder[typeOrder.count - 1].types.append((type.name, type.constructors.values.sorted(by: { $0.name < $1.name }).map(\.name.value)))
|
||||
currentConstructorCount += type.constructors.count
|
||||
totalConstructorCount += type.constructors.count
|
||||
if totalConstructorCount > 40 { }
|
||||
}
|
||||
|
||||
typeOrder.append(([], []))
|
||||
for function in resolvedFunctions.sorted(by: { $0.name < $1.name }) {
|
||||
typeOrder[typeOrder.count - 1].functions.append(function.name)
|
||||
}
|
||||
|
||||
let generatedFiles = try CodeGenerator.generate(apiPrefix: apiPrefix, types: resolvedTypes, functions: resolvedFunctions, constructorOrder: constructorOrder, typeOrder: typeOrder)
|
||||
|
||||
for (name, fileData) in generatedFiles {
|
||||
let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(name).path
|
||||
let _ = try? FileManager.default.removeItem(atPath: filePath)
|
||||
try fileData.write(toFile: filePath, atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
case let .layered(layers):
|
||||
let resolvedLayers = try Resolver.resolveLayeredTypes(layers: layers)
|
||||
for (layerNumber, types) in resolvedLayers {
|
||||
let (filename, source) = try CodeGenerator.generateLayered(apiPrefix: apiPrefix, layerNumber: layerNumber, types: types)
|
||||
let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(filename).path
|
||||
let _ = try? FileManager.default.removeItem(atPath: filePath)
|
||||
try source.write(toFile: filePath, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
} catch let e {
|
||||
print("\(e)")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue