|
| 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 AddProduct: SwiftCommand { |
| 26 | + /// The package product type used for the command-line. This is a |
| 27 | + /// subset of `ProductType` that expands out the library types. |
| 28 | + enum CommandProductType: String, Codable, ExpressibleByArgument { |
| 29 | + case executable |
| 30 | + case library |
| 31 | + case staticLibrary = "static-library" |
| 32 | + case dynamicLibrary = "dynamic-library" |
| 33 | + case plugin |
| 34 | + } |
| 35 | + |
| 36 | + package static let configuration = CommandConfiguration( |
| 37 | + abstract: "Add a new product to the manifest") |
| 38 | + |
| 39 | + @OptionGroup(visibility: .hidden) |
| 40 | + var globalOptions: GlobalOptions |
| 41 | + |
| 42 | + @Argument(help: "The name of the new product") |
| 43 | + var name: String |
| 44 | + |
| 45 | + @Option(help: "The type of target to add, which can be one of 'executable', 'library', 'static-library', 'dynamic-library', or 'plugin'") |
| 46 | + var type: CommandProductType = .library |
| 47 | + |
| 48 | + @Option( |
| 49 | + parsing: .upToNextOption, |
| 50 | + help: "A list of targets that are part of this product" |
| 51 | + ) |
| 52 | + var targets: [String] = [] |
| 53 | + |
| 54 | + @Option(help: "The URL for a remote binary target") |
| 55 | + var url: String? |
| 56 | + |
| 57 | + @Option(help: "The path to a local binary target") |
| 58 | + var path: String? |
| 59 | + |
| 60 | + @Option(help: "The checksum for a remote binary target") |
| 61 | + var checksum: String? |
| 62 | + |
| 63 | + func run(_ swiftCommandState: SwiftCommandState) throws { |
| 64 | + let workspace = try swiftCommandState.getActiveWorkspace() |
| 65 | + |
| 66 | + guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else { |
| 67 | + throw StringError("unknown package") |
| 68 | + } |
| 69 | + |
| 70 | + // Load the manifest file |
| 71 | + let fileSystem = workspace.fileSystem |
| 72 | + let manifestPath = packagePath.appending("Package.swift") |
| 73 | + let manifestContents: ByteString |
| 74 | + do { |
| 75 | + manifestContents = try fileSystem.readFileContents(manifestPath) |
| 76 | + } catch { |
| 77 | + throw StringError("cannot find package manifest in \(manifestPath)") |
| 78 | + } |
| 79 | + |
| 80 | + // Parse the manifest. |
| 81 | + let manifestSyntax = manifestContents.withData { data in |
| 82 | + data.withUnsafeBytes { buffer in |
| 83 | + buffer.withMemoryRebound(to: UInt8.self) { buffer in |
| 84 | + Parser.parse(source: buffer) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Map the product type. |
| 90 | + let type: ProductType = switch self.type { |
| 91 | + case .executable: .executable |
| 92 | + case .library: .library(.automatic) |
| 93 | + case .dynamicLibrary: .library(.dynamic) |
| 94 | + case .staticLibrary: .library(.static) |
| 95 | + case .plugin: .plugin |
| 96 | + } |
| 97 | + |
| 98 | + let product = try ProductDescription( |
| 99 | + name: name, |
| 100 | + type: type, |
| 101 | + targets: targets |
| 102 | + ) |
| 103 | + |
| 104 | + let editResult = try PackageModelSyntax.AddProduct.addProduct( |
| 105 | + product, |
| 106 | + to: manifestSyntax |
| 107 | + ) |
| 108 | + |
| 109 | + try editResult.applyEdits( |
| 110 | + to: fileSystem, |
| 111 | + manifest: manifestSyntax, |
| 112 | + manifestPath: manifestPath, |
| 113 | + verbose: !globalOptions.logging.quiet |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments