|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 PackageModel |
| 18 | +import PackageGraph |
| 19 | +import Workspace |
| 20 | + |
| 21 | +struct ShowTraits: AsyncSwiftCommand { |
| 22 | + static let configuration = CommandConfiguration( |
| 23 | + abstract: "List the available traits for a package.") |
| 24 | + |
| 25 | + @OptionGroup(visibility: .hidden) |
| 26 | + var globalOptions: GlobalOptions |
| 27 | + |
| 28 | + @Option(help: "Show traits for any package id in the transitive dependencies.") |
| 29 | + var packageId: String? |
| 30 | + |
| 31 | + @Option(help: "Set the output format.") |
| 32 | + var format: ShowTraitsMode = .text |
| 33 | + |
| 34 | + func run(_ swiftCommandState: SwiftCommandState) async throws { |
| 35 | + let packageGraph = try await swiftCommandState.loadPackageGraph() |
| 36 | + |
| 37 | + let traits = if let packageId { |
| 38 | + packageGraph.packages.filter({ $0.identity.description == packageId }).flatMap( { $0.manifest.traits } ).sorted(by: {$0.name < $1.name} ) |
| 39 | + } else { |
| 40 | + packageGraph.rootPackages.flatMap( { $0.manifest.traits } ).sorted(by: {$0.name < $1.name} ) |
| 41 | + } |
| 42 | + |
| 43 | + switch self.format { |
| 44 | + case .text: |
| 45 | + let defaultTraits = traits.filter( { $0.isDefault } ).flatMap( { $0.enabledTraits }) |
| 46 | + |
| 47 | + for trait in traits { |
| 48 | + guard !trait.isDefault else { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + print("\(trait.name)\(trait.description ?? "" != "" ? " - " + trait.description! : "")\(defaultTraits.contains(trait.name) ? " (default)" : "")") |
| 53 | + } |
| 54 | + |
| 55 | + case .json: |
| 56 | + let encoder = JSONEncoder() |
| 57 | + encoder.outputFormatting = .sortedKeys |
| 58 | + |
| 59 | + let data = try encoder.encode(traits) |
| 60 | + if let output = String(data: data, encoding: .utf8) { |
| 61 | + print(output) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + enum ShowTraitsMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable { |
| 67 | + case text, json |
| 68 | + |
| 69 | + public init?(rawValue: String) { |
| 70 | + switch rawValue.lowercased() { |
| 71 | + case "text": |
| 72 | + self = .text |
| 73 | + case "json": |
| 74 | + self = .json |
| 75 | + default: |
| 76 | + return nil |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public var description: String { |
| 81 | + switch self { |
| 82 | + case .text: return "text" |
| 83 | + case .json: return "json" |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments