@@ -19,6 +19,16 @@ import SwiftSyntax
19
19
/// (`.sourcekit-lsp/config.json`) from the Swift type definitions in
20
20
/// `SKOptions` Swift module.
21
21
public struct ConfigSchemaGen {
22
+ public struct WritePlan {
23
+ public let category : String
24
+ public let path : URL
25
+ public let contents : ( ) throws -> Data
26
+
27
+ public func write( ) throws {
28
+ try contents ( ) . write ( to: path)
29
+ }
30
+ }
31
+
22
32
static let projectRoot = URL ( fileURLWithPath: #filePath)
23
33
. deletingLastPathComponent ( )
24
34
. deletingLastPathComponent ( )
@@ -37,6 +47,14 @@ public struct ConfigSchemaGen {
37
47
. appendingPathComponent ( " Configuration File.md " )
38
48
39
49
public static func generate( ) throws {
50
+ let plans = try plan ( )
51
+ for plan in plans {
52
+ print ( " Writing \( plan. category) to \" \( plan. path. path) \" " )
53
+ try plan. write ( )
54
+ }
55
+ }
56
+
57
+ public static func plan( ) throws -> [ WritePlan ] {
40
58
let sourceFiles = FileManager . default. enumerator ( at: sourceDir, includingPropertiesForKeys: nil ) !
41
59
let typeNameResolver = TypeDeclResolver ( )
42
60
@@ -71,15 +89,20 @@ public struct ConfigSchemaGen {
71
89
)
72
90
)
73
91
74
- print ( " Writing schema to \( configSchemaJSONPath. path) " )
75
- try generateJSONSchema ( from: schema, context: context) . write ( to: configSchemaJSONPath)
92
+ var plans : [ WritePlan ] = [ ]
76
93
77
- print ( " Writing schema documentation to \( configSchemaDocPath. path) " )
78
- try generateDocumentation ( from: schema, context: context) . write (
79
- to: configSchemaDocPath,
80
- atomically: true ,
81
- encoding: . utf8
82
- )
94
+ plans. append ( WritePlan (
95
+ category: " JSON Schema " ,
96
+ path: configSchemaJSONPath,
97
+ contents: { try generateJSONSchema ( from: schema, context: context) }
98
+ ) )
99
+
100
+ plans. append ( WritePlan (
101
+ category: " Schema Documentation " ,
102
+ path: configSchemaDocPath,
103
+ contents: { try generateDocumentation ( from: schema, context: context) }
104
+ ) )
105
+ return plans
83
106
}
84
107
85
108
static func generateJSONSchema( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data {
@@ -92,8 +115,22 @@ public struct ConfigSchemaGen {
92
115
return try encoder. encode ( jsonSchema)
93
116
}
94
117
95
- static func generateDocumentation( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> String {
118
+ static func generateDocumentation( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data {
96
119
let docBuilder = OptionDocumentBuilder ( context: context)
97
- return try docBuilder. build ( from: schema)
120
+ guard let data = try docBuilder. build ( from: schema) . data ( using: . utf8) else {
121
+ throw ConfigSchemaGenError . documentationEncodingFailed
122
+ }
123
+ return data
124
+ }
125
+ }
126
+
127
+ enum ConfigSchemaGenError : Error , CustomStringConvertible {
128
+ case documentationEncodingFailed
129
+
130
+ var description : String {
131
+ switch self {
132
+ case . documentationEncodingFailed:
133
+ return " Failed to encode documentation as UTF-8 "
134
+ }
98
135
}
99
136
}
0 commit comments