1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2022 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 Commands
import CoreCommands
import Foundation
import PackageCollections
import PackageModel
import struct TSCUtility.Version
private enum CollectionsError: Swift.Error {
case invalidArgument(String)
case invalidVersionString(String)
case unsigned
case cannotVerifySignature
case invalidSignature
case missingSignature
}
// FIXME: add links to docs in error messages
extension CollectionsError: CustomStringConvertible {
var description: String {
switch self {
case .invalidArgument(let argumentName):
return "Invalid argument '\(argumentName)'"
case .invalidVersionString(let versionString):
return "Invalid version string '\(versionString)'"
case .unsigned:
return "The collection is not signed. If you would still like to add it please rerun 'add' with '--trust-unsigned'."
case .cannotVerifySignature:
return "The collection's signature cannot be verified due to missing configuration. Please refer to documentations on how to set up trusted root certificates or rerun command with '--skip-signature-check'."
case .invalidSignature:
return "The collection's signature is invalid. If you would like to continue please rerun command with '--skip-signature-check'."
case .missingSignature:
return "The collection is missing required signature, which means it might have been compromised. Please contact the collection's authors and alert them of the issue."
}
}
}
struct JSONOptions: ParsableArguments {
@Flag(name: .long, help: "Output as JSON")
var json: Bool = false
}
public struct PackageCollectionsCommand: AsyncParsableCommand {
public static var configuration = CommandConfiguration(
commandName: "package-collection",
_superCommandName: "swift",
abstract: "Interact with package collections",
discussion: "SEE ALSO: swift build, swift package, swift run, swift test",
version: SwiftVersion.current.completeDisplayString,
subcommands: [
Add.self,
Describe.self,
List.self,
Refresh.self,
Remove.self,
Search.self,
],
helpNames: [.short, .long, .customLong("help", withSingleDash: true)]
)
public init() {}
// MARK: Collections
struct List: AsyncSwiftCommand {
static let configuration = CommandConfiguration(abstract: "List configured collections")
@OptionGroup
var jsonOptions: JSONOptions
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
func run(_ swiftCommandState: SwiftCommandState) async throws {
let collections = try await withState(swiftCommandState) { collections in
try await collections.listCollections(identifiers: nil)
}
if self.jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(collections)
} else {
collections.forEach {
print("\($0.name) - \($0.source.url)")
}
}
}
}
struct Refresh: AsyncSwiftCommand {
static let configuration = CommandConfiguration(abstract: "Refresh configured collections")
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
func run(_ swiftCommandState: SwiftCommandState) async throws {
let collections = try await withState(swiftCommandState) { collections in
try await collections.refreshCollections()
}
print("Refreshed \(collections.count) configured package collection\(collections.count == 1 ? "" : "s").")
}
}
struct Add: AsyncSwiftCommand {
static let configuration = CommandConfiguration(abstract: "Add a new collection")
@Argument(help: "URL of the collection to add")
var collectionURL: String
@Option(name: .long, help: "Sort order for the added collection")
var order: Int?
@Flag(name: .long, help: "Trust the collection even if it is unsigned")
var trustUnsigned: Bool = false
@Flag(name: .long, help: "Skip signature check if the collection is signed")
var skipSignatureCheck: Bool = false
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
func run(_ swiftCommandState: SwiftCommandState) async throws {
let collectionURL = try url(self.collectionURL)
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL, skipSignatureCheck: self.skipSignatureCheck)
let collection: PackageCollectionsModel.Collection = try await withState(swiftCommandState) { collections in
do {
let userTrusted = self.trustUnsigned
return try await collections.addCollection(source, order: order) { _, callback in
callback(userTrusted)
}
} catch PackageCollectionError.trustConfirmationRequired, PackageCollectionError.untrusted {
throw CollectionsError.unsigned
} catch PackageCollectionError.cannotVerifySignature {
throw CollectionsError.cannotVerifySignature
} catch PackageCollectionError.invalidSignature {
throw CollectionsError.invalidSignature
} catch PackageCollectionError.missingSignature {
throw CollectionsError.missingSignature
}
}
print("Added \"\(collection.name)\" to your package collections.")
}
}
struct Remove: AsyncSwiftCommand {
static let configuration = CommandConfiguration(abstract: "Remove a configured collection")
@Argument(help: "URL of the collection to remove")
var collectionURL: String
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
func run(_ swiftCommandState: SwiftCommandState) async throws {
let collectionURL = try url(self.collectionURL)
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL)
try await withState(swiftCommandState) { collections in
let collection = try await collections.getCollection(source)
_ = try await collections.removeCollection(source)
print("Removed \"\(collection.name)\" from your package collections.")
}
}
}
// MARK: Search
enum SearchMethod: String, EnumerableFlag {
case keywords
case module
}
struct Search: AsyncSwiftCommand {
static var configuration = CommandConfiguration(abstract: "Search for packages by keywords or module names")
@OptionGroup
var jsonOptions: JSONOptions
@Flag(help: "Pick the method for searching")
var searchMethod: SearchMethod
@Argument(help: "Search query")
var searchQuery: String
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
func run(_ swiftCommandState: SwiftCommandState) async throws {
try await withState(swiftCommandState) { collections in
switch searchMethod {
case .keywords:
let results = try await collections.findPackages(searchQuery, collections: nil)
if jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(results.items)
} else {
results.items.forEach {
print("\($0.package.identity): \($0.package.summary ?? "")")
}
}
case .module:
let results = try await collections.findTargets(searchQuery, searchType: .exactMatch, collections: nil)
let packages = Set(results.items.flatMap { $0.packages })
if jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(packages)
} else {
packages.forEach {
print("\($0.identity): \($0.summary ?? "")")
}
}
}
}
}
}
// MARK: Packages
struct Describe: AsyncSwiftCommand {
static var configuration = CommandConfiguration(abstract: "Get metadata for a collection or a package included in an imported collection")
@OptionGroup
var jsonOptions: JSONOptions
@Argument(help: "URL of the package or collection to get information for")
var packageURL: String
@Option(name: .long, help: "Version of the package to get information for")
var version: String?
@Flag(name: .long, help: "Skip signature check if the collection is signed")
var skipSignatureCheck: Bool = false
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
private func printVersion(_ version: PackageCollectionsModel.Package.Version?) -> String? {
guard let version else {
return nil
}
guard let defaultManifest = version.defaultManifest else {
return nil
}
let manifests = version.manifests.values.filter { $0.toolsVersion != version.defaultToolsVersion }.map { printManifest($0) }.joined(separator: "\n")
let compatibility = optionalRow(
"Verified Compatibility (Platform, Swift Version)",
version.verifiedCompatibility?.map { "(\($0.platform.name), \($0.swiftVersion.rawValue))" }.joined(separator: ", ")
)
let license = optionalRow("License", version.license?.type.description)
return """
\(version.version)
\(self.printManifest(defaultManifest))\(manifests)\(compatibility)\(license)
"""
}
private func printManifest(_ manifest: PackageCollectionsModel.Package.Version.Manifest) -> String {
let modules = manifest.targets.compactMap { $0.moduleName }.joined(separator: ", ")
let products = optionalRow("Products", manifest.products.isEmpty ? nil : manifest.products.compactMap { $0.name }.joined(separator: ", "), indentationLevel: 3)
return """
Tools Version: \(manifest.toolsVersion.description)
Package Name: \(manifest.packageName)
Modules: \(modules)\(products)
"""
}
func run(_ swiftCommandState: SwiftCommandState) async throws {
try await withState(swiftCommandState) { collections in
let identity = PackageIdentity(urlString: self.packageURL)
do { // assume URL is for a package in an imported collection
let result = try await collections.getPackageMetadata(identity: identity, location: self.packageURL)
if let versionString = version {
guard let version = TSCUtility.Version(versionString), let result = result.package.versions.first(where: { $0.version == version }), let printedResult = printVersion(result) else {
throw CollectionsError.invalidVersionString(versionString)
}
if jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(result)
} else {
print("\(indent())Version: \(printedResult)")
}
} else {
let description = optionalRow("Description", result.package.summary)
let versions = result.package.versions.map { "\($0.version)" }.joined(separator: ", ")
let stars = optionalRow("Stars", result.package.watchersCount?.description)
let readme = optionalRow("Readme", result.package.readmeURL?.absoluteString)
let authors = optionalRow("Authors", result.package.authors?.map { $0.username }.joined(separator: ", "))
let license = optionalRow("License", result.package.license.map { "\($0.type) (\($0.url))" })
let languages = optionalRow("Languages", result.package.languages?.joined(separator: ", "))
let latestVersion = optionalRow("\(String(repeating: "-", count: 60))\n\(indent())Latest Version", printVersion(result.package.latestVersion))
if jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(result.package)
} else {
print("""
\(description)
Available Versions: \(versions)\(readme)\(license)\(authors)\(stars)\(languages)\(latestVersion)
""")
}
}
} catch { // assume URL is for a collection
// If a version argument was given, we do not perform the fallback.
if version != nil {
throw error
}
let collectionURL = try url(self.packageURL)
do {
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL, skipSignatureCheck: self.skipSignatureCheck)
let collection = try await collections.getCollection(source)
let description = optionalRow("Description", collection.overview)
let keywords = optionalRow("Keywords", collection.keywords?.joined(separator: ", "))
let createdAt = optionalRow("Created At", DateFormatter().string(from: collection.createdAt))
let packages = collection.packages.map { "\($0.identity)" }.joined(separator: "\n\(indent(levels: 2))")
if jsonOptions.json {
try JSONEncoder.makeWithDefaults().print(collection)
} else {
let signature = optionalRow("Signed By", collection.signature.map { "\($0.certificate.subject.commonName ?? "Unspecified") (\($0.isVerified ? "" : "not ")verified)" })
print("""
Name: \(collection.name)
Source: \(collection.source.url)\(description)\(keywords)\(createdAt)
Packages:
\(packages)\(signature)
""")
}
} catch PackageCollectionError.cannotVerifySignature {
throw CollectionsError.cannotVerifySignature
} catch PackageCollectionError.invalidSignature {
throw CollectionsError.invalidSignature
} catch {
print("Failed to get metadata. The given URL either belongs to a collection that is invalid or unavailable, or a package that is not found in any of the imported collections.")
}
}
}
}
}
}
private func indent(levels: Int = 1) -> String {
return String(repeating: " ", count: levels)
}
private func optionalRow(_ title: String, _ contents: String?, indentationLevel: Int = 1) -> String {
if let contents, !contents.isEmpty {
return "\n\(indent(levels: indentationLevel))\(title): \(contents)"
} else {
return ""
}
}
private extension JSONEncoder {
func print<T>(_ value: T) throws where T: Encodable {
let jsonData = try self.encode(value)
let jsonString = String(decoding: jsonData, as: UTF8.self)
Swift.print(jsonString)
}
}
private extension ParsableCommand {
func withState<T>(
_ swiftCommandState: SwiftCommandState,
handler: (_ collections: PackageCollectionsProtocol) async throws -> T
) async throws -> T {
_ = try? swiftCommandState.getActiveWorkspace(emitDeprecatedConfigurationWarning: true)
let collections = PackageCollections(
configuration: .init(
configurationDirectory: swiftCommandState.sharedConfigurationDirectory,
cacheDirectory: swiftCommandState.sharedCacheDirectory
),
fileSystem: swiftCommandState.fileSystem,
observabilityScope: swiftCommandState.observabilityScope
)
defer {
do {
try collections.shutdown()
} catch {
Self.exit(withError: error)
}
}
return try await handler(collections)
}
func url(_ urlString: String) throws -> URL {
guard let url = URL(string: urlString) else {
let filePrefix = "file://"
guard urlString.hasPrefix(filePrefix) else {
throw CollectionsError.invalidArgument("collectionURL")
}
// URL(fileURLWithPath:) can handle whitespaces in path
return URL(fileURLWithPath: String(urlString.dropFirst(filePrefix.count)))
}
return url
}
}
|