@@ -18,43 +18,62 @@ import SwiftSyntax
18
18
/// for the SourceKit-LSP configuration file format
19
19
/// (`.sourcekit-lsp/config.json`) from the Swift type definitions in
20
20
/// `SKOptions` Swift module.
21
- public struct ConfigSchemaGen {
22
- public struct WritePlan {
23
- public let category : String
24
- public let path : URL
25
- public let contents : ( ) throws -> Data
21
+ package struct ConfigSchemaGen {
22
+ private struct WritePlan {
23
+ fileprivate let category : String
24
+ fileprivate let path : URL
25
+ fileprivate let contents : ( ) throws -> Data
26
26
27
- public func write( ) throws {
27
+ fileprivate func write( ) throws {
28
28
try contents ( ) . write ( to: path)
29
29
}
30
30
}
31
31
32
- static let projectRoot = URL ( fileURLWithPath: #filePath)
32
+ private static let projectRoot = URL ( fileURLWithPath: #filePath)
33
33
. deletingLastPathComponent ( )
34
34
. deletingLastPathComponent ( )
35
35
. deletingLastPathComponent ( )
36
36
. deletingLastPathComponent ( )
37
- static let sourceDir =
37
+ private static let sourceDir =
38
38
projectRoot
39
39
. appendingPathComponent ( " Sources " )
40
40
. appendingPathComponent ( " SKOptions " )
41
- static let configSchemaJSONPath =
41
+ private static let configSchemaJSONPath =
42
42
projectRoot
43
43
. appendingPathComponent ( " config.schema.json " )
44
- static let configSchemaDocPath =
44
+ private static let configSchemaDocPath =
45
45
projectRoot
46
46
. appendingPathComponent ( " Documentation " )
47
47
. appendingPathComponent ( " Configuration File.md " )
48
48
49
- public static func generate( ) throws {
49
+ /// Generates and writes the JSON schema and documentation for the SourceKit-LSP configuration file format.
50
+ package static func generate( ) throws {
50
51
let plans = try plan ( )
51
52
for plan in plans {
52
53
print ( " Writing \( plan. category) to \" \( plan. path. path) \" " )
53
54
try plan. write ( )
54
55
}
55
56
}
56
57
57
- public static func plan( ) throws -> [ WritePlan ] {
58
+ /// Verifies that the generated JSON schema and documentation in the current source tree
59
+ /// are up-to-date with the Swift type definitions in `SKOptions`.
60
+ /// - Returns: `true` if the generated files are up-to-date, `false` otherwise.
61
+ package static func verify( ) throws -> Bool {
62
+ let plans = try plan ( )
63
+ for plan in plans {
64
+ print ( " Verifying \( plan. category) at \" \( plan. path. path) \" " )
65
+ let expectedContents = try plan. contents ( )
66
+ let actualContents = try Data ( contentsOf: plan. path)
67
+ guard expectedContents == actualContents else {
68
+ print ( " error: \( plan. category) is out-of-date! " )
69
+ print ( " Please run `./sourcekit-lsp-dev-utils generate-config-schema` to update it. " )
70
+ return false
71
+ }
72
+ }
73
+ return true
74
+ }
75
+
76
+ private static func plan( ) throws -> [ WritePlan ] {
58
77
let sourceFiles = FileManager . default. enumerator ( at: sourceDir, includingPropertiesForKeys: nil ) !
59
78
let typeNameResolver = TypeDeclResolver ( )
60
79
@@ -89,27 +108,21 @@ public struct ConfigSchemaGen {
89
108
)
90
109
)
91
110
92
- var plans : [ WritePlan ] = [ ]
93
-
94
- plans. append (
111
+ return [
95
112
WritePlan (
96
113
category: " JSON Schema " ,
97
114
path: configSchemaJSONPath,
98
115
contents: { try generateJSONSchema ( from: schema, context: context) }
99
- )
100
- )
101
-
102
- plans. append (
116
+ ) ,
103
117
WritePlan (
104
118
category: " Schema Documentation " ,
105
119
path: configSchemaDocPath,
106
120
contents: { try generateDocumentation ( from: schema, context: context) }
107
- )
108
- )
109
- return plans
121
+ ) ,
122
+ ]
110
123
}
111
124
112
- static func generateJSONSchema( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data {
125
+ private static func generateJSONSchema( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data {
113
126
let schemaBuilder = JSONSchemaBuilder ( context: context)
114
127
var jsonSchema = try schemaBuilder. build ( from: schema)
115
128
jsonSchema. title = " SourceKit-LSP Configuration "
@@ -119,7 +132,8 @@ public struct ConfigSchemaGen {
119
132
return try encoder. encode ( jsonSchema)
120
133
}
121
134
122
- static func generateDocumentation( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data {
135
+ private static func generateDocumentation( from schema: OptionTypeSchama , context: OptionSchemaContext ) throws -> Data
136
+ {
123
137
let docBuilder = OptionDocumentBuilder ( context: context)
124
138
guard let data = try docBuilder. build ( from: schema) . data ( using: . utf8) else {
125
139
throw ConfigSchemaGenError ( " Failed to encode documentation as UTF-8 " )
0 commit comments