|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 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 CoreCommands |
| 15 | +import Foundation |
| 16 | +import PackageModel |
| 17 | +import TSCBasic |
| 18 | + |
| 19 | +extension SwiftPackageTool { |
| 20 | + struct Install: SwiftCommand { |
| 21 | + static let configuration = CommandConfiguration( |
| 22 | + commandName: "experimental-install", |
| 23 | + abstract: "Offers the ability to install executable products of the current package." |
| 24 | + ) |
| 25 | + |
| 26 | + @OptionGroup() |
| 27 | + var globalOptions: GlobalOptions |
| 28 | + |
| 29 | + @Option(help: "The name of the executable product to install") |
| 30 | + var product: String? |
| 31 | + |
| 32 | + func run(_ tool: SwiftTool) throws { |
| 33 | + let swiftpmBinDir = try tool.fileSystem.getOrCreateSwiftPMInstalledBinariesDirectory() |
| 34 | + |
| 35 | + let env = ProcessInfo.processInfo.environment |
| 36 | + |
| 37 | + if let path = env.path, !path.contains(swiftpmBinDir.pathString), !globalOptions.logging.quiet { |
| 38 | + tool.observabilityScope.emit( |
| 39 | + warning: """ |
| 40 | + PATH doesn't include \(swiftpmBinDir.pathString)! This means you won't be able to access \ |
| 41 | + the installed executables by default, and will need to specify the full path. |
| 42 | + """ |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + let alreadyExisting = (try? InstalledPackageProduct.installedProducts(tool.fileSystem)) ?? [] |
| 47 | + |
| 48 | + let workspace = try tool.getActiveWorkspace() |
| 49 | + let packageRoot = try tool.getPackageRoot() |
| 50 | + |
| 51 | + let packageGraph = try workspace.loadPackageGraph( |
| 52 | + rootPath: packageRoot, |
| 53 | + observabilityScope: tool.observabilityScope |
| 54 | + ) |
| 55 | + |
| 56 | + let possibleCandidates = packageGraph.rootPackages.flatMap(\.products) |
| 57 | + .filter { $0.type == .executable } |
| 58 | + |
| 59 | + let productToInstall: Product |
| 60 | + |
| 61 | + switch possibleCandidates.count { |
| 62 | + case 0: |
| 63 | + throw StringError("No Executable Products in Package.swift.") |
| 64 | + case 1: |
| 65 | + productToInstall = possibleCandidates[0].underlyingProduct |
| 66 | + default: |
| 67 | + guard let product, let first = possibleCandidates.first(where: { $0.name == product }) else { |
| 68 | + throw StringError( |
| 69 | + """ |
| 70 | + Multiple candidates found, however, no product was specified. Specify a product with the \ |
| 71 | + `--product` option |
| 72 | + """ |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + productToInstall = first.underlyingProduct |
| 77 | + } |
| 78 | + |
| 79 | + if let existingPkg = alreadyExisting.first(where: { $0.name == productToInstall.name }) { |
| 80 | + throw StringError("\(productToInstall.name) is already installed at \(existingPkg.path)") |
| 81 | + } |
| 82 | + |
| 83 | + try tool.createBuildSystem(explicitProduct: productToInstall.name) |
| 84 | + .build(subset: .product(productToInstall.name)) |
| 85 | + |
| 86 | + let binPath = try tool.buildParameters().buildPath.appending(component: productToInstall.name) |
| 87 | + let finalBinPath = swiftpmBinDir.appending(component: binPath.basename) |
| 88 | + try tool.fileSystem.copy(from: binPath, to: finalBinPath) |
| 89 | + |
| 90 | + print("Executable product `\(productToInstall.name)` was successfully installed to \(finalBinPath).") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + struct Uninstall: SwiftCommand { |
| 95 | + static let configuration = CommandConfiguration( |
| 96 | + commandName: "experimental-uninstall", |
| 97 | + abstract: "Offers the ability to uninstall executable products of installed package products" |
| 98 | + ) |
| 99 | + |
| 100 | + @OptionGroup |
| 101 | + var globalOptions: GlobalOptions |
| 102 | + |
| 103 | + @Argument(help: "Name of the executable to uninstall.") |
| 104 | + var name: String |
| 105 | + |
| 106 | + func run(_ tool: SwiftTool) throws { |
| 107 | + let alreadyInstalled = (try? InstalledPackageProduct.installedProducts(tool.fileSystem)) ?? [] |
| 108 | + |
| 109 | + guard let removedExecutable = alreadyInstalled.first(where: { $0.name == name }) else { |
| 110 | + // The installed executable doesn't exist - let the user know, and stop here. |
| 111 | + throw StringError("No such installed executable as \(name)") |
| 112 | + } |
| 113 | + |
| 114 | + try tool.fileSystem.removeFileTree(removedExecutable.path) |
| 115 | + print("Executable product `\(self.name)` was successfully uninstalled from \(removedExecutable.path).") |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +private struct InstalledPackageProduct { |
| 121 | + static func installedProducts(_ fileSystem: FileSystem) throws -> [InstalledPackageProduct] { |
| 122 | + let binPath = try fileSystem.getOrCreateSwiftPMInstalledBinariesDirectory() |
| 123 | + |
| 124 | + let contents = ((try? fileSystem.getDirectoryContents(binPath)) ?? []) |
| 125 | + .map { binPath.appending($0) } |
| 126 | + |
| 127 | + return contents.map { path in |
| 128 | + InstalledPackageProduct(path: .init(path)) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// The name of this installed product, being the basename of the path. |
| 133 | + var name: String { |
| 134 | + self.path.basename |
| 135 | + } |
| 136 | + |
| 137 | + /// Path of the executable. |
| 138 | + let path: AbsolutePath |
| 139 | +} |
0 commit comments