|
| 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 Foundation |
| 17 | +import Workspace |
| 18 | + |
| 19 | +struct ShowExecutables: AsyncSwiftCommand { |
| 20 | + static let configuration = CommandConfiguration( |
| 21 | + abstract: "List the available executables from this package.") |
| 22 | + |
| 23 | + @OptionGroup(visibility: .hidden) |
| 24 | + var globalOptions: GlobalOptions |
| 25 | + |
| 26 | + @Option(help: "Set the output format.") |
| 27 | + var format: ShowExecutablesMode = .flatlist |
| 28 | + |
| 29 | + func run(_ swiftCommandState: SwiftCommandState) async throws { |
| 30 | + let packageGraph = try await swiftCommandState.loadPackageGraph() |
| 31 | + let rootPackages = packageGraph.rootPackages.map { $0.identity } |
| 32 | + |
| 33 | + let executables = packageGraph.allProducts.filter({ |
| 34 | + $0.type == .executable || $0.type == .snippet |
| 35 | + }).map { product -> Executable in |
| 36 | + if !rootPackages.contains(product.packageIdentity) { |
| 37 | + return Executable(package: product.packageIdentity.description, name: product.name) |
| 38 | + } else { |
| 39 | + return Executable(package: Optional<String>.none, name: product.name) |
| 40 | + } |
| 41 | + }.sorted(by: {$0.name < $1.name}) |
| 42 | + |
| 43 | + switch self.format { |
| 44 | + case .flatlist: |
| 45 | + for executable in executables { |
| 46 | + if let package = executable.package { |
| 47 | + print("\(executable.name) (\(package))") |
| 48 | + } else { |
| 49 | + print(executable.name) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + case .json: |
| 54 | + let encoder = JSONEncoder() |
| 55 | + let data = try encoder.encode(executables) |
| 56 | + if let output = String(data: data, encoding: .utf8) { |
| 57 | + print(output) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + struct Executable: Codable { |
| 63 | + var package: String? |
| 64 | + var name: String |
| 65 | + } |
| 66 | + |
| 67 | + enum ShowExecutablesMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable { |
| 68 | + case flatlist, json |
| 69 | + |
| 70 | + public init?(rawValue: String) { |
| 71 | + switch rawValue.lowercased() { |
| 72 | + case "flatlist": |
| 73 | + self = .flatlist |
| 74 | + case "json": |
| 75 | + self = .json |
| 76 | + default: |
| 77 | + return nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public var description: String { |
| 82 | + switch self { |
| 83 | + case .flatlist: return "flatlist" |
| 84 | + case .json: return "json" |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments