Skip to content

Commit 6803d8c

Browse files
Address code style feedbacks
1 parent 185438c commit 6803d8c

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

SourceKitLSPDevUtils/Sources/ConfigSchemaGen/ConfigSchemaGen.swift

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,62 @@ import SwiftSyntax
1818
/// for the SourceKit-LSP configuration file format
1919
/// (`.sourcekit-lsp/config.json`) from the Swift type definitions in
2020
/// `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
2626

27-
public func write() throws {
27+
fileprivate func write() throws {
2828
try contents().write(to: path)
2929
}
3030
}
3131

32-
static let projectRoot = URL(fileURLWithPath: #filePath)
32+
private static let projectRoot = URL(fileURLWithPath: #filePath)
3333
.deletingLastPathComponent()
3434
.deletingLastPathComponent()
3535
.deletingLastPathComponent()
3636
.deletingLastPathComponent()
37-
static let sourceDir =
37+
private static let sourceDir =
3838
projectRoot
3939
.appendingPathComponent("Sources")
4040
.appendingPathComponent("SKOptions")
41-
static let configSchemaJSONPath =
41+
private static let configSchemaJSONPath =
4242
projectRoot
4343
.appendingPathComponent("config.schema.json")
44-
static let configSchemaDocPath =
44+
private static let configSchemaDocPath =
4545
projectRoot
4646
.appendingPathComponent("Documentation")
4747
.appendingPathComponent("Configuration File.md")
4848

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 {
5051
let plans = try plan()
5152
for plan in plans {
5253
print("Writing \(plan.category) to \"\(plan.path.path)\"")
5354
try plan.write()
5455
}
5556
}
5657

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] {
5877
let sourceFiles = FileManager.default.enumerator(at: sourceDir, includingPropertiesForKeys: nil)!
5978
let typeNameResolver = TypeDeclResolver()
6079

@@ -89,27 +108,21 @@ public struct ConfigSchemaGen {
89108
)
90109
)
91110

92-
var plans: [WritePlan] = []
93-
94-
plans.append(
111+
return [
95112
WritePlan(
96113
category: "JSON Schema",
97114
path: configSchemaJSONPath,
98115
contents: { try generateJSONSchema(from: schema, context: context) }
99-
)
100-
)
101-
102-
plans.append(
116+
),
103117
WritePlan(
104118
category: "Schema Documentation",
105119
path: configSchemaDocPath,
106120
contents: { try generateDocumentation(from: schema, context: context) }
107-
)
108-
)
109-
return plans
121+
),
122+
]
110123
}
111124

112-
static func generateJSONSchema(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data {
125+
private static func generateJSONSchema(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data {
113126
let schemaBuilder = JSONSchemaBuilder(context: context)
114127
var jsonSchema = try schemaBuilder.build(from: schema)
115128
jsonSchema.title = "SourceKit-LSP Configuration"
@@ -119,7 +132,8 @@ public struct ConfigSchemaGen {
119132
return try encoder.encode(jsonSchema)
120133
}
121134

122-
static func generateDocumentation(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data {
135+
private static func generateDocumentation(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data
136+
{
123137
let docBuilder = OptionDocumentBuilder(context: context)
124138
guard let data = try docBuilder.build(from: schema).data(using: .utf8) else {
125139
throw ConfigSchemaGenError("Failed to encode documentation as UTF-8")

SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionDocument.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,4 @@ struct OptionDocumentBuilder {
104104
return shouldWrap ? "(\(cases))" : cases
105105
}
106106
}
107-
108107
}

SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/VerifyConfigSchema.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@ struct VerifyConfigSchema: ParsableCommand {
2121
)
2222

2323
func run() throws {
24-
let plans = try ConfigSchemaGen.plan()
25-
for plan in plans {
26-
print("Verifying \(plan.category) at \"\(plan.path.path)\"")
27-
let expectedContents = try plan.contents()
28-
let actualContents = try Data(contentsOf: plan.path)
29-
guard expectedContents == actualContents else {
30-
print("FATAL: \(plan.category) is out-of-date!")
31-
print("Please run `./sourcekit-lsp-dev-utils generate-config-schema` to update it.")
32-
throw ExitCode.failure
33-
}
24+
guard try ConfigSchemaGen.verify() else {
25+
throw ExitCode.failure
3426
}
3527
print("All schemas are up-to-date!")
3628
}

0 commit comments

Comments
 (0)