1
1
/*
2
2
This source file is part of the Swift.org open source project
3
-
3
+
4
4
Copyright 2020-2021 Apple Inc. and the Swift project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
-
6
+
7
7
See http://swift.org/LICENSE.txt for license information
8
8
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9
9
*/
@@ -67,22 +67,22 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
67
67
] ,
68
68
helpNames: [ . short, . long, . customLong( " help " , withSingleDash: true ) ]
69
69
)
70
-
70
+
71
71
public init ( ) { }
72
-
72
+
73
73
// MARK: Collections
74
-
74
+
75
75
struct List : ParsableCommand {
76
76
static let configuration = CommandConfiguration ( abstract: " List configured collections " )
77
-
77
+
78
78
@OptionGroup
79
79
var jsonOptions : JSONOptions
80
-
80
+
81
81
mutating func run( ) throws {
82
82
let collections = try with { collections in
83
83
try tsc_await { collections. listCollections ( identifiers: nil , callback: $0) }
84
84
}
85
-
85
+
86
86
if self . jsonOptions. json {
87
87
try JSONEncoder . makeWithDefaults ( ) . print ( collections)
88
88
} else {
@@ -92,36 +92,36 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
92
92
}
93
93
}
94
94
}
95
-
95
+
96
96
struct Refresh : ParsableCommand {
97
97
static let configuration = CommandConfiguration ( abstract: " Refresh configured collections " )
98
-
98
+
99
99
mutating func run( ) throws {
100
100
let collections = try with { collections in
101
101
try tsc_await { collections. refreshCollections ( callback: $0) }
102
102
}
103
103
print ( " Refreshed \( collections. count) configured package collection \( collections. count == 1 ? " " : " s " ) . " )
104
104
}
105
105
}
106
-
106
+
107
107
struct Add : ParsableCommand {
108
108
static let configuration = CommandConfiguration ( abstract: " Add a new collection " )
109
-
109
+
110
110
@Argument ( help: " URL of the collection to add " )
111
111
var collectionURL : String
112
-
112
+
113
113
@Option ( name: . long, help: " Sort order for the added collection " )
114
114
var order : Int ?
115
-
115
+
116
116
@Flag ( name: . long, help: " Trust the collection even if it is unsigned " )
117
117
var trustUnsigned : Bool = false
118
-
118
+
119
119
@Flag ( name: . long, help: " Skip signature check if the collection is signed " )
120
120
var skipSignatureCheck : Bool = false
121
-
121
+
122
122
mutating func run( ) throws {
123
123
let collectionURL = try url ( self . collectionURL)
124
-
124
+
125
125
let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionURL, skipSignatureCheck: self . skipSignatureCheck)
126
126
let collection : PackageCollectionsModel . Collection = try with { collections in
127
127
do {
@@ -144,20 +144,20 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
144
144
throw CollectionsError . missingSignature
145
145
}
146
146
}
147
-
147
+
148
148
print ( " Added \" \( collection. name) \" to your package collections. " )
149
149
}
150
150
}
151
-
151
+
152
152
struct Remove : ParsableCommand {
153
153
static let configuration = CommandConfiguration ( abstract: " Remove a configured collection " )
154
-
154
+
155
155
@Argument ( help: " URL of the collection to remove " )
156
156
var collectionURL : String
157
-
157
+
158
158
mutating func run( ) throws {
159
159
let collectionURL = try url ( self . collectionURL)
160
-
160
+
161
161
let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionURL)
162
162
try with { collections in
163
163
let collection = try tsc_await { collections. getCollection ( source, callback: $0) }
@@ -166,115 +166,115 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
166
166
}
167
167
}
168
168
}
169
-
169
+
170
170
// MARK: Search
171
-
171
+
172
172
enum SearchMethod : String , EnumerableFlag {
173
173
case keywords
174
174
case module
175
175
}
176
-
176
+
177
177
struct Search : ParsableCommand {
178
178
static var configuration = CommandConfiguration ( abstract: " Search for packages by keywords or module names " )
179
-
179
+
180
180
@OptionGroup
181
181
var jsonOptions : JSONOptions
182
-
182
+
183
183
@Flag ( help: " Pick the method for searching " )
184
184
var searchMethod : SearchMethod
185
-
185
+
186
186
@Argument ( help: " Search query " )
187
187
var searchQuery : String
188
-
188
+
189
189
mutating func run( ) throws {
190
190
try with { collections in
191
191
switch searchMethod {
192
192
case . keywords:
193
193
let results = try tsc_await { collections. findPackages ( searchQuery, collections: nil , callback: $0) }
194
-
194
+
195
195
if jsonOptions. json {
196
196
try JSONEncoder . makeWithDefaults ( ) . print ( results. items)
197
197
} else {
198
198
results. items. forEach {
199
- print ( " \( $0. package . repository . url ) : \( $0. package . summary ?? " " ) " )
199
+ print ( " \( $0. package . identity ) : \( $0. package . summary ?? " " ) " )
200
200
}
201
201
}
202
-
202
+
203
203
case . module:
204
204
let results = try tsc_await { collections. findTargets ( searchQuery, searchType: . exactMatch, collections: nil , callback: $0) }
205
-
205
+
206
206
let packages = Set ( results. items. flatMap { $0. packages } )
207
207
if jsonOptions. json {
208
208
try JSONEncoder . makeWithDefaults ( ) . print ( packages)
209
209
} else {
210
210
packages. forEach {
211
- print ( " \( $0. repository . url ) : \( $0. summary ?? " " ) " )
211
+ print ( " \( $0. identity ) : \( $0. summary ?? " " ) " )
212
212
}
213
213
}
214
214
}
215
215
}
216
216
}
217
217
}
218
-
218
+
219
219
// MARK: Packages
220
-
220
+
221
221
struct Describe : ParsableCommand {
222
222
static var configuration = CommandConfiguration ( abstract: " Get metadata for a collection or a package included in an imported collection " )
223
-
223
+
224
224
@OptionGroup
225
225
var jsonOptions : JSONOptions
226
-
226
+
227
227
@Argument ( help: " URL of the package or collection to get information for " )
228
228
var packageURL : String
229
-
229
+
230
230
@Option ( name: . long, help: " Version of the package to get information for " )
231
231
var version : String ?
232
-
232
+
233
233
private func printVersion( _ version: PackageCollectionsModel . Package . Version ? ) -> String ? {
234
234
guard let version = version else {
235
235
return nil
236
236
}
237
237
guard let defaultManifest = version. defaultManifest else {
238
238
return nil
239
239
}
240
-
240
+
241
241
let manifests = version. manifests. values. filter { $0. toolsVersion != version. defaultToolsVersion } . map { printManifest ( $0) } . joined ( separator: " \n " )
242
242
let compatibility = optionalRow (
243
243
" Verified Compatibility (Platform, Swift Version) " ,
244
244
version. verifiedCompatibility? . map { " ( \( $0. platform. name) , \( $0. swiftVersion. rawValue) ) " } . joined ( separator: " , " )
245
245
)
246
246
let license = optionalRow ( " License " , version. license? . type. description)
247
-
247
+
248
248
return """
249
249
\( version. version)
250
250
\( self . printManifest ( defaultManifest) ) \( manifests) \( compatibility) \( license)
251
251
"""
252
252
}
253
-
253
+
254
254
private func printManifest( _ manifest: PackageCollectionsModel . Package . Version . Manifest ) -> String {
255
255
let modules = manifest. targets. compactMap { $0. moduleName } . joined ( separator: " , " )
256
256
let products = optionalRow ( " Products " , manifest. products. isEmpty ? nil : manifest. products. compactMap { $0. name } . joined ( separator: " , " ) , indentationLevel: 3 )
257
-
257
+
258
258
return """
259
259
Tools Version: \( manifest. toolsVersion. description)
260
260
Package Name: \( manifest. packageName)
261
261
Modules: \( modules) \( products)
262
262
"""
263
263
}
264
-
264
+
265
265
mutating func run( ) throws {
266
266
try with { collections in
267
267
let identity = PackageIdentity ( url: packageURL)
268
268
let reference = PackageReference . remote ( identity: identity, location: packageURL)
269
-
269
+
270
270
do { // assume URL is for a package in an imported collection
271
271
let result = try tsc_await { collections. getPackageMetadata ( reference, callback: $0) }
272
-
272
+
273
273
if let versionString = version {
274
274
guard let version = TSCUtility . Version ( versionString) , let result = result. package . versions. first ( where: { $0. version == version } ) , let printedResult = printVersion ( result) else {
275
275
throw CollectionsError . invalidVersionString ( versionString)
276
276
}
277
-
277
+
278
278
if jsonOptions. json {
279
279
try JSONEncoder . makeWithDefaults ( ) . print ( result)
280
280
} else {
@@ -289,7 +289,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
289
289
let license = optionalRow ( " License " , result. package . license. map { " \( $0. type) ( \( $0. url) ) " } )
290
290
let languages = optionalRow ( " Languages " , result. package . languages? . joined ( separator: " , " ) )
291
291
let latestVersion = optionalRow ( " \( String ( repeating: " - " , count: 60 ) ) \n \( indent ( ) ) Latest Version " , printVersion ( result. package . latestVersion) )
292
-
292
+
293
293
if jsonOptions. json {
294
294
try JSONEncoder . makeWithDefaults ( ) . print ( result. package )
295
295
} else {
@@ -304,23 +304,23 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
304
304
if version != nil {
305
305
throw error
306
306
}
307
-
307
+
308
308
let collectionURL = try url ( self . packageURL)
309
-
309
+
310
310
do {
311
311
let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionURL)
312
312
let collection = try tsc_await { collections. getCollection ( source, callback: $0) }
313
-
313
+
314
314
let description = optionalRow ( " Description " , collection. overview)
315
315
let keywords = optionalRow ( " Keywords " , collection. keywords? . joined ( separator: " , " ) )
316
316
let createdAt = optionalRow ( " Created At " , DateFormatter ( ) . string ( from: collection. createdAt) )
317
- let packages = collection. packages. map { " \( $0. repository . url ) " } . joined ( separator: " \n \( indent ( levels: 2 ) ) " )
318
-
317
+ let packages = collection. packages. map { " \( $0. identity ) " } . joined ( separator: " \n \( indent ( levels: 2 ) ) " )
318
+
319
319
if jsonOptions. json {
320
320
try JSONEncoder . makeWithDefaults ( ) . print ( collection)
321
321
} else {
322
322
let signature = optionalRow ( " Signed By " , collection. signature. map { " \( $0. certificate. subject. commonName ?? " Unspecified " ) ( \( $0. isVerified ? " " : " not " ) verified) " } )
323
-
323
+
324
324
print ( """
325
325
Name: \( collection. name)
326
326
Source: \( collection. source. url) \( description) \( keywords) \( createdAt)
@@ -367,10 +367,10 @@ private extension ParsableCommand {
367
367
Self . exit ( withError: error)
368
368
}
369
369
}
370
-
370
+
371
371
return try handler ( collections)
372
372
}
373
-
373
+
374
374
func url( _ urlString: String ) throws -> Foundation . URL {
375
375
guard let url = URL ( string: urlString) else {
376
376
let filePrefix = " file:// "
0 commit comments