Skip to content

Commit

Permalink
Use swift-log instead of print (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored May 23, 2022
1 parent b36097a commit 733f84b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.3.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/apple/swift-log.git", from: "1.4.0"),
],
targets: [
.target(
name: "SotoCodeGenerator",
dependencies: [
.byName(name: "SotoCodeGeneratorLib"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log")
]
),
.target(
Expand All @@ -27,7 +29,8 @@ 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: "SwiftFormat", package: "SwiftFormat"),
.product(name: "Logging", package: "swift-log")
]
),
.testTarget(
Expand Down
4 changes: 4 additions & 0 deletions Sources/SotoCodeGenerator/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import ArgumentParser
import Foundation
import Logging
import SotoCodeGeneratorLib

struct Command: ParsableCommand, SotoCodeGenCommand {
Expand Down Expand Up @@ -41,6 +42,9 @@ struct Command: ParsableCommand, SotoCodeGenCommand {
@Flag(name: .long, help: "Load smithy")
var smithy: Bool = false

@Option(name: .long, help: "Log Level (trace, debug, info, error)")
var logLevel: String?

static var rootPath: String {
return #file
.split(separator: "/", omittingEmptySubsequences: false)
Expand Down
43 changes: 24 additions & 19 deletions Sources/SotoCodeGeneratorLib/AwsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@

import Foundation
import HummingbirdMustache
import Logging
import SotoSmithy
import SotoSmithyAWS

struct AwsService {
var model: Model
var serviceName: String
var serviceEndpointPrefix: String
var serviceId: ShapeId
var service: ServiceShape
var serviceProtocolTrait: AwsServiceProtocol
var endpoints: Endpoints
var operations: [ShapeId: OperationShape]
var outputHTMLComments: Bool

init(_ model: SotoSmithy.Model, endpoints: Endpoints, outputHTMLComments: Bool) throws {
let model: Model
let serviceName: String
let serviceEndpointPrefix: String
let serviceId: ShapeId
let service: ServiceShape
let serviceProtocolTrait: AwsServiceProtocol
let endpoints: Endpoints
let operations: [ShapeId: OperationShape]
let outputHTMLComments: Bool
let logger: Logger

init(_ model: SotoSmithy.Model, endpoints: Endpoints, outputHTMLComments: Bool, logger: Logger) throws {
guard let service = model.select(type: SotoSmithy.ServiceShape.self).first else { throw Error(reason: "No service object") }

self.model = model
Expand All @@ -45,6 +47,7 @@ struct AwsService {

self.endpoints = endpoints
self.outputHTMLComments = outputHTMLComments
self.logger = logger
}

/// Return service name from API
Expand Down Expand Up @@ -566,15 +569,17 @@ struct AwsService {
func getPartitionEndpoints() -> [String: (endpoint: String, region: Region)] {
var partitionEndpoints: [String: (endpoint: String, region: Region)] = [:]
self.endpoints.partitions.forEach {
if let partitionEndpoint = $0.services[self.serviceEndpointPrefix]?.partitionEndpoint {
guard let service = $0.services[self.serviceEndpointPrefix],
let endpoint = service.endpoints[partitionEndpoint],
let region = endpoint.credentialScope?.region
else {
preconditionFailure("Found partition endpoint without a credential scope region")
}
partitionEndpoints[$0.partition] = (endpoint: partitionEndpoint, region: region)
guard let service = $0.services[self.serviceEndpointPrefix] else { return }
guard let partitionEndpoint = service.partitionEndpoint else { return }
guard let endpoint = service.endpoints[partitionEndpoint] else {
self.logger.error("Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) does not exist")
return
}
guard let region = endpoint.credentialScope?.region else {
self.logger.error("Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) has no credential scope region")
return
}
partitionEndpoints[$0.partition] = (endpoint: partitionEndpoint, region: region)
}
return partitionEndpoints
}
Expand Down
37 changes: 24 additions & 13 deletions Sources/SotoCodeGeneratorLib/SotoCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Foundation
import HummingbirdMustache
import Logging
import SotoSmithy
import SotoSmithyAWS
import SwiftFormat
Expand All @@ -27,6 +28,7 @@ public protocol SotoCodeGenCommand {
var swiftFormat: Bool { get }
var htmlComments: Bool { get }
var smithy: Bool { get }
var logLevel: String? { get }
}

public struct SotoCodeGen {
Expand All @@ -53,10 +55,14 @@ public struct SotoCodeGen {

let command: SotoCodeGenCommand
let library: HBMustacheLibrary
let logger: Logging.Logger

public init(command: SotoCodeGenCommand) throws {
self.command = command
self.library = try Templates.createLibrary()
var logger = Logging.Logger(label: "")
logger.logLevel = self.command.logLevel.map { Logging.Logger.Level(rawValue: $0) ?? .info } ?? .info
self.logger = logger
Smithy.registerAWSTraits()
Smithy.registerTraitTypes(
SotoInputShapeTrait.self,
Expand All @@ -83,21 +89,26 @@ public struct SotoCodeGen {
DispatchQueue.global().async {
defer { group.leave() }
do {
let service = try AwsService(model.value, endpoints: endpoints, outputHTMLComments: command.htmlComments)
let service = try AwsService(
model.value,
endpoints: endpoints,
outputHTMLComments: command.htmlComments,
logger: self.logger
)
if self.command.output {
try self.generateFiles(with: service)
}
} catch {
print("\(model.key): \(error)")
self.logger.error("\(model.key): \(error)")
exit(1)
}
}
}

group.wait()

print("Code Generation took \(Int(-startTime.timeIntervalSinceNow)) seconds")
print("Done.")
self.logger.info("Code Generation took \(Int(-startTime.timeIntervalSinceNow)) seconds")
self.logger.info("Done.")
}

func getModelFiles() -> [String] {
Expand Down Expand Up @@ -170,21 +181,21 @@ public struct SotoCodeGen {
if try self.format(api)
.writeIfChanged(toFile: "\(basePath)\(service.serviceName)_API.swift")
{
print("Wrote: \(service.serviceName)_API.swift")
self.logger.info("Wrote \(service.serviceName)_API.swift")
}
let apiAsync = self.library.render(apiContext, withTemplate: "api+async")!
if self.command.output, try self.format(apiAsync).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_API+async.swift"
) {
print("Wrote: \(service.serviceName)_API+async.swift")
self.logger.info("Wrote \(service.serviceName)_API+async.swift")
}

let shapesContext = try service.generateShapesContext()
let shapes = self.library.render(shapesContext, withTemplate: "shapes")!
if self.command.output, try self.format(shapes).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Shapes.swift"
) {
print("Wrote: \(service.serviceName)_Shapes.swift")
self.logger.info("Wrote \(service.serviceName)_Shapes.swift")
}

let errorContext = try service.generateErrorContext()
Expand All @@ -193,7 +204,7 @@ public struct SotoCodeGen {
if self.command.output, try self.format(errors).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Error.swift"
) {
print("Wrote: \(service.serviceName)_Error.swift")
self.logger.info("Wrote \(service.serviceName)_Error.swift")
}
}

Expand All @@ -203,13 +214,13 @@ public struct SotoCodeGen {
if self.command.output, try self.format(paginators).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Paginator.swift"
) {
print("Wrote: \(service.serviceName)_Paginator.swift")
self.logger.info("Wrote \(service.serviceName)_Paginator.swift")
}
let paginatorsAsync = self.library.render(paginatorContext, withTemplate: "paginator+async")!
if self.command.output, try self.format(paginatorsAsync).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Paginator+async.swift"
) {
print("Wrote: \(service.serviceName)_Paginator+async.swift")
self.logger.info("Wrote \(service.serviceName)_Paginator+async.swift")
}
}

Expand All @@ -219,16 +230,16 @@ public struct SotoCodeGen {
if self.command.output, try self.format(waiters).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Waiter.swift"
) {
print("Wrote: \(service.serviceName)_Waiter.swift")
self.logger.info("Wrote \(service.serviceName)_Waiter.swift")
}
let waitersAsync = self.library.render(waiterContexts, withTemplate: "waiter+async")!
if self.command.output, try self.format(waitersAsync).writeIfChanged(
toFile: "\(basePath)/\(service.serviceName)_Waiter+async.swift"
) {
print("Wrote: \(service.serviceName)_Waiter+async.swift")
self.logger.info("Wrote \(service.serviceName)_Waiter+async.swift")
}
}
// print("Succesfully Generated \(service.serviceName)")
self.logger.debug("Succesfully Generated \(service.serviceName)")
}
}

Expand Down

0 comments on commit 733f84b

Please sign in to comment.