Skip to content

Commit

Permalink
Remove swift format dependency (#71)
Browse files Browse the repository at this point in the history
* Changes to templates so we can remove swiftFormat

* More changes

* Comment whitespace

* CodingKeys default rawValue

* Spaces in closure

* Spaces betwen shapes

* Empty init() {}

* Default values for CodingKeys enum

* Cleared up spaces in structs

* Enum default rawValues

* Remove SwiftFormat

* Hummingbird 1.0.3

* comments, TEXT encoding

* Copyright 2023

* Copyright 2023 again

* Fix paginator comments

* Don't bother with default rawValues for enums

* Clean up dictionary coder output

* copyright year
  • Loading branch information
adam-fowler authored Jan 16, 2023
1 parent 5aacf40 commit 8fad5a4
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 142 deletions.
4 changes: 1 addition & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/soto-project/soto-smithy.git", from: "0.3.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat.git", from: "0.48.17"),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.3"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
],
targets: [
Expand All @@ -29,7 +28,6 @@ let package = Package(
.product(name: "SotoSmithy", package: "soto-smithy"),
.product(name: "SotoSmithyAWS", package: "soto-smithy"),
.product(name: "HummingbirdMustache", package: "hummingbird-mustache"),
.product(name: "SwiftFormat", package: "SwiftFormat"),
.product(name: "Logging", package: "swift-log")
]
),
Expand Down
4 changes: 1 addition & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/soto-project/soto-smithy.git", from: "0.3.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat.git", .exact("0.48.17")),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.3"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
],
targets: [
Expand All @@ -29,7 +28,6 @@ let package = Package(
.product(name: "SotoSmithy", package: "soto-smithy"),
.product(name: "SotoSmithyAWS", package: "soto-smithy"),
.product(name: "HummingbirdMustache", package: "hummingbird-mustache"),
.product(name: "SwiftFormat", package: "SwiftFormat"),
.product(name: "Logging", package: "swift-log")
]
),
Expand Down
4 changes: 1 addition & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/soto-project/soto-smithy.git", from: "0.3.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat.git", .exact("0.48.17")),
.package(url: "https://github.com/hummingbird-project/hummingbird-mustache.git", from: "1.0.3"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
],
targets: [
Expand All @@ -29,7 +28,6 @@ let package = Package(
.product(name: "SotoSmithy", package: "soto-smithy"),
.product(name: "SotoSmithyAWS", package: "soto-smithy"),
.product(name: "HummingbirdMustache", package: "hummingbird-mustache"),
.product(name: "SwiftFormat", package: "SwiftFormat"),
.product(name: "Logging", package: "swift-log")
]
),
Expand Down
3 changes: 0 additions & 3 deletions Sources/SotoCodeGenerator/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ struct Command: ParsableCommand, SotoCodeGenCommand {
@Flag(name: .long, inversion: .prefixedNo, help: "Output files")
var output: Bool = true

@Flag(name: [.customShort("f"), .customLong("format")], inversion: .prefixedNo, help: "Run swift format on output")
var swiftFormat: Bool = false

@Flag(name: .long, help: "HTML comments")
var htmlComments: Bool = false

Expand Down
17 changes: 11 additions & 6 deletions Sources/SotoCodeGeneratorLib/AwsService+shapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2022 the Soto project authors
// Copyright (c) 2017-2023 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -75,7 +75,7 @@ extension AwsService {
if key.allLetterIsNumeric() {
key = "\(shapeName.toSwiftVariableCase())\(key)"
}
valueContexts.append(EnumMemberContext(case: key, documentation: processDocs(value.documentation), string: value.value))
valueContexts.append(EnumMemberContext(case: key, documentation: processDocs(value.documentation), rawValue: value.value))
}
return EnumContext(
name: shapeName.toSwiftClassCase(),
Expand Down Expand Up @@ -113,7 +113,7 @@ extension AwsService {
return EnumMemberContext(
case: key,
documentation: documentation.map { processDocs($0.value) } ?? [],
string: value
rawValue: value
)
}
return EnumContext(
Expand Down Expand Up @@ -370,11 +370,16 @@ extension AwsService {
else {
return nil
}
var codingKey: String = name
var rawValue: String = name
if let aliasTrait = member.traits?.first(where: { $0 is AliasTrait }) as? AliasTrait {
codingKey = aliasTrait.alias
rawValue = aliasTrait.alias
}
return CodingKeysContext(variable: name.toSwiftVariableCase(), codingKey: codingKey, duplicate: false)
let variable = name.toSwiftVariableCase()
return CodingKeysContext(
variable: variable,
rawValue: rawValue,
duplicate: false
)
}

/// Generate array/dictionary encoding contexts
Expand Down
16 changes: 9 additions & 7 deletions Sources/SotoCodeGeneratorLib/AwsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2022 the Soto project authors
// Copyright (c) 2017-2023 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -300,8 +300,8 @@ struct AwsService {
}

/// process documenation string
func processDocs(from shape: Shape) -> [String.SubSequence] {
var docs: [String.SubSequence]
func processDocs(from shape: Shape) -> [Substring] {
var docs: [Substring]

let documentation = shape.trait(type: DocumentationTrait.self)?.value
if self.outputHTMLComments {
Expand All @@ -311,7 +311,8 @@ struct AwsService {
.tagStriped()
.replacingOccurrences(of: "\n +", with: " ", options: .regularExpression, range: nil)
.split(separator: "\n")
.compactMap { $0.isEmpty ? nil : $0 } ?? []
.compactMap { $0.isEmpty ? nil : $0 }
.map { $0.dropLast(while: { $0.isWhitespace }) } ?? []
}

if let externalDocumentation = shape.trait(type: ExternalDocumentationTrait.self)?.value {
Expand All @@ -323,7 +324,7 @@ struct AwsService {
}

/// process documenation string
func processMemberDocs(from shape: MemberShape) -> [String.SubSequence] {
func processMemberDocs(from shape: MemberShape) -> [Substring] {
guard var documentation = shape.trait(type: DocumentationTrait.self)?.value else { return [] }
if let recommendation = shape.trait(type: RecommendedTrait.self)?.reason {
documentation += "\n\(recommendation)"
Expand All @@ -333,6 +334,7 @@ struct AwsService {
.replacingOccurrences(of: "\n +", with: " ", options: .regularExpression, range: nil)
.split(separator: "\n")
.compactMap { $0.isEmpty ? nil : $0 }
.map { $0.dropLast(while: { $0.isWhitespace }) }
}

/// process documentation string
Expand Down Expand Up @@ -672,7 +674,7 @@ extension AwsService {
struct EnumMemberContext {
let `case`: String
let documentation: [String.SubSequence]
let string: String
let rawValue: String
}

struct ArrayEncodingPropertiesContext: EncodingPropertiesContext {
Expand Down Expand Up @@ -752,7 +754,7 @@ extension AwsService {

struct CodingKeysContext {
let variable: String
let codingKey: String
let rawValue: String
var duplicate: Bool
}

Expand Down
33 changes: 3 additions & 30 deletions Sources/SotoCodeGeneratorLib/SotoCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import HummingbirdMustache
import Logging
import SotoSmithy
import SotoSmithyAWS
import SwiftFormat

public protocol SotoCodeGenCommand {
var inputFile: String? { get }
Expand All @@ -28,7 +27,6 @@ public protocol SotoCodeGenCommand {
var endpoints: String { get }
var module: String? { get }
var output: Bool { get }
var swiftFormat: Bool { get }
var htmlComments: Bool { get }
var smithy: Bool { get }
var logLevel: String? { get }
Expand All @@ -40,22 +38,6 @@ public struct SotoCodeGen {
let error: Error
}

enum SwiftFormatConfig {
static let disabledRules = FormatRules.disabledByDefault + ["redundantReturn", "redundantBackticks", "trailingCommas", "extensionAccessControl"]
static let ruleNames = Set(FormatRules.byName.keys).subtracting(disabledRules)
static let rules: [FormatRule] = ruleNames.map { FormatRules.byName[$0]! }
static let formatOptions = FormatOptions(
ifdefIndent: .noIndent,
wrapArguments: .beforeFirst,
wrapParameters: .beforeFirst,
wrapCollections: .beforeFirst,
hoistPatternLet: false,
stripUnusedArguments: .unnamedOnly,
explicitSelf: .insert,
noSpaceOperators: ["...", "..<"]
)
}

let command: SotoCodeGenCommand
let library: HBMustacheLibrary
let logger: Logging.Logger
Expand Down Expand Up @@ -196,15 +178,6 @@ public struct SotoCodeGen {
}) { left, _ in left }
}

/// Run swift format on String
func format(_ string: String) throws -> String {
if self.command.swiftFormat {
return try SwiftFormat.format(string, rules: Self.SwiftFormatConfig.rules, options: Self.SwiftFormatConfig.formatOptions)
} else {
return string
}
}

/// Generate service files from AWSService
/// - Parameter codeGenerator: service generated from JSON
func generateFiles(with service: AwsService, config: ConfigFile) throws {
Expand Down Expand Up @@ -232,13 +205,13 @@ public struct SotoCodeGen {
}

let api = self.library.render(apiContext, withTemplate: "api")!
if try self.format(api)
if try api
.writeIfChanged(toFile: "\(basePath)/\(prefix)_api.swift")
{
self.logger.info("Wrote \(prefix)_api.swift")
}
let apiAsync = self.library.render(apiContext, withTemplate: "api_async")!
if self.command.output, try self.format(apiAsync).writeIfChanged(
if self.command.output, try apiAsync.writeIfChanged(
toFile: "\(basePath)/\(prefix)_api+async.swift"
) {
self.logger.info("Wrote \(prefix)_api+async.swift")
Expand All @@ -252,7 +225,7 @@ public struct SotoCodeGen {
}

let shapes = self.library.render(shapesContext, withTemplate: "shapes")!
if self.command.output, try self.format(shapes).writeIfChanged(
if self.command.output, try shapes.writeIfChanged(
toFile: "\(basePath)/\(prefix)_shapes.swift"
) {
self.logger.info("Wrote \(prefix)_shapes.swift")
Expand Down
13 changes: 13 additions & 0 deletions Sources/SotoCodeGeneratorLib/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ extension StringProtocol {
return true
}

func dropLast(while: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence {
var position = self.endIndex
var count = 0
while position != self.startIndex {
position = self.index(before: position)
if try !`while`(self[position]) {
break
}
count += 1
}
return self.dropLast(count)
}

private func lowerFirst() -> String {
return String(self[startIndex]).lowercased() + self[index(after: startIndex)...]
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/SotoCodeGeneratorLib/Templates/Templates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2021 the Soto project authors
// Copyright (c) 2017-2023 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -18,6 +18,7 @@ enum Templates {
static var values: [String: String] = [
"api": apiTemplate,
"api_async": apiAsyncTemplate,
"comment": commentTemplate,
"enum": enumTemplate,
"enumWithValues": enumWithValuesTemplate,
"errors": errorTemplate,
Expand Down
16 changes: 8 additions & 8 deletions Sources/SotoCodeGeneratorLib/Templates/api+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2021 the Soto project authors
// Copyright (c) 2017-2023 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -23,18 +23,17 @@ extension Templates {
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension {{ name }} {
// MARK: Async API Calls
{{#operations}}
{{#comment}}
/// {{.}}
{{>comment}}
{{/comment}}
{{#documentationUrl}}
/// {{.}}
{{/documentationUrl}}
{{#deprecated}}
@available(*, deprecated, message:"{{.}}")
@available(*, deprecated, message: "{{.}}")
{{/deprecated}}
{{scope}} func {{funcName}}({{#inputShape}}_ input: {{.}}, {{/inputShape}}logger: {{logger}} = AWSClient.loggingDisabled, on eventLoop: EventLoop? = nil) async throws{{#outputShape}} -> {{.}}{{/outputShape}} {
return try await self.client.execute(operation: "{{name}}", path: "{{path}}", httpMethod: .{{httpMethod}}, serviceConfig: self.config{{#inputShape}}, input: input{{/inputShape}}{{#endpointRequired}}, endpointDiscovery: .init(storage: self.endpointStorage, discover: self.getEndpoint, required: {{required}}){{/endpointRequired}}{{#hostPrefix}}, hostPrefix: "{{{.}}}"{{/hostPrefix}}, logger: logger, on: eventLoop)
Expand All @@ -46,29 +45,30 @@ extension Templates {
{{#streamingOperations}}
{{#comment}}
/// {{.}}
{{>comment}}
{{/comment}}
{{#documentationUrl}}
/// {{.}}
{{/documentationUrl}}
{{#deprecated}}
@available(*, deprecated, message:"{{.}}")
@available(*, deprecated, message: "{{.}}")
{{/deprecated}}
{{scope}} func {{funcName}}Streaming({{#inputShape}}_ input: {{.}}, {{/inputShape}}logger: {{logger}} = AWSClient.loggingDisabled, on eventLoop: EventLoop? = nil, _ stream: @escaping ({{streaming}}, EventLoop) -> EventLoopFuture<Void>) async throws{{#outputShape}} -> {{.}}{{/outputShape}} {
return try await self.client.execute(operation: "{{name}}", path: "{{path}}", httpMethod: .{{httpMethod}}, serviceConfig: self.config{{#inputShape}}, input: input{{/inputShape}}{{#endpointRequired}}, endpointDiscovery: .init(storage: self.endpointStorage, discover: self.getEndpoint, required: {{required}}){{/endpointRequired}}{{#hostPrefix}}, hostPrefix: "{{{.}}}"{{/hostPrefix}}, logger: logger, on: eventLoop, stream: stream)
}
{{/streamingOperations}}
{{/first(streamingOperations)}}
}
{{#paginators}}
{{>paginators_async}}
{{/paginators}}
{{#waiters}}
{{>waiters_async}}
{{/waiters}}
#endif // compiler(>=5.5.2) && canImport(_Concurrency)
"""
}
Loading

0 comments on commit 8fad5a4

Please sign in to comment.