|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import ArgumentParser |
| 14 | +import Basics |
| 15 | +import CoreCommands |
| 16 | +import PackageModel |
| 17 | +import PackageModelSyntax |
| 18 | +import SwiftParser |
| 19 | +import SwiftSyntax |
| 20 | +import TSCBasic |
| 21 | +import TSCUtility |
| 22 | +import Workspace |
| 23 | + |
| 24 | +extension SwiftPackageCommand { |
| 25 | + struct AddDependency: SwiftCommand { |
| 26 | + package static let configuration = CommandConfiguration( |
| 27 | + abstract: "Add a package dependency to the manifest") |
| 28 | + |
| 29 | + @Argument(help: "The URL or directory of the package to add") |
| 30 | + var dependency: String |
| 31 | + |
| 32 | + @OptionGroup(visibility: .hidden) |
| 33 | + var globalOptions: GlobalOptions |
| 34 | + |
| 35 | + @Option(help: "The exact package version to depend on") |
| 36 | + var exact: Version? |
| 37 | + |
| 38 | + @Option(help: "The specific package revision to depend on") |
| 39 | + var revision: String? |
| 40 | + |
| 41 | + @Option(help: "The branch of the package to depend on") |
| 42 | + var branch: String? |
| 43 | + |
| 44 | + @Option(help: "The package version to depend on (up to the next major version)") |
| 45 | + var from: Version? |
| 46 | + |
| 47 | + @Option(help: "The package version to depend on (up to the next minor version)") |
| 48 | + var upToNextMinorFrom: Version? |
| 49 | + |
| 50 | + @Option(help: "Specify upper bound on the package version range (exclusive)") |
| 51 | + var to: Version? |
| 52 | + |
| 53 | + func run(_ swiftCommandState: SwiftCommandState) throws { |
| 54 | + let workspace = try swiftCommandState.getActiveWorkspace() |
| 55 | + |
| 56 | + guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else { |
| 57 | + throw StringError("unknown package") |
| 58 | + } |
| 59 | + |
| 60 | + // Load the manifest file |
| 61 | + let fileSystem = workspace.fileSystem |
| 62 | + let manifestPath = packagePath.appending("Package.swift") |
| 63 | + let manifestContents: ByteString |
| 64 | + do { |
| 65 | + manifestContents = try fileSystem.readFileContents(manifestPath) |
| 66 | + } catch { |
| 67 | + throw StringError("cannot find package manifest in \(manifestPath)") |
| 68 | + } |
| 69 | + |
| 70 | + // Parse the manifest. |
| 71 | + let manifestSyntax = manifestContents.withData { data in |
| 72 | + data.withUnsafeBytes { buffer in |
| 73 | + buffer.withMemoryRebound(to: UInt8.self) { buffer in |
| 74 | + Parser.parse(source: buffer) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + let identity = PackageIdentity(url: .init(dependency)) |
| 80 | + |
| 81 | + // Collect all of the possible version requirements. |
| 82 | + var requirements: [PackageDependency.SourceControl.Requirement] = [] |
| 83 | + if let exact { |
| 84 | + requirements.append(.exact(exact)) |
| 85 | + } |
| 86 | + |
| 87 | + if let branch { |
| 88 | + requirements.append(.branch(branch)) |
| 89 | + } |
| 90 | + |
| 91 | + if let revision { |
| 92 | + requirements.append(.revision(revision)) |
| 93 | + } |
| 94 | + |
| 95 | + if let from { |
| 96 | + requirements.append(.range(.upToNextMajor(from: from))) |
| 97 | + } |
| 98 | + |
| 99 | + if let upToNextMinorFrom { |
| 100 | + requirements.append(.range(.upToNextMinor(from: upToNextMinorFrom))) |
| 101 | + } |
| 102 | + |
| 103 | + if requirements.count > 1 { |
| 104 | + throw StringError("must specify at most one of --exact, --branch, --revision, --from, or --up-to-next-minor-from") |
| 105 | + } |
| 106 | + |
| 107 | + guard let firstRequirement = requirements.first else { |
| 108 | + throw StringError("must specify one of --exact, --branch, --revision, --from, or --up-to-next-minor-from") |
| 109 | + } |
| 110 | + |
| 111 | + let requirement: PackageDependency.SourceControl.Requirement |
| 112 | + if case .range(let range) = firstRequirement { |
| 113 | + if let to { |
| 114 | + requirement = .range(range.lowerBound..<to) |
| 115 | + } else { |
| 116 | + requirement = .range(range) |
| 117 | + } |
| 118 | + } else { |
| 119 | + requirement = firstRequirement |
| 120 | + |
| 121 | + if to != nil { |
| 122 | + throw StringError("--to can only be specified with --from or --up-to-next-minor-from") |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Figure out the location of the package. |
| 127 | + let location: PackageDependency.SourceControl.Location |
| 128 | + if let path = try? Basics.AbsolutePath(validating: dependency) { |
| 129 | + location = .local(path) |
| 130 | + } else { |
| 131 | + location = .remote(.init(dependency)) |
| 132 | + } |
| 133 | + |
| 134 | + let packageDependency: PackageDependency = .sourceControl( |
| 135 | + identity: identity, |
| 136 | + nameForTargetDependencyResolutionOnly: nil, |
| 137 | + location: location, |
| 138 | + requirement: requirement, |
| 139 | + productFilter: .everything |
| 140 | + ) |
| 141 | + |
| 142 | + let editResult = try AddPackageDependency.addPackageDependency( |
| 143 | + packageDependency, |
| 144 | + to: manifestSyntax |
| 145 | + ) |
| 146 | + |
| 147 | + try editResult.applyEdits( |
| 148 | + to: fileSystem, |
| 149 | + manifest: manifestSyntax, |
| 150 | + manifestPath: manifestPath, |
| 151 | + verbose: !globalOptions.logging.quiet |
| 152 | + ) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments