Skip to content

Commit

Permalink
Remove swift-syntax/swift-format as generator dependencies (#343)
Browse files Browse the repository at this point in the history
Remove swift-syntax/swift-format as generator dependencies

### Motivation

The swift-syntax/swift-format dependencies were only used to reformat the code before writing to disk, and significantly increased build and execution times, as well as leading to more dependency graph conflicts for our adopters. Turns out we can relatively easily remove the dependency, so doing that in this PR.

### Modifications

Removed swift-format/swift-syntax as dependencies and replaced them with an improved version of `TextBasedRenderer`, which now adds some basic indentation, surprisingly getting us pretty close to the previous formatting.

### Result

No more swift-syntax/swift-format dependency, but the generated code still looks very reasonable, and build/execution times should get noticably faster.

### Test Plan

Adapted unit tests, integration tests: snippet and file-based tests. All passing now.


Reviewed by: glbrntt

Builds:
     ✔︎ pull request validation (5.10) - Build finished. 
     ✔︎ pull request validation (5.8) - Build finished. 
     ✔︎ pull request validation (5.9) - Build finished. 
     ✔︎ pull request validation (compatibility test) - Build finished. 
     ✔︎ pull request validation (docc test) - Build finished. 
     ✔︎ pull request validation (integration test) - Build finished. 
     ✔︎ pull request validation (nightly) - Build finished. 
     ✔︎ pull request validation (soundness) - Build finished. 

#343
  • Loading branch information
czechboy0 authored Oct 25, 2023
1 parent 193989e commit 3074113
Show file tree
Hide file tree
Showing 15 changed files with 2,345 additions and 1,283 deletions.
20 changes: 1 addition & 19 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ let package = Package(
],
dependencies: [

// Generate Swift code
.package(
url: "https://github.com/apple/swift-syntax.git",
"508.0.1"..<"510.0.0"
),

// Format Swift code
.package(
url: "https://github.com/apple/swift-format.git",
"508.0.1"..<"510.0.0"
),

// General algorithms
.package(
url: "https://github.com/apple/swift-algorithms",
Expand Down Expand Up @@ -105,10 +93,6 @@ let package = Package(
.product(name: "OpenAPIKitCompat", package: "OpenAPIKit"),
.product(name: "Algorithms", package: "swift-algorithms"),
.product(name: "Yams", package: "Yams"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftFormat", package: "swift-format"),
.product(name: "SwiftFormatConfiguration", package: "swift-format"),
],
swiftSettings: swiftSettings
),
Expand All @@ -126,9 +110,7 @@ let package = Package(
.testTarget(
name: "OpenAPIGeneratorReferenceTests",
dependencies: [
"_OpenAPIGeneratorCore",
.product(name: "SwiftFormat", package: "swift-format"),
.product(name: "SwiftFormatConfiguration", package: "swift-format"),
"_OpenAPIGeneratorCore"
],
resources: [
.copy("Resources")
Expand Down
25 changes: 0 additions & 25 deletions Sources/_OpenAPIGeneratorCore/Extensions/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation

extension Data {
/// A copy of the data formatted using swift-format.
///
/// Data is assumed to contain Swift code encoded using UTF-8.
///
/// - Throws: When data is not valid UTF-8.
var swiftFormatted: Data {
get throws {
let string = String(decoding: self, as: UTF8.self)
return try Self(string.swiftFormatted.utf8)
}
}
}

extension InMemoryInputFile {
/// Creates a new in-memory file by reading the contents at the specified path.
/// - Parameter url: The path to the file to read.
Expand All @@ -36,17 +22,6 @@ extension InMemoryInputFile {
}
}

extension InMemoryOutputFile {
/// A copy of the file formatted using swift-format.
public var swiftFormatted: InMemoryOutputFile {
get throws {
var new = self
new.contents = try contents.swiftFormatted
return new
}
}
}

/// File handle to stderr.
let stdErrHandle = FileHandle.standardError

Expand Down
63 changes: 0 additions & 63 deletions Sources/_OpenAPIGeneratorCore/Extensions/SwiftFormat.swift

This file was deleted.

8 changes: 2 additions & 6 deletions Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public func runGenerator(
/// - validator: A validator for parsed OpenAPI documents.
/// - translator: A translator from OpenAPI to Swift.
/// - renderer: A Swift code renderer.
/// - formatter: A Swift code formatter.
/// - config: A set of configuration values for the generator.
/// - diagnostics: A collector to which the generator emits diagnostics.
/// - Returns: A configured generator pipeline that can be executed with
Expand All @@ -110,8 +109,7 @@ func makeGeneratorPipeline(
parser: any ParserProtocol = YamsParser(),
validator: @escaping (ParsedOpenAPIRepresentation, Config) throws -> [Diagnostic] = validateDoc,
translator: any TranslatorProtocol = MultiplexTranslator(),
renderer: any RendererProtocol = TextBasedRenderer(),
formatter: @escaping (InMemoryOutputFile) throws -> InMemoryOutputFile = { try $0.swiftFormatted },
renderer: any RendererProtocol = TextBasedRenderer.default,
config: Config,
diagnostics: any DiagnosticCollector
) -> GeneratorPipeline {
Expand Down Expand Up @@ -161,9 +159,7 @@ func makeGeneratorPipeline(
diagnostics: diagnostics
)
},
postTransitionHooks: [
formatter
]
postTransitionHooks: []
)
)
}
Loading

0 comments on commit 3074113

Please sign in to comment.