Skip to content

Commit d238d33

Browse files
committed
Align "add dependency" command line with SE-0301
1 parent 601fa31 commit d238d33

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

Sources/Commands/PackageCommands/Add.swift renamed to Sources/Commands/PackageCommands/AddDependency.swift

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,33 @@ import TSCUtility
2323
import Workspace
2424

2525
extension SwiftPackageCommand {
26-
struct Add: SwiftCommand {
26+
struct AddDependency: SwiftCommand {
2727
package static let configuration = CommandConfiguration(
28-
abstract: "Add a dependency to the package manifest")
28+
abstract: "Add a package dependency to the manifest")
29+
30+
@Argument(help: "The URL or directory of the package to add")
31+
var dependency: String
2932

3033
@OptionGroup(visibility: .hidden)
3134
var globalOptions: GlobalOptions
3235

33-
@Option(help: "The branch to depend on")
34-
var branch: String?
36+
@Option(help: "The exact package version to depend on")
37+
var exact: Version?
3538

36-
@Option(help: "The minimum version requirement")
37-
var fromVersion: String?
39+
@Option(help: "The specific package revision to depend on")
40+
var revision: String?
3841

39-
@Option(help: "The exact version requirement")
40-
var exactVersion: String?
42+
@Option(help: "The branch of the package to depend on")
43+
var branch: String?
4144

42-
@Option(help: "A specific revision requirement")
43-
var revision: String?
45+
@Option(help: "The package version to depend on (up to the next major version)")
46+
var from: Version?
4447

45-
// FIXME: range option
48+
@Option(help: "The package version to depend on (up to the next minor version)")
49+
var upToNextMinorFrom: Version?
4650

47-
@Argument(help: "The URL or directory of the package to add")
48-
var url: String
51+
@Option(help: "Specify upper bound on the package version range (exclusive)")
52+
var to: Version?
4953

5054
func run(_ swiftCommandState: SwiftCommandState) throws {
5155
let workspace = try swiftCommandState.getActiveWorkspace()
@@ -73,26 +77,59 @@ extension SwiftPackageCommand {
7377
}
7478
}
7579

76-
let identity = PackageIdentity(url: .init(url))
80+
let identity = PackageIdentity(url: .init(dependency))
81+
82+
// Collect all of the possible version requirements.
83+
var requirements: [PackageDependency.SourceControl.Requirement] = []
84+
if let exact {
85+
requirements.append(.exact(exact))
86+
}
7787

78-
// Figure out the version requirement.
79-
let requirement: PackageDependency.SourceControl.Requirement
8088
if let branch {
81-
requirement = .branch(branch)
82-
} else if let fromVersion {
83-
requirement = .revision(fromVersion)
84-
} else if let exactVersion {
85-
requirement = .exact(try Version(versionString: exactVersion))
89+
requirements.append(.branch(branch))
90+
}
91+
92+
if let revision {
93+
requirements.append(.revision(revision))
94+
}
95+
96+
if let from {
97+
requirements.append(.range(.upToNextMajor(from: from)))
98+
}
99+
100+
if let upToNextMinorFrom {
101+
requirements.append(.range(.upToNextMinor(from: upToNextMinorFrom)))
102+
}
103+
104+
if requirements.count > 1 {
105+
throw StringError("must specify at most one of --exact, --branch, --revision, --from, or --up-to-next-minor-from")
106+
}
107+
108+
guard let firstRequirement = requirements.first else {
109+
throw StringError("must specify one of --exact, --branch, --revision, --from, or --up-to-next-minor-from")
110+
}
111+
112+
let requirement: PackageDependency.SourceControl.Requirement
113+
if case .range(let range) = firstRequirement {
114+
if let to {
115+
requirement = .range(range.lowerBound..<to)
116+
} else {
117+
requirement = .range(range)
118+
}
86119
} else {
87-
throw StringError("must specify one of --branch, --from-version, or --exact-version")
120+
requirement = firstRequirement
121+
122+
if to != nil {
123+
throw StringError("--to can only be specified with --from or --up-to-next-minor-from")
124+
}
88125
}
89126

90127
// Figure out the location of the package.
91128
let location: PackageDependency.SourceControl.Location
92-
if let path = try? Basics.AbsolutePath(validating: url) {
129+
if let path = try? Basics.AbsolutePath(validating: dependency) {
93130
location = .local(path)
94131
} else {
95-
location = .remote(.init(url))
132+
location = .remote(.init(dependency))
96133
}
97134

98135
let packageDependency: PackageDependency = .sourceControl(

Sources/Commands/PackageCommands/SwiftPackageCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ package struct SwiftPackageCommand: AsyncParsableCommand {
3535
discussion: "SEE ALSO: swift build, swift run, swift test",
3636
version: SwiftVersion.current.completeDisplayString,
3737
subcommands: [
38-
Add.self,
38+
AddDependency.self,
3939
Clean.self,
4040
PurgeCache.self,
4141
Reset.self,

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ final class PackageCommandTests: CommandsTestCase {
790790
}
791791
}
792792

793-
func testPackageAddVersion() throws {
793+
func testPackageAddDependency() throws {
794794
try testWithTemporaryDirectory { tmpPath in
795795
let fs = localFileSystem
796796
let path = tmpPath.appending("PackageB")
@@ -807,7 +807,7 @@ final class PackageCommandTests: CommandsTestCase {
807807
"""
808808
)
809809

810-
_ = try execute(["add", "--branch", "main", "https://github.com/apple/swift-syntax.git"], packagePath: path)
810+
_ = try execute(["add-dependency", "--branch", "main", "https://github.com/apple/swift-syntax.git"], packagePath: path)
811811

812812
let manifest = path.appending("Package.swift")
813813
XCTAssertFileExists(manifest)

0 commit comments

Comments
 (0)