-
Notifications
You must be signed in to change notification settings - Fork 528
Add refactoring action to upgrade package dependencies to latest major version #3366
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
ahoppen
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
ahoppen:upgrade-package-dependencies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
Sources/SwiftRefactor/PackageManifest/UpgradePackageDependencies.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftParser | ||
| import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
|
|
||
| /// Upgrade all package dependencies that have a `from` or `exact` version restriction to their latest released version, | ||
| /// including new major versions. | ||
| /// | ||
| /// The intention of this refactoring is to update leaf package (like executables) to the latest version of their | ||
| /// dependencies, allowing them to also pick up potentially API-breaking new major or prerelease versions that can | ||
| /// easily go unnoticed using `swift package update`. | ||
| @_spi(PackageRefactor) | ||
| public struct UpgradePackageDependencies: EditRefactoringProvider { | ||
| public typealias Input = SourceFileSyntax | ||
|
|
||
| public struct Context { | ||
| let latestVersions: [String: String] | ||
|
|
||
| public init( | ||
| resolvingLatestVersionIn manifest: SourceFileSyntax, | ||
| using resolveLatestPackageVersion: @Sendable @escaping (_ url: String, _ currentVersion: String) async -> String? | ||
| ) async throws { | ||
| let dependencies = try findDependencies(in: manifest) | ||
| self.latestVersions = await withTaskGroup(of: (url: String, latestVersion: String)?.self) { taskGroup in | ||
| for dependency in dependencies { | ||
| taskGroup.addTask { | ||
| guard let url = dependency.url.representedLiteralValue, | ||
| let currentVersion = dependency.currentVersion.representedLiteralValue | ||
| else { | ||
| return nil | ||
| } | ||
| guard let latestVersion = await resolveLatestPackageVersion(url, currentVersion) else { | ||
| return nil | ||
| } | ||
| return (url, latestVersion) | ||
| } | ||
| } | ||
| var latestVersions: [String: String] = [:] | ||
| for await case .some(let result) in taskGroup { | ||
| latestVersions[result.url] = result.latestVersion | ||
| } | ||
| return latestVersions | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct VersionedDependency { | ||
| let url: StringLiteralExprSyntax | ||
| let currentVersion: StringLiteralExprSyntax | ||
| } | ||
|
|
||
| private static func findDependencies(in manifest: SourceFileSyntax) throws -> [VersionedDependency] { | ||
| guard let packageCall = manifest.findCall(calleeName: "Package") else { | ||
| throw ManifestEditError.cannotFindPackage | ||
| } | ||
| guard let dependencies = packageCall.findArgument(labeled: "dependencies")?.expression.as(ArrayExprSyntax.self) | ||
| else { | ||
| throw ManifestEditError.cannotFindDependencies | ||
| } | ||
| return dependencies.elements.compactMap { (dependency) -> VersionedDependency? in | ||
| guard let functionCall = dependency.expression.as(FunctionCallExprSyntax.self), | ||
| functionCall.calledExpression.as(MemberAccessExprSyntax.self)?.declName.baseName.tokenKind | ||
| == .identifier("package"), | ||
| let url = functionCall.findArgument(labeled: "url")?.expression.as(StringLiteralExprSyntax.self) | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| // TODO: Support version initialized using `Version` initializer | ||
| if let version = functionCall.findArgument(labeled: "from")?.expression.as(StringLiteralExprSyntax.self) { | ||
| return VersionedDependency(url: url, currentVersion: version) | ||
| } else if let version = functionCall.findArgument(labeled: "exact")?.expression.as(StringLiteralExprSyntax.self) { | ||
| return VersionedDependency(url: url, currentVersion: version) | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static func textRefactor(syntax manifest: SourceFileSyntax, in context: Context) throws -> [SourceEdit] { | ||
| return try findDependencies(in: manifest).compactMap { dependency in | ||
| guard let urlValue = dependency.url.representedLiteralValue, let latestVersion = context.latestVersions[urlValue] | ||
| else { | ||
| return nil | ||
| } | ||
| guard dependency.currentVersion.representedLiteralValue != latestVersion else { | ||
| return nil | ||
| } | ||
| return SourceEdit( | ||
| range: dependency.currentVersion.trimmedRange, | ||
| replacement: StringLiteralExprSyntax(content: latestVersion).description | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will address this if there is a consensus that we want swiftlang/swift-package-manager#10212.