@@ -36,19 +36,6 @@ struct JSONOptions: ParsableArguments {
36
36
var json : Bool = false
37
37
}
38
38
39
- struct ProfileOptions : ParsableArguments {
40
- @Option ( name: . long, help: " Profile to use for the given command " )
41
- var profile : String ?
42
-
43
- var usedProfile : PackageCollectionsModel . Profile ? {
44
- if let profile = profile {
45
- return . init( name: profile)
46
- } else {
47
- return nil
48
- }
49
- }
50
- }
51
-
52
39
public struct SwiftPackageCollectionsTool : ParsableCommand {
53
40
public static var configuration = CommandConfiguration (
54
41
commandName: " package-collections " ,
@@ -59,9 +46,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
59
46
subcommands: [
60
47
Add . self,
61
48
Describe . self,
62
- DescribeCollection . self,
63
49
List . self,
64
- ProfileList . self,
65
50
Refresh . self,
66
51
Remove . self,
67
52
Search . self
@@ -71,27 +56,6 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
71
56
public init ( ) {
72
57
}
73
58
74
- // MARK: Profiles
75
-
76
- struct ProfileList : ParsableCommand {
77
- static let configuration = CommandConfiguration ( abstract: " List configured profiles " )
78
-
79
- @OptionGroup
80
- var jsonOptions : JSONOptions
81
-
82
- mutating func run( ) throws {
83
- let profiles : [ PackageCollectionsModel . Profile ] = try tsc_await { self . collections. listProfiles ( callback: $0) }
84
-
85
- if jsonOptions. json {
86
- try JSONEncoder ( ) . print ( profiles)
87
- } else {
88
- profiles. forEach {
89
- print ( $0)
90
- }
91
- }
92
- }
93
- }
94
-
95
59
// MARK: Collections
96
60
97
61
struct List : ParsableCommand {
@@ -100,11 +64,10 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
100
64
@OptionGroup
101
65
var jsonOptions : JSONOptions
102
66
103
- @OptionGroup
104
- var profileOptions : ProfileOptions
105
-
106
67
mutating func run( ) throws {
107
- let collections = try tsc_await { self . collections. listCollections ( identifiers: nil , in: profileOptions. usedProfile, callback: $0) }
68
+ let collections = try with { collections in
69
+ return try tsc_await { collections. listCollections ( identifiers: nil , callback: $0) }
70
+ }
108
71
109
72
if jsonOptions. json {
110
73
try JSONEncoder ( ) . print ( collections)
@@ -119,11 +82,10 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
119
82
struct Refresh : ParsableCommand {
120
83
static let configuration = CommandConfiguration ( abstract: " Refresh configured collections " )
121
84
122
- @OptionGroup
123
- var profileOptions : ProfileOptions
124
-
125
85
mutating func run( ) throws {
126
- let collections = try tsc_await { self . collections. refreshCollections ( in: profileOptions. usedProfile, callback: $0) }
86
+ let collections = try with { collections in
87
+ return try tsc_await { collections. refreshCollections ( callback: $0) }
88
+ }
127
89
print ( " Refreshed \( collections. count) configured package collections. " )
128
90
}
129
91
}
@@ -137,16 +99,15 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
137
99
@Option ( name: . long, help: " Sort order for the added collection " )
138
100
var order : Int ?
139
101
140
- @OptionGroup
141
- var profileOptions : ProfileOptions
142
-
143
102
mutating func run( ) throws {
144
103
guard let collectionUrl = URL ( string: collectionUrl) else {
145
104
throw CollectionsError . invalidArgument ( " collectionUrl " )
146
105
}
147
106
148
- let source = PackageCollectionsModel . CollectionSource ( type: . feed, url: collectionUrl)
149
- let collection = try tsc_await { self . collections. addCollection ( source, order: order, to: profileOptions. usedProfile, callback: $0) }
107
+ let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionUrl)
108
+ let collection = try with { collections in
109
+ return try tsc_await { collections. addCollection ( source, order: order, callback: $0) }
110
+ }
150
111
151
112
print ( " Added \" \( collection. name) \" to your package collections. " )
152
113
}
@@ -155,9 +116,6 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
155
116
struct Remove : ParsableCommand {
156
117
static let configuration = CommandConfiguration ( abstract: " Remove a configured collection " )
157
118
158
- @OptionGroup
159
- var profileOptions : ProfileOptions
160
-
161
119
@Argument ( help: " URL of the collection to remove " )
162
120
var collectionUrl : String
163
121
@@ -166,43 +124,13 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
166
124
throw CollectionsError . invalidArgument ( " collectionUrl " )
167
125
}
168
126
169
- let source = PackageCollectionsModel . CollectionSource ( type: . feed, url: collectionUrl)
170
- let collection = try tsc_await { self . collections. getCollection ( source, callback: $0) }
171
-
172
- _ = try tsc_await { self . collections. removeCollection ( source, from: profileOptions. usedProfile, callback: $0) }
173
- print ( " Removed \" \( collection. name) \" from your package collections. " )
174
- }
175
- }
176
-
177
- struct DescribeCollection : ParsableCommand {
178
- static let configuration = CommandConfiguration ( abstract: " Get metadata for a configured collection " )
179
-
180
- @Argument ( help: " URL of the collection to describe " )
181
- var collectionUrl : String
182
-
183
- @OptionGroup
184
- var profileOptions : ProfileOptions
185
-
186
- mutating func run( ) throws {
187
- guard let collectionUrl = URL ( string: collectionUrl) else {
188
- throw CollectionsError . invalidArgument ( " collectionUrl " )
127
+ let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionUrl)
128
+ let collection = try with { collections in
129
+ return try tsc_await { collections. getCollection ( source, callback: $0) }
189
130
}
190
131
191
- let source = PackageCollectionsModel . CollectionSource ( type: . feed, url: collectionUrl)
192
- let collection = try tsc_await { self . collections. getCollection ( source, callback: $0) }
193
-
194
- let description = optionalRow ( " Description " , collection. description)
195
- let keywords = optionalRow ( " Keywords " , collection. keywords? . joined ( separator: " , " ) )
196
- let createdAt = DateFormatter ( ) . string ( from: collection. createdAt)
197
- let packages = collection. packages. map { " \( $0. repository. url) " } . joined ( separator: " \n " )
198
-
199
- print ( """
200
- Name: \( collection. name)
201
- Source: \( collection. source. url) \( description) \( keywords)
202
- Created At: \( createdAt)
203
- Packages:
204
- \( packages)
205
- """ )
132
+ _ = try with { collections in try tsc_await { collections. removeCollection ( source, callback: $0) } }
133
+ print ( " Removed \" \( collection. name) \" from your package collections. " )
206
134
}
207
135
}
208
136
@@ -219,9 +147,6 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
219
147
@OptionGroup
220
148
var jsonOptions : JSONOptions
221
149
222
- @OptionGroup
223
- var profileOptions : ProfileOptions
224
-
225
150
@Flag ( help: " Pick the method for searching " )
226
151
var searchMethod : SearchMethod
227
152
@@ -231,18 +156,22 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
231
156
mutating func run( ) throws {
232
157
switch searchMethod {
233
158
case . keywords:
234
- let results = try tsc_await { collections. findPackages ( searchQuery, collections: nil , profile: profileOptions. usedProfile, callback: $0) }
159
+ let results = try with { collections in
160
+ return try tsc_await { collections. findPackages ( searchQuery, collections: nil , callback: $0) }
161
+ }
235
162
236
163
results. items. forEach {
237
164
print ( " \( $0. package . repository. url) : \( $0. package . summary ?? " " ) " )
238
165
}
239
166
240
167
case . module:
241
- let results = try tsc_await { collections. findTargets ( searchQuery, searchType: . exactMatch, collections: nil , profile: profileOptions. usedProfile, callback: $0) }
168
+ let results = try with { collections in
169
+ return try tsc_await { collections. findTargets ( searchQuery, searchType: . exactMatch, collections: nil , callback: $0) }
170
+ }
242
171
243
172
let packages = Set ( results. items. flatMap { $0. packages } )
244
173
packages. forEach {
245
- print ( " \( $0. repository. url) : \( $0. description ?? " " ) " )
174
+ print ( " \( $0. repository. url) : \( $0. summary ?? " " ) " )
246
175
}
247
176
}
248
177
}
@@ -251,15 +180,12 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
251
180
// MARK: Packages
252
181
253
182
struct Describe : ParsableCommand {
254
- static var configuration = CommandConfiguration ( abstract: " Get metadata for a single package " )
183
+ static var configuration = CommandConfiguration ( abstract: " Get metadata for a single package or collection " )
255
184
256
185
@OptionGroup
257
186
var jsonOptions : JSONOptions
258
187
259
- @OptionGroup
260
- var profileOptions : ProfileOptions
261
-
262
- @Argument ( help: " URL of the package to get information for " )
188
+ @Argument ( help: " URL of the package or collection to get information for " )
263
189
var packageUrl : String
264
190
265
191
@Option ( name: . long, help: " Version of the package to get information for " )
@@ -283,28 +209,59 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
283
209
}
284
210
285
211
mutating func run( ) throws {
286
- let identity = PackageReference . computeIdentity ( packageURL : packageUrl)
212
+ let identity = PackageIdentity ( url : packageUrl)
287
213
let reference = PackageReference ( identity: identity, path: packageUrl)
288
-
289
- let result = try tsc_await { self . collections. getPackageMetadata ( reference, profile: profileOptions. usedProfile, callback: $0) }
290
-
291
- if let versionString = version {
292
- guard let version = TSCUtility . Version ( string: versionString) , let result = result. package . versions. first ( where: { $0. version == version } ) , let printedResult = printVersion ( result) else {
293
- throw CollectionsError . invalidVersionString ( versionString)
214
+
215
+ do { // assume URL is for a package
216
+ let result = try with { collections in
217
+ return try tsc_await { collections. getPackageMetadata ( reference, callback: $0) }
294
218
}
295
-
296
- print ( " Version: \( printedResult) " )
297
- } else {
298
- let description = optionalRow ( " Description " , result. package . description)
299
- let versions = result. package . versions. map { " \( $0. version) " } . joined ( separator: " , " )
300
- let watchers = optionalRow ( " Watchers " , result. package . watchersCount? . description)
301
- let readme = optionalRow ( " Readme " , result. package . readmeURL? . absoluteString)
302
- let authors = optionalRow ( " Authors " , result. package . authors? . map { $0. username } . joined ( separator: " , " ) )
303
- let latestVersion = optionalRow ( " -------------------------------------------------------------- \n Latest Version " , printVersion ( result. package . latestVersion) )
304
-
219
+
220
+ if let versionString = version {
221
+ guard let version = TSCUtility . Version ( string: versionString) , let result = result. package . versions. first ( where: { $0. version == version } ) , let printedResult = printVersion ( result) else {
222
+ throw CollectionsError . invalidVersionString ( versionString)
223
+ }
224
+
225
+ print ( " Version: \( printedResult) " )
226
+ } else {
227
+ let description = optionalRow ( " Description " , result. package . summary)
228
+ let versions = result. package . versions. map { " \( $0. version) " } . joined ( separator: " , " )
229
+ let watchers = optionalRow ( " Watchers " , result. package . watchersCount? . description)
230
+ let readme = optionalRow ( " Readme " , result. package . readmeURL? . absoluteString)
231
+ let authors = optionalRow ( " Authors " , result. package . authors? . map { $0. username } . joined ( separator: " , " ) )
232
+ let latestVersion = optionalRow ( " -------------------------------------------------------------- \n Latest Version " , printVersion ( result. package . latestVersion) )
233
+
234
+ print ( """
235
+ \( description) Available Versions: \( versions) \( watchers) \( readme) \( authors) \( latestVersion)
236
+ """ )
237
+ }
238
+ } catch { // assume URL is for a collection
239
+ // If a version argument was given, we do not perform the fallback.
240
+ if version != nil {
241
+ throw error
242
+ }
243
+
244
+ guard let collectionUrl = URL ( string: packageUrl) else {
245
+ throw CollectionsError . invalidArgument ( " collectionUrl " )
246
+ }
247
+
248
+ let source = PackageCollectionsModel . CollectionSource ( type: . json, url: collectionUrl)
249
+ let collection = try with { collections in
250
+ try tsc_await { collections. getCollection ( source, callback: $0) }
251
+ }
252
+
253
+ let description = optionalRow ( " Description " , collection. overview)
254
+ let keywords = optionalRow ( " Keywords " , collection. keywords? . joined ( separator: " , " ) )
255
+ let createdAt = DateFormatter ( ) . string ( from: collection. createdAt)
256
+ let packages = collection. packages. map { " \( $0. repository. url) " } . joined ( separator: " \n " )
257
+
305
258
print ( """
306
- \( description) Available Versions: \( versions) \( watchers) \( readme) \( authors) \( latestVersion)
307
- """ )
259
+ Name: \( collection. name)
260
+ Source: \( collection. source. url) \( description) \( keywords)
261
+ Created At: \( createdAt)
262
+ Packages:
263
+ \( packages)
264
+ """ )
308
265
}
309
266
}
310
267
}
@@ -331,7 +288,16 @@ private extension JSONEncoder {
331
288
}
332
289
333
290
private extension ParsableCommand {
334
- var collections : PackageCollectionsProtocol {
335
- fatalError ( " not implemented " )
291
+ func with< T> ( handler: ( _ collections: PackageCollectionsProtocol ) throws -> T ) throws -> T {
292
+ let collections = PackageCollections ( )
293
+ defer {
294
+ do {
295
+ try collections. shutdown ( )
296
+ } catch {
297
+ Self . exit ( withError: error)
298
+ }
299
+ }
300
+
301
+ return try handler ( collections)
336
302
}
337
303
}
0 commit comments