Skip to content

Commit c1037f5

Browse files
committed
[Commands] Switch AddTargetDependency to use refactoring action from swift-syntax
1 parent a4b2da7 commit c1037f5

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ let package = Package(
629629
"XCBuildSupport",
630630
"SwiftBuildSupport",
631631
"SwiftFixIt",
632-
] + swiftSyntaxDependencies(["SwiftIDEUtils"]),
632+
] + swiftSyntaxDependencies(["SwiftIDEUtils", "SwiftRefactor"]),
633633
exclude: ["CMakeLists.txt", "README.md"],
634634
swiftSettings: swift6CompatibleExperimentalFeatures + [
635635
.unsafeFlags(["-static"]),

Sources/Commands/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ add_library(Commands
5353
Utilities/MultiRootSupport.swift
5454
Utilities/PlainTextEncoder.swift
5555
Utilities/PluginDelegate.swift
56+
Utilities/RefactoringSupport.swift
5657
Utilities/SymbolGraphExtract.swift
5758
Utilities/TestingSupport.swift
5859
Utilities/XCTEvents.swift)
5960
target_link_libraries(Commands PUBLIC
6061
SwiftCollections::OrderedCollections
62+
SwiftSyntax::SwiftRefactor
6163
ArgumentParser
6264
Basics
6365
BinarySymbols

Sources/Commands/PackageCommands/AddTargetDependency.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import CoreCommands
1616
import Foundation
1717
import PackageGraph
1818
import PackageModel
19-
import PackageModelSyntax
2019
import SwiftParser
20+
@_spi(PackageRefactor) import SwiftRefactor
2121
import SwiftSyntax
2222
import TSCBasic
2323
import TSCUtility
@@ -66,17 +66,19 @@ extension SwiftPackageCommand {
6666
}
6767
}
6868

69-
let dependency: TargetDescription.Dependency
69+
let dependency: PackageTarget.Dependency
7070
if let package {
7171
dependency = .product(name: dependencyName, package: package)
7272
} else {
73-
dependency = .target(name: dependencyName, condition: nil)
73+
dependency = .target(name: dependencyName)
7474
}
7575

76-
let editResult = try PackageModelSyntax.AddTargetDependency.addTargetDependency(
77-
dependency,
78-
targetName: targetName,
79-
to: manifestSyntax
76+
let editResult = try SwiftRefactor.AddTargetDependency.manifestRefactor(
77+
syntax: manifestSyntax,
78+
in: .init(
79+
dependency: dependency,
80+
targetName: targetName
81+
)
8082
)
8183

8284
try editResult.applyEdits(
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Basics
14+
@_spi(FixItApplier) import SwiftIDEUtils
15+
@_spi(PackageRefactor) import SwiftRefactor
16+
import SwiftSyntax
17+
18+
package extension PackageEdit {
19+
/// Apply the edits for the given manifest to the specified file system,
20+
/// updating the manifest to the given manifest
21+
func applyEdits(
22+
to filesystem: any FileSystem,
23+
manifest: SourceFileSyntax,
24+
manifestPath: AbsolutePath,
25+
verbose: Bool
26+
) throws {
27+
let rootPath = manifestPath.parentDirectory
28+
29+
// Update the manifest
30+
if verbose {
31+
print("Updating package manifest at \(manifestPath.relative(to: rootPath))...", terminator: "")
32+
}
33+
34+
let updatedManifestSource = FixItApplier.apply(
35+
edits: manifestEdits,
36+
to: manifest
37+
)
38+
try filesystem.writeFileContents(
39+
manifestPath,
40+
string: updatedManifestSource
41+
)
42+
if verbose {
43+
print(" done.")
44+
}
45+
46+
// Write all of the auxiliary files.
47+
for (auxiliaryFileRelPath, auxiliaryFileSyntax) in auxiliaryFiles {
48+
// If the file already exists, skip it.
49+
let filePath = try rootPath.appending(RelativePath(validating: auxiliaryFileRelPath))
50+
if filesystem.exists(filePath) {
51+
if verbose {
52+
print("Skipping \(filePath.relative(to: rootPath)) because it already exists.")
53+
}
54+
55+
continue
56+
}
57+
58+
// If the directory does not exist yet, create it.
59+
let fileDir = filePath.parentDirectory
60+
if !filesystem.exists(fileDir) {
61+
if verbose {
62+
print("Creating directory \(fileDir.relative(to: rootPath))...", terminator: "")
63+
}
64+
65+
try filesystem.createDirectory(fileDir, recursive: true)
66+
67+
if verbose {
68+
print(" done.")
69+
}
70+
}
71+
72+
// Write the file.
73+
if verbose {
74+
print("Writing \(filePath.relative(to: rootPath))...", terminator: "")
75+
}
76+
77+
try filesystem.writeFileContents(
78+
filePath,
79+
string: auxiliaryFileSyntax.description
80+
)
81+
82+
if verbose {
83+
print(" done.")
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)