Skip to content

Commit

Permalink
fix: Make manifest Swift 6 Language Mode compatible (#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins authored Sep 24, 2024
1 parent f20ede5 commit 5b63d6c
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 450 deletions.
3 changes: 2 additions & 1 deletion AWSSDKSwiftCLI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ let package = Package(
.product(name: "Algorithms", package: "swift-algorithms"),
],
resources: [
.process("Resources/Package.Base.swift"),
.process("Resources/Package.Prefix.txt"),
.process("Resources/Package.Base.txt"),
.process("Resources/DocIndex.Base.md")
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ struct GeneratePackageManifestCommand: ParsableCommand {
@Option(help: "The names of the services to include in the package manifest. This defaults to all services located in aws-sdk-swift/Sources/Services")
var services: [String] = []

@Flag(help: "If the package manifest should exclude AWS services.")
var excludeAWSServices = false

@Flag(help: "If the package manifest should exclude runtime tests.")
var excludeRuntimeTests = false

Expand All @@ -45,7 +42,6 @@ struct GeneratePackageManifestCommand: ParsableCommand {
clientRuntimeVersion: clientRuntimeVersion,
crtVersion: crtVersion,
services: services.isEmpty ? nil : services,
excludeAWSServices: excludeAWSServices,
excludeRuntimeTests: excludeRuntimeTests
)
try generatePackageManifest.run()
Expand All @@ -69,8 +65,6 @@ struct GeneratePackageManifest {
/// The list of services to include as products
/// If `nil` then the list is populated with the names of all items within the `Sources/Services` directory
let services: [String]?
/// If the package manifest should exclude the AWS services.
let excludeAWSServices: Bool
/// If the package manifest should exclude runtime unit tests.
let excludeRuntimeTests: Bool

Expand Down Expand Up @@ -213,14 +207,12 @@ extension GeneratePackageManifest {
clientRuntimeVersion: clientRuntimeVersion,
crtVersion: crtVersion,
services: services,
excludeAWSServices: excludeAWSServices,
excludeRuntimeTests: excludeRuntimeTests
) { _clientRuntimeVersion, _crtVersion, _services in
let builder = PackageManifestBuilder(
clientRuntimeVersion: _clientRuntimeVersion,
crtVersion: _crtVersion,
services: _services,
excludeAWSServices: excludeAWSServices,
excludeRuntimeTests: excludeRuntimeTests
)
return try builder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,69 @@ struct PackageManifestBuilder {
let clientRuntimeVersion: Version
let crtVersion: Version
let services: [Service]
let excludeAWSServices: Bool
let excludeRuntimeTests: Bool
let prefixContents: () throws -> String
let basePackageContents: () throws -> String

init(
clientRuntimeVersion: Version,
crtVersion: Version,
services: [Service],
excludeAWSServices: Bool,
excludeRuntimeTests: Bool,
prefixContents: @escaping () throws -> String,
basePackageContents: @escaping () throws -> String
) {
self.clientRuntimeVersion = clientRuntimeVersion
self.crtVersion = crtVersion
self.services = services
self.excludeAWSServices = excludeAWSServices
self.basePackageContents = basePackageContents
self.excludeRuntimeTests = excludeRuntimeTests
self.prefixContents = prefixContents
self.basePackageContents = basePackageContents
}

init(
clientRuntimeVersion: Version,
crtVersion: Version,
services: [Service],
excludeAWSServices: Bool,
excludeRuntimeTests: Bool
) {
self.init(clientRuntimeVersion: clientRuntimeVersion, crtVersion: crtVersion, services: services, excludeAWSServices: excludeAWSServices, excludeRuntimeTests: excludeRuntimeTests) {
// Returns the contents of the base package manifest stored in the bundle at `Resources/Package.Base.swift`
let basePackageName = "Package.Base"

// Get the url for the base package manifest that is stored in the bundle
guard let url = Bundle.module.url(forResource: basePackageName, withExtension: "swift") else {
throw Error("Could not find \(basePackageName).swift in bundle")
self.init(
clientRuntimeVersion: clientRuntimeVersion,
crtVersion: crtVersion,
services: services,
excludeRuntimeTests: excludeRuntimeTests,
prefixContents: Self.contentReader(filename: "Package.Prefix"),
basePackageContents: Self.contentReader(filename: "Package.Base")
)
}

static func contentReader(filename: String) -> () throws -> String {
return {
// Get the url for the file that is stored in the bundle
guard let url = Bundle.module.url(forResource: filename, withExtension: "txt") else {
throw Error("Could not find \(filename).txt in bundle")
}

// Load the contents of the base package manifest
let fileContents = try FileManager.default.loadContents(atPath: url.path)

// Convert the base package manifest data to a string
guard let fileText = String(data: fileContents, encoding: .utf8) else {
throw Error("Failed to create string from contents of file \(basePackageName).swift")
throw Error("Failed to create string from contents of file \(filename).txt")
}

return fileText
}
}

// MARK: - Build

/// Builds the contents of the package manifest file.
func build() throws-> String {
let contents = try [
prefixContents(),
buildGeneratedContent(),
basePackageContents(),
"",
buildGeneratedContent()
]
return contents.joined(separator: .newline)
}
Expand All @@ -92,8 +99,6 @@ struct PackageManifestBuilder {
// Add the generated content that defines the list of services to include
buildServiceTargets(),
"",
buildResolvedServices(),
"\n"
]
return contents.joined(separator: .newline)
}
Expand All @@ -102,25 +107,20 @@ struct PackageManifestBuilder {
///
/// - Returns: A pragma mark comment to provide separation between the non-generated (base) and generated content
private func buildPragmaMark() -> String {
"// MARK: - Generated"
"// MARK: - Dynamic Content"
}


/// Builds the dependencies versions
private func buildDependencies() -> String {
"""
addDependencies(
clientRuntimeVersion: \(clientRuntimeVersion.description.wrappedInQuotes()),
crtVersion: \(crtVersion.description.wrappedInQuotes())
)
let clientRuntimeVersion: Version = \(clientRuntimeVersion.description.wrappedInQuotes())
let crtVersion: Version = \(crtVersion.description.wrappedInQuotes())
"""
}

private func buildRuntimeTests() -> String {
return [
"// Uncomment this line to exclude runtime unit tests",
(excludeRuntimeTests ? "" : "// ") + "excludeRuntimeUnitTests()"
].joined(separator: .newline)
"let excludeRuntimeUnitTests = \(excludeRuntimeTests)"
}

/// Builds the list of services to include.
Expand All @@ -131,14 +131,6 @@ struct PackageManifestBuilder {
lines += ["let serviceTargets: [String] = ["]
lines += services.map { " \($0.name.wrappedInQuotes())," }
lines += ["]"]
lines += [""]
lines += ["// Uncomment this line to enable all services"]
lines += ["\(excludeAWSServices ? "// " : "")addAllServices()"]

return lines.joined(separator: .newline)
}

private func buildResolvedServices() -> String {
"addResolvedTargets()"
}
}
Loading

0 comments on commit 5b63d6c

Please sign in to comment.