Skip to content

[Commands] Port package refactoring commands to use refactoring actions from swift-syntax #9016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ let swiftPMDataModelProduct = (
"PackageLoading",
"PackageMetadata",
"PackageModel",
"PackageModelSyntax",
"SourceControl",
"Workspace",
]
Expand Down Expand Up @@ -368,20 +367,6 @@ let package = Package(
]
),

.target(
/** Primary Package model objects relationship to SwiftSyntax */
name: "PackageModelSyntax",
dependencies: [
"Basics",
"PackageLoading",
"PackageModel",
] + swiftSyntaxDependencies(["SwiftBasicFormat", "SwiftDiagnostics", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"]),
exclude: ["CMakeLists.txt"],
swiftSettings: commonExperimentalFeatures + [
.unsafeFlags(["-static"]),
]
),

.target(
/** Package model conventions and loading support */
name: "PackageLoading",
Expand Down Expand Up @@ -623,13 +608,12 @@ let package = Package(
"Build",
"CoreCommands",
"PackageGraph",
"PackageModelSyntax",
"SourceControl",
"Workspace",
"XCBuildSupport",
"SwiftBuildSupport",
"SwiftFixIt",
] + swiftSyntaxDependencies(["SwiftIDEUtils"]),
] + swiftSyntaxDependencies(["SwiftIDEUtils", "SwiftRefactor"]),
exclude: ["CMakeLists.txt", "README.md"],
swiftSettings: swift6CompatibleExperimentalFeatures + [
.unsafeFlags(["-static"]),
Expand Down Expand Up @@ -928,13 +912,6 @@ let package = Package(
name: "PackageModelTests",
dependencies: ["PackageModel", "_InternalTestSupport"]
),
.testTarget(
name: "PackageModelSyntaxTests",
dependencies: [
"PackageModelSyntax",
"_InternalTestSupport",
] + swiftSyntaxDependencies(["SwiftIDEUtils"])
),
.testTarget(
name: "PackageGraphTests",
dependencies: ["PackageGraph", "_InternalTestSupport"],
Expand Down Expand Up @@ -1064,7 +1041,6 @@ if ProcessInfo.processInfo.environment["SWIFTCI_DISABLE_SDK_DEPENDENT_TESTS"] ==
"Build",
"Commands",
"PackageModel",
"PackageModelSyntax",
"PackageRegistryCommand",
"SourceControl",
"_InternalTestSupport",
Expand Down
1 change: 0 additions & 1 deletion Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ add_subdirectory(PackageFingerprint)
add_subdirectory(PackageGraph)
add_subdirectory(PackageLoading)
add_subdirectory(PackageModel)
add_subdirectory(PackageModelSyntax)
add_subdirectory(PackagePlugin)
add_subdirectory(PackageRegistry)
add_subdirectory(PackageRegistryCommand)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Commands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ add_library(Commands
Utilities/MultiRootSupport.swift
Utilities/PlainTextEncoder.swift
Utilities/PluginDelegate.swift
Utilities/RefactoringSupport.swift
Utilities/SymbolGraphExtract.swift
Utilities/TestingSupport.swift
Utilities/XCTEvents.swift)
target_link_libraries(Commands PUBLIC
SwiftCollections::OrderedCollections
SwiftSyntax::SwiftRefactor
ArgumentParser
Basics
BinarySymbols
Build
CoreCommands
LLBuildManifest
PackageGraph
PackageModelSyntax
SourceControl
SwiftFixIt
TSCBasic
Expand Down
65 changes: 40 additions & 25 deletions Sources/Commands/PackageCommands/AddDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import Basics
import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import PackageModelSyntax
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import TSCBasic
import TSCUtility
import Workspace

import class PackageModel.Manifest

extension SwiftPackageCommand {
struct AddDependency: SwiftCommand {
package static let configuration = CommandConfiguration(
Expand Down Expand Up @@ -98,7 +99,7 @@ extension SwiftPackageCommand {
// Collect all of the possible version requirements.
var requirements: [PackageDependency.SourceControl.Requirement] = []
if let exact {
requirements.append(.exact(exact))
requirements.append(.exact(exact.description))
}

if let branch {
Expand All @@ -110,11 +111,17 @@ extension SwiftPackageCommand {
}

if let from {
requirements.append(.range(.upToNextMajor(from: from)))
requirements.append(.rangeFrom(from.description))
}

if let upToNextMinorFrom {
requirements.append(.range(.upToNextMinor(from: upToNextMinorFrom)))
let range: Range<Version> = .upToNextMinor(from: upToNextMinorFrom)
requirements.append(
.range(
lowerBound: range.lowerBound.description,
upperBound: range.upperBound.description
)
)
}

if requirements.count > 1 {
Expand All @@ -130,13 +137,14 @@ extension SwiftPackageCommand {
}

let requirement: PackageDependency.SourceControl.Requirement
if case .range(let range) = firstRequirement {
if let to {
requirement = .range(range.lowerBound ..< to)
switch firstRequirement {
case .range(let lowerBound, _), .rangeFrom(let lowerBound):
requirement = if let to {
.range(lowerBound: lowerBound, upperBound: to.description)
} else {
requirement = .range(range)
firstRequirement
}
} else {
default:
requirement = firstRequirement

if self.to != nil {
Expand All @@ -147,7 +155,7 @@ extension SwiftPackageCommand {
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: .sourceControl(name: nil, location: url, requirement: requirement)
packageDependency: .sourceControl(.init(location: url, requirement: requirement))
)
}

Expand All @@ -159,15 +167,21 @@ extension SwiftPackageCommand {
// Collect all of the possible version requirements.
var requirements: [PackageDependency.Registry.Requirement] = []
if let exact {
requirements.append(.exact(exact))
requirements.append(.exact(exact.description))
}

if let from {
requirements.append(.range(.upToNextMajor(from: from)))
requirements.append(.rangeFrom(from.description))
}

if let upToNextMinorFrom {
requirements.append(.range(.upToNextMinor(from: upToNextMinorFrom)))
let range: Range<Version> = .upToNextMinor(from: upToNextMinorFrom)
requirements.append(
.range(
lowerBound: range.lowerBound.description,
upperBound: range.upperBound.description
)
)
}

if requirements.count > 1 {
Expand All @@ -183,13 +197,14 @@ extension SwiftPackageCommand {
}

let requirement: PackageDependency.Registry.Requirement
if case .range(let range) = firstRequirement {
if let to {
requirement = .range(range.lowerBound ..< to)
switch firstRequirement {
case .range(let lowerBound, _), .rangeFrom(let lowerBound):
requirement = if let to {
.range(lowerBound: lowerBound, upperBound: to.description)
} else {
requirement = .range(range)
firstRequirement
}
} else {
default:
requirement = firstRequirement

if self.to != nil {
Expand All @@ -200,7 +215,7 @@ extension SwiftPackageCommand {
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: .registry(id: id, requirement: requirement)
packageDependency: .registry(.init(identity: id, requirement: requirement))
)
}

Expand All @@ -212,14 +227,14 @@ extension SwiftPackageCommand {
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: .fileSystem(name: nil, path: directory)
packageDependency: .fileSystem(.init(path: directory))
)
}

private func applyEdits(
packagePath: Basics.AbsolutePath,
workspace: Workspace,
packageDependency: MappablePackageDependency.Kind
packageDependency: PackageDependency
) throws {
// Load the manifest file
let fileSystem = workspace.fileSystem
Expand All @@ -240,9 +255,9 @@ extension SwiftPackageCommand {
}
}

let editResult = try AddPackageDependency.addPackageDependency(
packageDependency,
to: manifestSyntax
let editResult = try AddPackageDependency.manifestRefactor(
syntax: manifestSyntax,
in: .init(dependency: packageDependency)
)

try editResult.applyEdits(
Expand Down
13 changes: 6 additions & 7 deletions Sources/Commands/PackageCommands/AddProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import Basics
import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import PackageModelSyntax
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import TSCBasic
import TSCUtility
Expand Down Expand Up @@ -80,23 +79,23 @@ extension SwiftPackageCommand {
}

// Map the product type.
let type: ProductType = switch self.type {
let type: ProductDescription.ProductType = switch self.type {
case .executable: .executable
case .library: .library(.automatic)
case .dynamicLibrary: .library(.dynamic)
case .staticLibrary: .library(.static)
case .plugin: .plugin
}

let product = try ProductDescription(
let product = ProductDescription(
name: name,
type: type,
targets: targets
)

let editResult = try PackageModelSyntax.AddProduct.addProduct(
product,
to: manifestSyntax
let editResult = try SwiftRefactor.AddProduct.manifestRefactor(
syntax: manifestSyntax,
in: .init(product: product)
)

try editResult.applyEdits(
Expand Down
25 changes: 22 additions & 3 deletions Sources/Commands/PackageCommands/AddSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import Basics
import CoreCommands
import Foundation
import PackageGraph
import PackageLoading
import PackageModel
import PackageModelSyntax
import SwiftParser
import SwiftSyntax
@_spi(PackageRefactor) import SwiftRefactor
import TSCBasic
import TSCUtility
import Workspace
Expand Down Expand Up @@ -124,32 +126,40 @@ extension SwiftPackageCommand {
}
}

let editResult: PackageEditResult
let editResult: PackageEdit

switch setting {
case .experimentalFeature:
try manifestSyntax.checkManifestAtLeast(.v5_8)

editResult = try AddSwiftSetting.experimentalFeature(
to: target,
name: value,
manifest: manifestSyntax
)
case .upcomingFeature:
try manifestSyntax.checkManifestAtLeast(.v5_8)

editResult = try AddSwiftSetting.upcomingFeature(
to: target,
name: value,
manifest: manifestSyntax
)
case .languageMode:
try manifestSyntax.checkManifestAtLeast(.v6_0)

guard let mode = SwiftLanguageVersion(string: value) else {
throw ValidationError("Unknown Swift language mode: \(value)")
}

editResult = try AddSwiftSetting.languageMode(
to: target,
mode: mode,
mode: mode.rawValue,
manifest: manifestSyntax
)
case .strictMemorySafety:
try manifestSyntax.checkManifestAtLeast(.v6_2)

guard value.isEmpty || value == SwiftSetting.strictMemorySafety.rawValue else {
throw ValidationError("'strictMemorySafety' does not support argument '\(value)'")
}
Expand All @@ -170,3 +180,12 @@ extension SwiftPackageCommand {
}
}
}

fileprivate extension SourceFileSyntax {
func checkManifestAtLeast(_ version: ToolsVersion) throws {
let toolsVersion = try ToolsVersionParser.parse(utf8String: description)
if toolsVersion < version {
throw StringError("package manifest version \(toolsVersion) is too old: please update to manifest version \(version) or newer")
}
}
}
Loading