Skip to content

Allow relative path with swift package add-dependency command #7871

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

Merged
merged 1 commit into from
Feb 14, 2025
Merged
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
51 changes: 11 additions & 40 deletions Sources/Commands/PackageCommands/AddDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ extension SwiftPackageCommand {
throw StringError("unknown package")
}

switch type {
switch self.type {
case .url:
try self.createSourceControlPackage(
packagePath: packagePath,
workspace: workspace,
url: dependency
url: self.dependency
)
case .path:
try self.createFileSystemPackage(
packagePath: packagePath,
workspace: workspace,
directory: dependency
directory: self.dependency
)
case .registry:
try self.createRegistryPackage(
packagePath: packagePath,
workspace: workspace,
id: dependency
id: self.dependency
)
}
}
Expand Down Expand Up @@ -144,19 +144,10 @@ extension SwiftPackageCommand {
}
}

let packageDependency: PackageDependency = .sourceControl(
identity: identity,
nameForTargetDependencyResolutionOnly: nil,
location: .remote(.init(url)),
requirement: requirement,
productFilter: .everything,
traits: []
)

try applyEdits(
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: packageDependency
packageDependency: .sourceControl(name: nil, location: url, requirement: requirement)
)
}

Expand Down Expand Up @@ -208,18 +199,10 @@ extension SwiftPackageCommand {
}
}

let packageDependency: PackageDependency = .registry(
identity: identity,
requirement: requirement,
productFilter: .everything,
traits: []
)


try applyEdits(
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: packageDependency
packageDependency: .registry(id: id, requirement: requirement)
)
}

Expand All @@ -228,29 +211,17 @@ extension SwiftPackageCommand {
workspace: Workspace,
directory: String
) throws {
guard let path = try? Basics.AbsolutePath(validating: directory) else {
throw StringError("Package path not found")
}
let identity = PackageIdentity(path: path)
let packageDependency: PackageDependency = .fileSystem(
identity: identity,
nameForTargetDependencyResolutionOnly: nil,
path: path,
productFilter: .everything,
traits: []
)

try applyEdits(
try self.applyEdits(
packagePath: packagePath,
workspace: workspace,
packageDependency: packageDependency
packageDependency: .fileSystem(name: nil, path: directory)
)
}

private func applyEdits(
packagePath: Basics.AbsolutePath,
workspace: Workspace,
packageDependency: PackageDependency
packageDependency: MappablePackageDependency.Kind
) throws {
// Load the manifest file
let fileSystem = workspace.fileSystem
Expand Down
12 changes: 6 additions & 6 deletions Sources/PackageModelSyntax/AddPackageDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftSyntax
import SwiftSyntaxBuilder

/// Add a package dependency to a manifest's source code.
public struct AddPackageDependency {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swiftformat made this change

public enum AddPackageDependency {
/// The set of argument labels that can occur after the "dependencies"
/// argument in the Package initializers.
///
Expand All @@ -28,13 +28,13 @@ public struct AddPackageDependency {
"targets",
"swiftLanguageVersions",
"cLanguageStandard",
"cxxLanguageStandard"
"cxxLanguageStandard",
]

/// Produce the set of source edits needed to add the given package
/// dependency to the given manifest file.
public static func addPackageDependency(
_ dependency: PackageDependency,
_ dependency: MappablePackageDependency.Kind,
to manifest: SourceFileSyntax
) throws -> PackageEditResult {
// Make sure we have a suitable tools version in the manifest.
Expand All @@ -50,19 +50,19 @@ public struct AddPackageDependency {

return PackageEditResult(
manifestEdits: [
.replace(packageCall, with: newPackageCall.description)
.replace(packageCall, with: newPackageCall.description),
]
)
}

/// Implementation of adding a package dependency to an existing call.
static func addPackageDependencyLocal(
_ dependency: PackageDependency,
_ dependency: MappablePackageDependency.Kind,
to packageCall: FunctionCallExprSyntax
) throws -> FunctionCallExprSyntax {
try packageCall.appendingToArrayArgument(
label: "dependencies",
trailingLabels: Self.argumentLabelsAfterDependencies,
trailingLabels: self.argumentLabelsAfterDependencies,
newElement: dependency.asSyntax()
)
}
Expand Down
73 changes: 34 additions & 39 deletions Sources/PackageModelSyntax/AddTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftSyntaxBuilder
import struct TSCUtility.Version

/// Add a target to a manifest's source code.
public struct AddTarget {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swiftformat made this change

public enum AddTarget {
/// The set of argument labels that can occur after the "targets"
/// argument in the Package initializers.
///
Expand All @@ -27,7 +27,7 @@ public struct AddTarget {
private static let argumentLabelsAfterTargets: Set<String> = [
"swiftLanguageVersions",
"cLanguageStandard",
"cxxLanguageStandard"
"cxxLanguageStandard",
]

/// The kind of test harness to use. This isn't part of the manifest
Expand Down Expand Up @@ -85,7 +85,7 @@ public struct AddTarget {
target.dependencies.append(contentsOf: macroTargetDependencies)

default:
break;
break
}

var newPackageCall = try packageCall.appendingToArrayArgument(
Expand All @@ -103,7 +103,7 @@ public struct AddTarget {
guard let outerDirectory else {
return PackageEditResult(
manifestEdits: [
.replace(packageCall, with: newPackageCall.description)
.replace(packageCall, with: newPackageCall.description),
]
)
}
Expand All @@ -114,7 +114,7 @@ public struct AddTarget {
var auxiliaryFiles: AuxiliaryFiles = []

// Add the primary source file. Every target type has this.
addPrimarySourceFile(
self.addPrimarySourceFile(
outerPath: outerPath,
target: target,
configuration: configuration,
Expand All @@ -125,7 +125,7 @@ public struct AddTarget {
var extraManifestEdits: [SourceEdit] = []
switch target.type {
case .macro:
addProvidedMacrosSourceFile(
self.addProvidedMacrosSourceFile(
outerPath: outerPath,
target: target,
to: &auxiliaryFiles
Expand All @@ -135,7 +135,7 @@ public struct AddTarget {
newPackageCall = try AddPackageDependency
.addPackageDependencyLocal(
.swiftSyntax(
configuration: installedSwiftPMConfiguration
configuration: installedSwiftPMConfiguration
),
to: newPackageCall
)
Expand All @@ -149,7 +149,7 @@ public struct AddTarget {
.positionAfterSkippingLeadingTrivia
extraManifestEdits.append(
SourceEdit(
range: insertPos..<insertPos,
range: insertPos ..< insertPos,
replacement: newImport
)
)
Expand All @@ -158,12 +158,12 @@ public struct AddTarget {
}
}

default: break;
default: break
}

return PackageEditResult(
manifestEdits: [
.replace(packageCall, with: newPackageCall.description)
.replace(packageCall, with: newPackageCall.description),
] + extraManifestEdits,
auxiliaryFiles: auxiliaryFiles
)
Expand All @@ -182,20 +182,18 @@ public struct AddTarget {
)

// Introduce imports for each of the dependencies that were specified.
var importModuleNames = target.dependencies.map {
$0.name
}
var importModuleNames = target.dependencies.map(\.name)

// Add appropriate test module dependencies.
if target.type == .test {
switch configuration.testHarness {
case .none:
case .none:
break

case .xctest:
importModuleNames.append("XCTest")

case .swiftTesting:
case .swiftTesting:
importModuleNames.append("Testing")
}
}
Expand Down Expand Up @@ -306,25 +304,25 @@ public struct AddTarget {
}
}

fileprivate extension TargetDescription.Dependency {
extension TargetDescription.Dependency {
/// Retrieve the name of the dependency
var name: String {
fileprivate var name: String {
switch self {
case .target(name: let name, condition: _),
.byName(name: let name, condition: _),
.product(name: let name, package: _, moduleAliases: _, condition: _):
.byName(name: let name, condition: _),
.product(name: let name, package: _, moduleAliases: _, condition: _):
name
}
}
}

/// The array of auxiliary files that can be added by a package editing
/// operation.
fileprivate typealias AuxiliaryFiles = [(RelativePath, SourceFileSyntax)]
private typealias AuxiliaryFiles = [(RelativePath, SourceFileSyntax)]

fileprivate extension AuxiliaryFiles {
extension AuxiliaryFiles {
/// Add a source file to the list of auxiliary files.
mutating func addSourceFile(
fileprivate mutating func addSourceFile(
path: RelativePath,
sourceCode: SourceFileSyntax
) {
Expand All @@ -334,45 +332,42 @@ fileprivate extension AuxiliaryFiles {

/// The set of dependencies we need to introduce to a newly-created macro
/// target.
fileprivate let macroTargetDependencies: [TargetDescription.Dependency] = [
private let macroTargetDependencies: [TargetDescription.Dependency] = [
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
]

/// The package dependency for swift-syntax, for use in macros.
fileprivate extension PackageDependency {
extension MappablePackageDependency.Kind {
/// Source control URL for the swift-syntax package.
static var swiftSyntaxURL: SourceControlURL {
fileprivate static var swiftSyntaxURL: SourceControlURL {
"https://github.com/swiftlang/swift-syntax.git"
}

/// Package dependency on the swift-syntax package.
static func swiftSyntax(
configuration: InstalledSwiftPMConfiguration
) -> PackageDependency {
fileprivate static func swiftSyntax(
configuration: InstalledSwiftPMConfiguration
) -> MappablePackageDependency.Kind {
let swiftSyntaxVersionDefault = configuration
.swiftSyntaxVersionForMacroTemplate
let swiftSyntaxVersion = Version(swiftSyntaxVersionDefault.description)!

return .sourceControl(
identity: PackageIdentity(url: swiftSyntaxURL),
nameForTargetDependencyResolutionOnly: nil,
location: .remote(swiftSyntaxURL),
requirement: .range(.upToNextMajor(from: swiftSyntaxVersion)),
productFilter: .everything,
traits: []
name: nil,
location: self.swiftSyntaxURL.absoluteString,
requirement: .range(.upToNextMajor(from: swiftSyntaxVersion))
)
}
}

fileprivate extension TargetDescription {
var sanitizedName: String {
name
extension TargetDescription {
fileprivate var sanitizedName: String {
self.name
.spm_mangledToC99ExtendedIdentifier()
.localizedFirstWordCapitalized()
}
}

fileprivate extension String {
func localizedFirstWordCapitalized() -> String { prefix(1).localizedCapitalized + dropFirst() }
extension String {
fileprivate func localizedFirstWordCapitalized() -> String { prefix(1).localizedCapitalized + dropFirst() }
}
Loading