Skip to content

Commit af2f096

Browse files
committed
[Commands] Switch AddSetting/Migrate to use refactoring action from swift-syntax
1 parent cbb2df3 commit af2f096

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

Sources/Commands/PackageCommands/AddSetting.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import Basics
1515
import CoreCommands
1616
import Foundation
1717
import PackageGraph
18+
import PackageLoading
1819
import PackageModel
19-
import PackageModelSyntax
2020
import SwiftParser
21+
import SwiftSyntax
22+
@_spi(PackageRefactor) import SwiftRefactor
2123
import TSCBasic
2224
import TSCUtility
2325
import Workspace
@@ -124,32 +126,40 @@ extension SwiftPackageCommand {
124126
}
125127
}
126128

127-
let editResult: PackageEditResult
129+
let editResult: PackageEdit
128130

129131
switch setting {
130132
case .experimentalFeature:
133+
try manifestSyntax.checkManifestAtLeast(.v5_8)
134+
131135
editResult = try AddSwiftSetting.experimentalFeature(
132136
to: target,
133137
name: value,
134138
manifest: manifestSyntax
135139
)
136140
case .upcomingFeature:
141+
try manifestSyntax.checkManifestAtLeast(.v5_8)
142+
137143
editResult = try AddSwiftSetting.upcomingFeature(
138144
to: target,
139145
name: value,
140146
manifest: manifestSyntax
141147
)
142148
case .languageMode:
149+
try manifestSyntax.checkManifestAtLeast(.v6_0)
150+
143151
guard let mode = SwiftLanguageVersion(string: value) else {
144152
throw ValidationError("Unknown Swift language mode: \(value)")
145153
}
146154

147155
editResult = try AddSwiftSetting.languageMode(
148156
to: target,
149-
mode: mode,
157+
mode: mode.rawValue,
150158
manifest: manifestSyntax
151159
)
152160
case .strictMemorySafety:
161+
try manifestSyntax.checkManifestAtLeast(.v6_2)
162+
153163
guard value.isEmpty || value == SwiftSetting.strictMemorySafety.rawValue else {
154164
throw ValidationError("'strictMemorySafety' does not support argument '\(value)'")
155165
}
@@ -170,3 +180,12 @@ extension SwiftPackageCommand {
170180
}
171181
}
172182
}
183+
184+
fileprivate extension SourceFileSyntax {
185+
func checkManifestAtLeast(_ version: ToolsVersion) throws {
186+
let toolsVersion = try ToolsVersionParser.parse(utf8String: description)
187+
if toolsVersion < version {
188+
throw StringError("package manifest version \(toolsVersion) is too old: please update to manifest version \(version) or newer")
189+
}
190+
}
191+
}

Sources/Commands/PackageCommands/Migrate.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import OrderedCollections
2323

2424
import PackageGraph
2525
import PackageModel
26-
import enum PackageModelSyntax.ManifestEditError
26+
27+
@_spi(PackageRefactor)
28+
import enum SwiftRefactor.ManifestEditError
2729

2830
import SPMBuildCore
2931
import SwiftFixIt
@@ -283,16 +285,14 @@ extension SwiftPackageCommand {
283285
switch error {
284286
case .cannotFindPackage,
285287
.cannotAddSettingsToPluginTarget,
286-
.existingDependency:
288+
.existingDependency,
289+
.malformedManifest:
287290
break
288291
case .cannotFindArrayLiteralArgument,
289292
// This means the target could not be found
290293
// syntactically, not that it does not exist.
291294
.cannotFindTargets,
292-
.cannotFindTarget,
293-
// This means the swift-tools-version is lower than
294-
// the version where one of the setting was introduced.
295-
.oldManifest:
295+
.cannotFindTarget:
296296
let settings = try features.map {
297297
try $0.swiftSettingDescription
298298
}.joined(separator: ", ")

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,8 +2333,8 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
23332333
error.stderr,
23342334
.contains(
23352335
"""
2336-
error: Could not update manifest to enable requested features for target 'A' (package manifest version 5.8.0 is too old: please update to manifest version 6.2.0 or newer). Please enable them manually by adding the following Swift settings to the target: '.enableUpcomingFeature("ExistentialAny"), .enableUpcomingFeature("InferIsolatedConformances"), .strictMemorySafety()'
2337-
error: Could not update manifest to enable requested features for target 'B' (package manifest version 5.8.0 is too old: please update to manifest version 6.2.0 or newer). Please enable them manually by adding the following Swift settings to the target: '.enableUpcomingFeature("ExistentialAny"), .enableUpcomingFeature("InferIsolatedConformances"), .strictMemorySafety()'
2336+
error: Could not update manifest to enable requested features for target 'A' (package manifest version 5.8.0 is too old: please update to manifest version 6.2.0 or newer)
2337+
error: Could not update manifest to enable requested features for target 'B' (package manifest version 5.8.0 is too old: please update to manifest version 6.2.0 or newer)
23382338
error: Could not update manifest to enable requested features for target 'CannotFindSettings' (unable to find array literal for 'swiftSettings' argument). Please enable them manually by adding the following Swift settings to the target: '.enableUpcomingFeature("ExistentialAny"), .enableUpcomingFeature("InferIsolatedConformances"), .strictMemorySafety()'
23392339
error: Could not update manifest to enable requested features for target 'CannotFindTarget' (unable to find target named 'CannotFindTarget' in package). Please enable them manually by adding the following Swift settings to the target: '.enableUpcomingFeature("ExistentialAny"), .enableUpcomingFeature("InferIsolatedConformances"), .strictMemorySafety()'
23402340
"""

0 commit comments

Comments
 (0)