File: ConfigSchemaGen.swift

package info (click to toggle)
swiftlang 6.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,748 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (151 lines) | stat: -rw-r--r-- 5,492 bytes parent folder | download | duplicates (2)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import SwiftParser
import SwiftSyntax

/// The main entry point for generating a JSON schema and Markdown documentation
/// for the SourceKit-LSP configuration file format
/// (`.sourcekit-lsp/config.json`) from the Swift type definitions in
/// `SKOptions` Swift module.
package struct ConfigSchemaGen {
  private struct WritePlan {
    fileprivate let category: String
    fileprivate let path: URL
    fileprivate let contents: () throws -> Data

    fileprivate func write() throws {
      try contents().write(to: path)
    }
  }

  private static let projectRoot = URL(fileURLWithPath: #filePath)
    .deletingLastPathComponent()
    .deletingLastPathComponent()
    .deletingLastPathComponent()
    .deletingLastPathComponent()
  private static let sourceDir =
    projectRoot
    .appendingPathComponent("Sources")
    .appendingPathComponent("SKOptions")
  private static let configSchemaJSONPath =
    projectRoot
    .appendingPathComponent("config.schema.json")
  private static let configSchemaDocPath =
    projectRoot
    .appendingPathComponent("Documentation")
    .appendingPathComponent("Configuration File.md")

  /// Generates and writes the JSON schema and documentation for the SourceKit-LSP configuration file format.
  package static func generate() throws {
    let plans = try plan()
    for plan in plans {
      print("Writing \(plan.category) to \"\(plan.path.path)\"")
      try plan.write()
    }
  }

  /// Verifies that the generated JSON schema and documentation in the current source tree
  /// are up-to-date with the Swift type definitions in `SKOptions`.
  /// - Returns: `true` if the generated files are up-to-date, `false` otherwise.
  package static func verify() throws -> Bool {
    let plans = try plan()
    for plan in plans {
      print("Verifying \(plan.category) at \"\(plan.path.path)\"")
      let expectedContents = try plan.contents()
      let actualContents = try Data(contentsOf: plan.path)
      guard expectedContents == actualContents else {
        print("error: \(plan.category) is out-of-date!")
        print("Please run `./sourcekit-lsp-dev-utils generate-config-schema` to update it.")
        return false
      }
    }
    return true
  }

  private static func plan() throws -> [WritePlan] {
    let sourceFiles = FileManager.default.enumerator(at: sourceDir, includingPropertiesForKeys: nil)!
    let typeNameResolver = TypeDeclResolver()

    for case let fileURL as URL in sourceFiles {
      guard fileURL.pathExtension == "swift" else {
        continue
      }
      let sourceText = try String(contentsOf: fileURL)
      let sourceFile = Parser.parse(source: sourceText)
      typeNameResolver.collect(from: sourceFile)
    }
    let rootTypeDecl = try typeNameResolver.lookupType(fullyQualified: ["SourceKitLSPOptions"])
    let context = OptionSchemaContext(typeNameResolver: typeNameResolver)
    var schema = try context.buildSchema(from: rootTypeDecl)

    // Manually annotate the logging level enum since LogLevel type exists
    // outside of the SKOptions module
    schema["logging"]?["level"]?.kind = .enum(
      OptionTypeSchama.Enum(
        name: "LogLevel",
        cases: ["debug", "info", "default", "error", "fault"].map {
          OptionTypeSchama.Case(name: $0)
        }
      )
    )
    schema["logging"]?["privacyLevel"]?.kind = .enum(
      OptionTypeSchama.Enum(
        name: "PrivacyLevel",
        cases: ["public", "private", "sensitive"].map {
          OptionTypeSchama.Case(name: $0)
        }
      )
    )

    return [
      WritePlan(
        category: "JSON Schema",
        path: configSchemaJSONPath,
        contents: { try generateJSONSchema(from: schema, context: context) }
      ),
      WritePlan(
        category: "Schema Documentation",
        path: configSchemaDocPath,
        contents: { try generateDocumentation(from: schema, context: context) }
      ),
    ]
  }

  private static func generateJSONSchema(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data {
    let schemaBuilder = JSONSchemaBuilder(context: context)
    var jsonSchema = try schemaBuilder.build(from: schema)
    jsonSchema.title = "SourceKit-LSP Configuration"
    jsonSchema.comment = "DO NOT EDIT THIS FILE. This file is generated by \(#fileID)."
    let encoder = JSONEncoder()
    encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
    return try encoder.encode(jsonSchema)
  }

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

struct ConfigSchemaGenError: Error, CustomStringConvertible {
  let description: String

  init(_ description: String) {
    self.description = description
  }
}