-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Show traits package subcommand #9213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cmcgee1024
wants to merge
6
commits into
swiftlang:main
Choose a base branch
from
cmcgee1024:show_traits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+394
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
32b8da6
Add a show-traits package subcommand to showing available traits and …
cmcgee1024 4a53d44
Add documentation for the show-traits subcommand
cmcgee1024 b0b02e2
Update Sources/Commands/PackageCommands/ShowTraits.swift
cmcgee1024 903f7b7
Rename flatlist to text, and make JSON encoding stable with sorted keys
cmcgee1024 5a67777
Merge branch 'show_traits' of github.com:cmcgee1024/swift-package-man…
cmcgee1024 a6eb3bd
Code review feedback
cmcgee1024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import ArgumentParser | ||
import Basics | ||
import CoreCommands | ||
import Foundation | ||
import PackageModel | ||
import PackageGraph | ||
import Workspace | ||
|
||
struct ShowTraits: AsyncSwiftCommand { | ||
static let configuration = CommandConfiguration( | ||
abstract: "List the available traits for a package.") | ||
|
||
@OptionGroup(visibility: .hidden) | ||
var globalOptions: GlobalOptions | ||
|
||
@Option(help: "Show traits for any package id in the transitive dependencies.") | ||
var packageId: String? | ||
|
||
@Option(help: "Set the output format.") | ||
var format: ShowTraitsMode = .text | ||
|
||
func run(_ swiftCommandState: SwiftCommandState) async throws { | ||
let packageGraph = try await swiftCommandState.loadPackageGraph() | ||
|
||
let traits = if let packageId { | ||
packageGraph.packages.filter({ $0.identity.description == packageId }).flatMap( { $0.manifest.traits } ).sorted(by: {$0.name < $1.name} ) | ||
} else { | ||
packageGraph.rootPackages.flatMap( { $0.manifest.traits } ).sorted(by: {$0.name < $1.name} ) | ||
} | ||
|
||
switch self.format { | ||
case .text: | ||
let defaultTraits = traits.filter( { $0.isDefault } ).flatMap( { $0.enabledTraits }) | ||
|
||
for trait in traits { | ||
guard !trait.isDefault else { | ||
continue | ||
} | ||
|
||
print("\(trait.name)\(trait.description ?? "" != "" ? " - " + trait.description! : "")\(defaultTraits.contains(trait.name) ? " (default)" : "")") | ||
} | ||
|
||
case .json: | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = .sortedKeys | ||
|
||
let data = try encoder.encode(traits) | ||
if let output = String(data: data, encoding: .utf8) { | ||
print(output) | ||
} | ||
} | ||
} | ||
|
||
enum ShowTraitsMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable { | ||
case text, json | ||
|
||
public init?(rawValue: String) { | ||
switch rawValue.lowercased() { | ||
case "text": | ||
self = .text | ||
case "json": | ||
self = .json | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
public var description: String { | ||
switch self { | ||
case .text: return "text" | ||
case .json: return "json" | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.