forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.swift
More file actions
89 lines (74 loc) · 2.96 KB
/
CodeGenerator.swift
File metadata and controls
89 lines (74 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Foundation
public class CodeGenerator<Decoder: FlexibleDecoder> {
let astOutput: ASTOutput
public class func jsonGenerator(with decoder: JSONDecoder = JSONDecoder(), astOutputURL url: URL) throws -> CodeGenerator<JSONDecoder> {
/// Type needs to be specified here due to https://bugs.swift.org/browse/SR-12325
try CodeGenerator<JSONDecoder>(flexible: decoder, astOutputURL: url)
}
private var singleFileOutputs = [String]()
public init(flexible: Decoder,
astOutputURL url: URL) throws {
self.astOutput = try ASTOutput.load(from: url, decoder: flexible)
}
public func run(with options: ApolloCodegenOptions) throws {
try self.generateTypesUsed(with: options)
try self.generateFragments(with: options)
try self.generateOperations(with: options)
switch options.outputFormat {
case .singleFile(let fileURL):
let joinedContent = self.singleFileOutputs.joined(separator: "\n\n")
try self.writeOutput(joinedContent, toFile: fileURL)
case .multipleFiles:
// This is done as each file is generated.
break
}
}
private func generateTypesUsed(with options: ApolloCodegenOptions) throws {
for type in self.astOutput.typesUsed {
switch type.kind {
case .EnumType:
let enumOutput = try EnumGenerator().run(typeUsed: type, options: options)
try self.renderOutput(enumOutput, named: type.name, with: options)
case .InputObjectType:
try self.renderOutput("Not done yet", named: type.name, with: options)
}
}
}
private func generateFragments(with options: ApolloCodegenOptions) throws {
// TODO!
}
private func generateOperations(with options: ApolloCodegenOptions) throws {
// TODO
}
private func renderOutput(_ output: String,
named name: String,
with options: ApolloCodegenOptions) throws {
switch options.outputFormat {
case .singleFile:
self.singleFileOutputs.append(output)
case .multipleFiles(let folderURL):
try self.createFileWithOutput(output,
named: name,
inFolder: folderURL)
}
}
private func createFileWithOutput(_ output: String,
named name: String,
inFolder folderURL: URL) throws {
try FileManager.default.apollo.createFolderIfNeeded(at: folderURL)
let fileURL = folderURL
.appendingPathComponent(name)
.appendingPathExtension("swift")
try self.writeOutput(output, toFile: fileURL)
}
private func writeOutput(_ output: String,
toFile fileURL: URL) throws {
let finalOutput = """
// @generated
// This file was automatically generated by apollo-ios and should not be edited.
""" + output
try finalOutput.write(to: fileURL,
atomically: true,
encoding: .utf8)
}
}