Skip to content

Commit a9d7504

Browse files
author
Ed Paulosky
authored
feat: Improves configuring imports for codegen (#541)
* targets file based config branch for CRT * Adds param for spiName for imports * Updates ImportDeclarations to allow for the usage of _spiName * reverts pacakge * bumps package.swift * Asserts that only Internal spi name can be used currently * ktlint
1 parent 7b28da1 commit a9d7504

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
.library(name: "SmithyTestUtil", targets: ["SmithyTestUtil"])
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/awslabs/aws-crt-swift.git", .exact("0.6.1")),
16+
.package(url: "https://github.com/awslabs/aws-crt-swift.git", .exact("0.9.0")),
1717
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
1818
.package(url: "https://github.com/MaxDesiatov/XMLCoder.git", from: "0.13.0")
1919
],

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/ImportDeclarations.kt

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,31 @@ package software.amazon.smithy.swift.codegen
88
class ImportDeclarations {
99
private val imports = mutableSetOf<ImportStatement>()
1010

11-
fun addImport(packageName: String, isTestable: Boolean = false) {
12-
imports.add(ImportStatement(packageName, isTestable))
11+
fun addImport(packageName: String, isTestable: Boolean = false, internalSPIName: String? = null) {
12+
if (internalSPIName != null && internalSPIName != "Internal") {
13+
throw IllegalArgumentException(
14+
"""
15+
We currently only support usage of a single spiName 'Internal'.
16+
If you'd like to use another name then please update the logic below to fully support multiple spiNames.
17+
"""
18+
)
19+
}
20+
21+
val existingImport = imports.find { it.packageName == packageName }
22+
if (existingImport != null) {
23+
// If we have an existing import with the same package name, then replace the existing one
24+
val newImport = ImportStatement(
25+
packageName,
26+
isTestable || existingImport.isTestable,
27+
internalSPIName ?: existingImport.internalSPIName
28+
)
29+
imports.remove(existingImport)
30+
imports.add(newImport)
31+
return
32+
}
33+
34+
// Otherwise, we have a new import so add it
35+
imports.add(ImportStatement(packageName, isTestable, internalSPIName))
1336
}
1437

1538
override fun toString(): String {
@@ -18,19 +41,23 @@ class ImportDeclarations {
1841
}
1942

2043
return imports
44+
.sortedBy { it.packageName }
2145
.map(ImportStatement::statement)
22-
.sorted()
2346
.joinToString(separator = "\n")
2447
}
2548
}
2649

27-
private data class ImportStatement(val packageName: String, val isTestable: Boolean) {
50+
private data class ImportStatement(val packageName: String, val isTestable: Boolean, val internalSPIName: String?) {
2851
val statement: String
2952
get() {
53+
var import = "import $packageName"
54+
if (internalSPIName != null) {
55+
import = "@_spi($internalSPIName) $import"
56+
}
3057
if (isTestable) {
31-
return "@testable import $packageName"
58+
import = "@testable $import"
3259
}
33-
return "import $packageName"
60+
return import
3461
}
3562

3663
override fun toString(): String = statement

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,19 @@ class SwiftWriter(private val fullPackageName: String) : CodeWriter() {
9090
val staticHeader: String = "// Code generated by smithy-swift-codegen. DO NOT EDIT!\n\n"
9191
}
9292

93-
fun addImport(symbol: Symbol) {
94-
93+
fun addImport(symbol: Symbol, internalSPIName: String? = null) {
9594
if (symbol.isBuiltIn || symbol.isServiceNestedNamespace) return
9695
// always add dependencies
9796
dependencies.addAll(symbol.dependencies)
9897

9998
// only add imports for symbols that exist in a certain namespace
10099
if (symbol.namespace.isNotEmpty()) {
101-
imports.addImport(symbol.namespace, false)
100+
imports.addImport(symbol.namespace, false, internalSPIName)
102101
}
103102
}
104103

105-
fun addImport(packageName: String, isTestable: Boolean = false) {
106-
imports.addImport(packageName, isTestable)
104+
fun addImport(packageName: String, isTestable: Boolean = false, internalSPIName: String? = null) {
105+
imports.addImport(packageName, isTestable, internalSPIName)
107106
}
108107

109108
fun addFoundationImport() {

0 commit comments

Comments
 (0)