@@ -45,7 +45,7 @@ public final class InitPackage {
45
45
case executable = " executable "
46
46
case systemModule = " system-module "
47
47
case manifest = " manifest "
48
- case `extension` = " extension "
48
+ case commandPlugin = " command-plugin "
49
49
50
50
public var description : String {
51
51
return rawValue
@@ -121,6 +121,7 @@ public final class InitPackage {
121
121
122
122
try writeREADMEFile ( )
123
123
try writeGitIgnore ( )
124
+ try writePlugins ( )
124
125
try writeSources ( )
125
126
try writeModuleMap ( )
126
127
try writeTests ( )
@@ -195,7 +196,7 @@ public final class InitPackage {
195
196
]
196
197
""" )
197
198
198
- if packageType == . library || packageType == . executable || packageType == . manifest {
199
+ if packageType == . library || packageType == . executable || packageType == . manifest || packageType == . commandPlugin {
199
200
var param = " "
200
201
201
202
param += """
@@ -204,24 +205,35 @@ public final class InitPackage {
204
205
// Targets can depend on other targets in this package, and on products in packages this package depends on.
205
206
206
207
"""
207
- if packageType == . executable {
208
+
209
+ if packageType == . commandPlugin {
208
210
param += """
209
- .executableTarget(
211
+ .plugin(
212
+ name: " \( typeName) " ,
213
+ capability: .command(intent: .custom(verb: " \( typeName) " , description: " prints hello world " ))
214
+ ),
215
+ ]
210
216
"""
211
217
} else {
218
+ if packageType == . executable {
219
+ param += """
220
+ .executableTarget(
221
+ """
222
+ } else {
223
+ param += """
224
+ .target(
225
+ """
226
+ }
212
227
param += """
213
- .target(
228
+
229
+ name: " \( pkgname) " ,
230
+ dependencies: []),
231
+ .testTarget(
232
+ name: " \( pkgname) Tests " ,
233
+ dependencies: [ " \( pkgname) " ]),
234
+ ]
214
235
"""
215
236
}
216
- param += """
217
-
218
- name: " \( pkgname) " ,
219
- dependencies: []),
220
- .testTarget(
221
- name: " \( pkgname) Tests " ,
222
- dependencies: [ " \( pkgname) " ]),
223
- ]
224
- """
225
237
226
238
pkgParams. append ( param)
227
239
}
@@ -280,8 +292,44 @@ public final class InitPackage {
280
292
}
281
293
}
282
294
295
+ private func writePlugins( ) throws {
296
+ switch packageType {
297
+ case . commandPlugin:
298
+ let plugins = destinationPath. appending ( component: " Plugins " )
299
+ guard self . fileSystem. exists ( plugins) == false else {
300
+ return
301
+ }
302
+ progressReporter ? ( " Creating \( plugins. relative ( to: destinationPath) ) / " )
303
+ try makeDirectories ( plugins)
304
+
305
+ let moduleDir = plugins. appending ( component: " \( pkgname) " )
306
+ try makeDirectories ( moduleDir)
307
+
308
+ let sourceFileName = " \( typeName) .swift "
309
+ let sourceFile = try AbsolutePath ( validating: sourceFileName, relativeTo: moduleDir)
310
+
311
+ let content = """
312
+ import PackagePlugin
313
+
314
+ @main
315
+ struct \( typeName) : CommandPlugin {
316
+ func performCommand(context: PluginContext, arguments: [String]) async throws {
317
+ print( " Hello, World! " )
318
+ }
319
+ }
320
+ """
321
+
322
+ try writePackageFile ( sourceFile) { stream in
323
+ stream. write ( content)
324
+ }
325
+
326
+ case . empty, . library, . executable, . systemModule, . manifest:
327
+ break
328
+ }
329
+ }
330
+
283
331
private func writeSources( ) throws {
284
- if packageType == . systemModule || packageType == . manifest {
332
+ if packageType == . systemModule || packageType == . manifest || packageType == . commandPlugin {
285
333
return
286
334
}
287
335
let sources = destinationPath. appending ( component: " Sources " )
@@ -325,7 +373,7 @@ public final class InitPackage {
325
373
}
326
374
327
375
"""
328
- case . systemModule, . empty, . manifest, . `extension` :
376
+ case . systemModule, . empty, . manifest, . commandPlugin :
329
377
throw InternalError ( " invalid packageType \( packageType) " )
330
378
}
331
379
@@ -356,7 +404,7 @@ public final class InitPackage {
356
404
}
357
405
358
406
private func writeTests( ) throws {
359
- if packageType == . systemModule {
407
+ if packageType == . systemModule || packageType == . commandPlugin {
360
408
return
361
409
}
362
410
let tests = destinationPath. appending ( component: " Tests " )
@@ -367,7 +415,7 @@ public final class InitPackage {
367
415
try makeDirectories ( tests)
368
416
369
417
switch packageType {
370
- case . systemModule, . empty, . manifest, . `extension` : break
418
+ case . systemModule, . empty, . manifest, . commandPlugin : break
371
419
case . library, . executable:
372
420
try writeTestFileStubs ( testsPath: tests)
373
421
}
@@ -418,7 +466,7 @@ public final class InitPackage {
418
466
419
467
let testClassFile = try AbsolutePath ( validating: " \( moduleName) Tests.swift " , relativeTo: testModule)
420
468
switch packageType {
421
- case . systemModule, . empty, . manifest, . `extension` : break
469
+ case . systemModule, . empty, . manifest, . commandPlugin : break
422
470
case . library:
423
471
try writeLibraryTestsFile ( testClassFile)
424
472
case . executable:
0 commit comments