Skip to content

Commit b9a70e3

Browse files
authored
Merge pull request #3143 from xedin/adopt-edit-protocol-directly-for-package-manifest-refactorings
[SwiftRefactor] Switch package manifest refactoring actions to use `EditRefactoringProvider`
2 parents 9c2371a + 2cf5193 commit b9a70e3

File tree

10 files changed

+57
-394
lines changed

10 files changed

+57
-394
lines changed

Sources/SwiftRefactor/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ add_swift_syntax_library(SwiftRefactor
3030
PackageManifest/AddSwiftSetting.swift
3131
PackageManifest/AddTargetDependency.swift
3232
PackageManifest/ManifestEditError.swift
33-
PackageManifest/ManifestEditRefactoringProvider.swift
3433
PackageManifest/ManifestSyntaxRepresentable.swift
3534
PackageManifest/PackageDependency.swift
36-
PackageManifest/PackageEdit.swift
3735
PackageManifest/PackageTarget.swift
3836
PackageManifest/ProductDescription.swift
3937
PackageManifest/StringUtils.swift

Sources/SwiftRefactor/PackageManifest/AddPackageDependency.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616

1717
/// Add a package dependency to a package manifest's source code.
1818
@_spi(PackageRefactor)
19-
public struct AddPackageDependency: ManifestEditRefactoringProvider {
19+
public struct AddPackageDependency: EditRefactoringProvider {
2020
public struct Context {
2121
public var dependency: PackageDependency
2222

@@ -36,10 +36,10 @@ public struct AddPackageDependency: ManifestEditRefactoringProvider {
3636

3737
/// Produce the set of source edits needed to add the given package
3838
/// dependency to the given manifest file.
39-
public static func manifestRefactor(
39+
public static func textRefactor(
4040
syntax manifest: SourceFileSyntax,
4141
in context: Context
42-
) throws -> PackageEdit {
42+
) throws -> [SourceEdit] {
4343
let dependency = context.dependency
4444
guard let packageCall = manifest.findCall(calleeName: "Package") else {
4545
throw ManifestEditError.cannotFindPackage
@@ -51,19 +51,17 @@ public struct AddPackageDependency: ManifestEditRefactoringProvider {
5151
in: packageCall
5252
)
5353
else {
54-
return PackageEdit(manifestEdits: [])
54+
return []
5555
}
5656

5757
let newPackageCall = try addPackageDependencyLocal(
5858
dependency,
5959
to: packageCall
6060
)
6161

62-
return PackageEdit(
63-
manifestEdits: [
64-
.replace(packageCall, with: newPackageCall.description)
65-
]
66-
)
62+
return [
63+
.replace(packageCall, with: newPackageCall.description)
64+
]
6765
}
6866

6967
/// Return `true` if the dependency already exists in the manifest, otherwise return `false`.

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 6 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616

1717
/// Add a target to a manifest's source code.
1818
@_spi(PackageRefactor)
19-
public struct AddPackageTarget: ManifestEditRefactoringProvider {
19+
public struct AddPackageTarget: EditRefactoringProvider {
2020
public struct Context {
2121
public let target: PackageTarget
2222
public var testHarness: TestHarness
@@ -57,10 +57,10 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
5757
/// Add the given target to the manifest, producing a set of edit results
5858
/// that updates the manifest and adds some source files to stub out the
5959
/// new target.
60-
public static func manifestRefactor(
60+
public static func textRefactor(
6161
syntax manifest: SourceFileSyntax,
6262
in context: Context
63-
) throws -> PackageEdit {
63+
) throws -> [SourceEdit] {
6464
guard let packageCall = manifest.findCall(calleeName: "Package") else {
6565
throw ManifestEditError.cannotFindPackage
6666
}
@@ -86,44 +86,10 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
8686
newElement: target.asSyntax()
8787
)
8888

89-
let outerDirectory: String?
90-
switch target.type {
91-
case .binary, .plugin, .system: outerDirectory = nil
92-
case .executable, .library, .macro: outerDirectory = "Sources"
93-
case .test: outerDirectory = "Tests"
94-
}
95-
96-
guard let outerDirectory else {
97-
return PackageEdit(
98-
manifestEdits: [
99-
.replace(packageCall, with: newPackageCall.description)
100-
]
101-
)
102-
}
103-
104-
let outerPath = outerDirectory
105-
106-
/// The set of auxiliary files this refactoring will create.
107-
var auxiliaryFiles: AuxiliaryFiles = []
108-
109-
// Add the primary source file. Every target type has this.
110-
addPrimarySourceFile(
111-
outerPath: outerPath,
112-
target: target,
113-
in: context,
114-
to: &auxiliaryFiles
115-
)
116-
11789
// Perform any other actions that are needed for this target type.
11890
var extraManifestEdits: [SourceEdit] = []
11991
switch target.type {
12092
case .macro:
121-
addProvidedMacrosSourceFile(
122-
outerPath: outerPath,
123-
target: target,
124-
to: &auxiliaryFiles
125-
)
126-
12793
if !manifest.containsStringLiteral("swift-syntax") {
12894
newPackageCall =
12995
try AddPackageDependency
@@ -153,159 +119,9 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
153119
default: break
154120
}
155121

156-
return PackageEdit(
157-
manifestEdits: [
158-
.replace(packageCall, with: newPackageCall.description)
159-
] + extraManifestEdits,
160-
auxiliaryFiles: auxiliaryFiles
161-
)
162-
}
163-
164-
/// Add the primary source file for a target to the list of auxiliary
165-
/// source files.
166-
fileprivate static func addPrimarySourceFile(
167-
outerPath: String,
168-
target: PackageTarget,
169-
in context: Context,
170-
to auxiliaryFiles: inout AuxiliaryFiles
171-
) {
172-
let sourceFilePath = "\(outerPath)/\(target.name)/\(target.name).swift"
173-
174-
// Introduce imports for each of the dependencies that were specified.
175-
var importModuleNames = target.dependencies.map {
176-
$0.name
177-
}
178-
179-
// Add appropriate test module dependencies.
180-
if target.type == .test {
181-
switch context.testHarness {
182-
case .none:
183-
break
184-
185-
case .xctest:
186-
importModuleNames.append("XCTest")
187-
188-
case .swiftTesting:
189-
importModuleNames.append("Testing")
190-
}
191-
}
192-
193-
let importDecls = importModuleNames.lazy.sorted().map { name in
194-
DeclSyntax("import \(raw: name)\n")
195-
}
196-
197-
let imports = CodeBlockItemListSyntax {
198-
for importDecl in importDecls {
199-
importDecl
200-
}
201-
}
202-
203-
let sourceFileText: SourceFileSyntax
204-
switch target.type {
205-
case .binary, .plugin, .system:
206-
fatalError("should have exited above")
207-
208-
case .macro:
209-
sourceFileText = """
210-
\(imports)
211-
struct \(raw: target.sanitizedName): Macro {
212-
/// TODO: Implement one or more of the protocols that inherit
213-
/// from Macro. The appropriate macro protocol is determined
214-
/// by the "macro" declaration that \(raw: target.sanitizedName) implements.
215-
/// Examples include:
216-
/// @freestanding(expression) macro --> ExpressionMacro
217-
/// @attached(member) macro --> MemberMacro
218-
}
219-
"""
220-
221-
case .test:
222-
switch context.testHarness {
223-
case .none:
224-
sourceFileText = """
225-
\(imports)
226-
// Test code here
227-
"""
228-
229-
case .xctest:
230-
sourceFileText = """
231-
\(imports)
232-
class \(raw: target.sanitizedName)Tests: XCTestCase {
233-
func test\(raw: target.sanitizedName)() {
234-
XCTAssertEqual(42, 17 + 25)
235-
}
236-
}
237-
"""
238-
239-
case .swiftTesting:
240-
sourceFileText = """
241-
\(imports)
242-
@Suite
243-
struct \(raw: target.sanitizedName)Tests {
244-
@Test("\(raw: target.sanitizedName) tests")
245-
func example() {
246-
#expect(42 == 17 + 25)
247-
}
248-
}
249-
"""
250-
}
251-
252-
case .library:
253-
sourceFileText = """
254-
\(imports)
255-
"""
256-
257-
case .executable:
258-
sourceFileText = """
259-
\(imports)
260-
@main
261-
struct \(raw: target.sanitizedName)Main {
262-
static func main() {
263-
print("Hello, world")
264-
}
265-
}
266-
"""
267-
}
268-
269-
auxiliaryFiles.addSourceFile(
270-
path: sourceFilePath,
271-
sourceCode: sourceFileText
272-
)
273-
}
274-
275-
/// Add a file that introduces the main entrypoint and provided macros
276-
/// for a macro target.
277-
fileprivate static func addProvidedMacrosSourceFile(
278-
outerPath: String,
279-
target: PackageTarget,
280-
to auxiliaryFiles: inout AuxiliaryFiles
281-
) {
282-
auxiliaryFiles.addSourceFile(
283-
path: "\(outerPath)/\(target.name)/ProvidedMacros.swift",
284-
sourceCode: """
285-
import SwiftCompilerPlugin
286-
287-
@main
288-
struct \(raw: target.sanitizedName)Macros: CompilerPlugin {
289-
let providingMacros: [Macro.Type] = [
290-
\(raw: target.sanitizedName).self,
291-
]
292-
}
293-
"""
294-
)
295-
}
296-
}
297-
298-
/// The array of auxiliary files that can be added by a package editing
299-
/// operation.
300-
private typealias AuxiliaryFiles = [(String, SourceFileSyntax)]
301-
302-
fileprivate extension AuxiliaryFiles {
303-
/// Add a source file to the list of auxiliary files.
304-
mutating func addSourceFile(
305-
path: String,
306-
sourceCode: SourceFileSyntax
307-
) {
308-
self.append((path, sourceCode))
122+
return [
123+
.replace(packageCall, with: newPackageCall.description)
124+
] + extraManifestEdits
309125
}
310126
}
311127

Sources/SwiftRefactor/PackageManifest/AddPluginUsage.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SwiftSyntaxBuilder
1717
/// Add a plugin usage to a particular target in the manifest's source
1818
/// code.
1919
@_spi(PackageRefactor)
20-
public struct AddPluginUsage: ManifestEditRefactoringProvider {
20+
public struct AddPluginUsage: EditRefactoringProvider {
2121
public struct Context {
2222
public let targetName: String
2323
public let pluginUsage: PackageTarget.PluginUsage
@@ -30,10 +30,10 @@ public struct AddPluginUsage: ManifestEditRefactoringProvider {
3030

3131
/// Produce the set of source edits needed to add the given package
3232
/// dependency to the given manifest file.
33-
public static func manifestRefactor(
33+
public static func textRefactor(
3434
syntax manifest: SourceFileSyntax,
3535
in context: Context
36-
) throws -> PackageEdit {
36+
) throws -> [SourceEdit] {
3737
let targetName = context.targetName
3838
let pluginUsage = context.pluginUsage
3939

@@ -50,10 +50,8 @@ public struct AddPluginUsage: ManifestEditRefactoringProvider {
5050
newElement: pluginUsage.asSyntax()
5151
)
5252

53-
return PackageEdit(
54-
manifestEdits: [
55-
.replace(targetCall, with: newTargetCall.description)
56-
]
57-
)
53+
return [
54+
.replace(targetCall, with: newTargetCall.description)
55+
]
5856
}
5957
}

Sources/SwiftRefactor/PackageManifest/AddProduct.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616

1717
/// Add a product to the manifest's source code.
1818
@_spi(PackageRefactor)
19-
public struct AddProduct: ManifestEditRefactoringProvider {
19+
public struct AddProduct: EditRefactoringProvider {
2020
public struct Context {
2121
public let product: ProductDescription
2222

@@ -36,10 +36,10 @@ public struct AddProduct: ManifestEditRefactoringProvider {
3636

3737
/// Produce the set of source edits needed to add the given package
3838
/// dependency to the given manifest file.
39-
public static func manifestRefactor(
39+
public static func textRefactor(
4040
syntax manifest: SourceFileSyntax,
4141
in context: Context
42-
) throws -> PackageEdit {
42+
) throws -> [SourceEdit] {
4343
let product = context.product
4444

4545
guard let packageCall = manifest.findCall(calleeName: "Package") else {
@@ -52,10 +52,8 @@ public struct AddProduct: ManifestEditRefactoringProvider {
5252
newElement: product.asSyntax()
5353
)
5454

55-
return PackageEdit(
56-
manifestEdits: [
57-
.replace(packageCall, with: newPackageCall.description)
58-
]
59-
)
55+
return [
56+
.replace(packageCall, with: newPackageCall.description)
57+
]
6058
}
6159
}

0 commit comments

Comments
 (0)