|
| 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 AddTargetDependency: SwiftCommand { |
| 26 | + package static let configuration = CommandConfiguration( |
| 27 | + abstract: "Add a new target dependency to the manifest") |
| 28 | + |
| 29 | + @OptionGroup(visibility: .hidden) |
| 30 | + var globalOptions: GlobalOptions |
| 31 | + |
| 32 | + @Argument(help: "The name of the new dependency") |
| 33 | + var dependencyName: String |
| 34 | + |
| 35 | + @Argument(help: "The name of the target to update") |
| 36 | + var targetName: String |
| 37 | + |
| 38 | + @Option(help: "The package in which the dependency resides") |
| 39 | + var package: String? |
| 40 | + |
| 41 | + func run(_ swiftCommandState: SwiftCommandState) throws { |
| 42 | + let workspace = try swiftCommandState.getActiveWorkspace() |
| 43 | + |
| 44 | + guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else { |
| 45 | + throw StringError("unknown package") |
| 46 | + } |
| 47 | + |
| 48 | + // Load the manifest file |
| 49 | + let fileSystem = workspace.fileSystem |
| 50 | + let manifestPath = packagePath.appending("Package.swift") |
| 51 | + let manifestContents: ByteString |
| 52 | + do { |
| 53 | + manifestContents = try fileSystem.readFileContents(manifestPath) |
| 54 | + } catch { |
| 55 | + throw StringError("cannot find package manifest in \(manifestPath)") |
| 56 | + } |
| 57 | + |
| 58 | + // Parse the manifest. |
| 59 | + let manifestSyntax = manifestContents.withData { data in |
| 60 | + data.withUnsafeBytes { buffer in |
| 61 | + buffer.withMemoryRebound(to: UInt8.self) { buffer in |
| 62 | + Parser.parse(source: buffer) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + let dependency: TargetDescription.Dependency |
| 68 | + if let package { |
| 69 | + dependency = .product(name: dependencyName, package: package) |
| 70 | + } else { |
| 71 | + dependency = .target(name: dependencyName, condition: nil) |
| 72 | + } |
| 73 | + |
| 74 | + let editResult = try PackageModelSyntax.AddTargetDependency.addTargetDependency( |
| 75 | + dependency, |
| 76 | + targetName: targetName, |
| 77 | + to: manifestSyntax |
| 78 | + ) |
| 79 | + |
| 80 | + try editResult.applyEdits( |
| 81 | + to: fileSystem, |
| 82 | + manifest: manifestSyntax, |
| 83 | + manifestPath: manifestPath, |
| 84 | + verbose: !globalOptions.logging.quiet |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments