Skip to content

Commit f35ea82

Browse files
committed
[SwiftRefactor] PackageManifest: Remove handling of auxiliary files
This is now handled by the Swift Package Manager itself and refactoring actions are only responsible for package manifest updates.
1 parent 9c2371a commit f35ea82

File tree

3 files changed

+3
-279
lines changed

3 files changed

+3
-279
lines changed

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 1 addition & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -156,157 +122,9 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
156122
return PackageEdit(
157123
manifestEdits: [
158124
.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
125+
] + extraManifestEdits
272126
)
273127
}
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))
309-
}
310128
}
311129

312130
/// The set of dependencies we need to introduce to a newly-created macro

Sources/SwiftRefactor/PackageManifest/PackageEdit.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,4 @@ import SwiftSyntax
1818
public struct PackageEdit {
1919
/// Edits to perform to the package manifest.
2020
public var manifestEdits: [SourceEdit] = []
21-
22-
/// Auxiliary files to write.
23-
public var auxiliaryFiles: [(relativePath: String, contents: SourceFileSyntax)] = []
2421
}

Tests/SwiftRefactorTest/ManifestEditTests.swift

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,6 @@ final class ManifestEditTests: XCTestCase {
442442
]
443443
)
444444
""",
445-
expectedAuxiliarySources: [
446-
"Sources/MyLib/MyLib.swift": """
447-
448-
"""
449-
],
450445
provider: AddPackageTarget.self,
451446
context: .init(
452447
target: PackageTarget(name: "MyLib")
@@ -478,14 +473,6 @@ final class ManifestEditTests: XCTestCase {
478473
]
479474
)
480475
""",
481-
expectedAuxiliarySources: [
482-
"Sources/MyLib/MyLib.swift": """
483-
import OtherLib
484-
import SwiftSyntax
485-
import TargetLib
486-
487-
"""
488-
],
489476
provider: AddPackageTarget.self,
490477
context: .init(
491478
target: PackageTarget(
@@ -530,20 +517,6 @@ final class ManifestEditTests: XCTestCase {
530517
]
531518
)
532519
""",
533-
expectedAuxiliarySources: [
534-
"Sources/MyProgram target-name/MyProgram target-name.swift": """
535-
import MyLib
536-
import SwiftSyntax
537-
import TargetLib
538-
539-
@main
540-
struct MyProgram_target_nameMain {
541-
static func main() {
542-
print("Hello, world")
543-
}
544-
}
545-
"""
546-
],
547520
provider: AddPackageTarget.self,
548521
context: .init(
549522
target: PackageTarget(
@@ -590,31 +563,6 @@ final class ManifestEditTests: XCTestCase {
590563
]
591564
)
592565
""",
593-
expectedAuxiliarySources: [
594-
"Sources/MyMacro target-name/MyMacro target-name.swift": """
595-
import SwiftCompilerPlugin
596-
import SwiftSyntaxMacros
597-
598-
struct MyMacro_target_name: Macro {
599-
/// TODO: Implement one or more of the protocols that inherit
600-
/// from Macro. The appropriate macro protocol is determined
601-
/// by the "macro" declaration that MyMacro_target_name implements.
602-
/// Examples include:
603-
/// @freestanding(expression) macro --> ExpressionMacro
604-
/// @attached(member) macro --> MemberMacro
605-
}
606-
""",
607-
"Sources/MyMacro target-name/ProvidedMacros.swift": """
608-
import SwiftCompilerPlugin
609-
610-
@main
611-
struct MyMacro_target_nameMacros: CompilerPlugin {
612-
let providingMacros: [Macro.Type] = [
613-
MyMacro_target_name.self,
614-
]
615-
}
616-
""",
617-
],
618566
provider: AddPackageTarget.self,
619567
context: .init(
620568
target: PackageTarget(
@@ -642,19 +590,6 @@ final class ManifestEditTests: XCTestCase {
642590
]
643591
)
644592
""",
645-
expectedAuxiliarySources: [
646-
"Tests/MyTest target-name/MyTest target-name.swift": """
647-
import Testing
648-
649-
@Suite
650-
struct MyTest_target_nameTests {
651-
@Test("MyTest_target_name tests")
652-
func example() {
653-
#expect(42 == 17 + 25)
654-
}
655-
}
656-
"""
657-
],
658593
provider: AddPackageTarget.self,
659594
context: .init(
660595
target: PackageTarget(
@@ -1033,12 +968,10 @@ final class ManifestEditTests: XCTestCase {
1033968
}
1034969

1035970
/// Assert that applying the given edit/refactor operation to the manifest
1036-
/// produces the expected manifest source file and the expected auxiliary
1037-
/// files.
971+
/// produces the expected manifest source file.
1038972
func assertManifestRefactor<Provider: ManifestEditRefactoringProvider>(
1039973
_ originalManifest: SourceFileSyntax,
1040974
expectedManifest: SourceFileSyntax,
1041-
expectedAuxiliarySources: [String: SourceFileSyntax] = [:],
1042975
provider: Provider.Type,
1043976
context: Provider.Context,
1044977
file: StaticString = #filePath,
@@ -1047,7 +980,6 @@ func assertManifestRefactor<Provider: ManifestEditRefactoringProvider>(
1047980
return try assertManifestRefactor(
1048981
originalManifest,
1049982
expectedManifest: expectedManifest,
1050-
expectedAuxiliarySources: expectedAuxiliarySources,
1051983
file: file,
1052984
line: line
1053985
) { (manifest) in
@@ -1056,12 +988,10 @@ func assertManifestRefactor<Provider: ManifestEditRefactoringProvider>(
1056988
}
1057989

1058990
/// Assert that applying the given edit/refactor operation to the manifest
1059-
/// produces the expected manifest source file and the expected auxiliary
1060-
/// files.
991+
/// produces the expected manifest source file.
1061992
func assertManifestRefactor(
1062993
_ originalManifest: SourceFileSyntax,
1063994
expectedManifest: SourceFileSyntax,
1064-
expectedAuxiliarySources: [String: SourceFileSyntax] = [:],
1065995
file: StaticString = #filePath,
1066996
line: UInt = #line,
1067997
operation: (SourceFileSyntax) throws -> PackageEdit
@@ -1079,25 +1009,4 @@ func assertManifestRefactor(
10791009
file: file,
10801010
line: line
10811011
)
1082-
1083-
// Check all of the auxiliary sources.
1084-
for (auxSourcePath, auxSourceSyntax) in edits.auxiliaryFiles {
1085-
guard let expectedSyntax = expectedAuxiliarySources[auxSourcePath] else {
1086-
XCTFail("unexpected auxiliary source file '\(auxSourcePath)' in \(expectedAuxiliarySources)")
1087-
return
1088-
}
1089-
1090-
assertStringsEqualWithDiff(
1091-
auxSourceSyntax.description,
1092-
expectedSyntax.description,
1093-
file: file,
1094-
line: line
1095-
)
1096-
}
1097-
1098-
XCTAssertEqual(
1099-
edits.auxiliaryFiles.count,
1100-
expectedAuxiliarySources.count,
1101-
"didn't get all of the auxiliary files we expected"
1102-
)
11031012
}

0 commit comments

Comments
 (0)