@@ -44,7 +44,7 @@ public final class InitPackage {
44
44
case library = " library "
45
45
case executable = " executable "
46
46
case tool = " tool "
47
- case `extension` = " extension "
47
+ case commandPlugin = " command-plugin "
48
48
case macro = " macro "
49
49
50
50
public var description : String {
@@ -115,6 +115,7 @@ public final class InitPackage {
115
115
// none of it exists, and then act.
116
116
try writeManifestFile ( )
117
117
try writeGitIgnore ( )
118
+ try writePlugins ( )
118
119
try writeSources ( )
119
120
try writeTests ( )
120
121
}
@@ -269,6 +270,14 @@ public final class InitPackage {
269
270
]),
270
271
]
271
272
"""
273
+ } else if packageType == . commandPlugin {
274
+ param += """
275
+ .plugin(
276
+ name: " \( typeName) " ,
277
+ capability: .command(intent: .custom(verb: " \( typeName) " , description: " prints hello world " ))
278
+ ),
279
+ ]
280
+ """
272
281
} else if packageType == . macro {
273
282
param += """
274
283
// Macro implementation, only built for the host and never part of a client program.
@@ -350,6 +359,42 @@ public final class InitPackage {
350
359
}
351
360
}
352
361
362
+ private func writePlugins( ) throws {
363
+ switch packageType {
364
+ case . commandPlugin:
365
+ let plugins = destinationPath. appending ( component: " Plugins " )
366
+ guard self . fileSystem. exists ( plugins) == false else {
367
+ return
368
+ }
369
+ progressReporter ? ( " Creating \( plugins. relative ( to: destinationPath) ) / " )
370
+ try makeDirectories ( plugins)
371
+
372
+ let moduleDir = plugins. appending ( component: " \( pkgname) " )
373
+ try makeDirectories ( moduleDir)
374
+
375
+ let sourceFileName = " \( typeName) .swift "
376
+ let sourceFile = try AbsolutePath ( validating: sourceFileName, relativeTo: moduleDir)
377
+
378
+ let content = """
379
+ import PackagePlugin
380
+
381
+ @main
382
+ struct \( typeName) : CommandPlugin {
383
+ func performCommand(context: PluginContext, arguments: [String]) async throws {
384
+ print( " Hello, World! " )
385
+ }
386
+ }
387
+ """
388
+
389
+ try writePackageFile ( sourceFile) { stream in
390
+ stream. write ( content)
391
+ }
392
+
393
+ case . empty, . library, . executable, . tool, . macro:
394
+ break
395
+ }
396
+ }
397
+
353
398
private func writeSources( ) throws {
354
399
if packageType == . empty {
355
400
return
@@ -427,7 +472,7 @@ public final class InitPackage {
427
472
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: " \( moduleName) Macros " , type: " StringifyMacro " )
428
473
"""
429
474
430
- case . empty, . `extension` :
475
+ case . empty, . commandPlugin :
431
476
throw InternalError ( " invalid packageType \( packageType) " )
432
477
}
433
478
@@ -443,7 +488,7 @@ public final class InitPackage {
443
488
444
489
private func writeTests( ) throws {
445
490
switch packageType {
446
- case . empty, . executable, . tool, . `extension` : return
491
+ case . empty, . executable, . tool, . commandPlugin : return
447
492
default : break
448
493
}
449
494
let tests = destinationPath. appending ( " Tests " )
@@ -589,7 +634,7 @@ public final class InitPackage {
589
634
590
635
let testClassFile = try AbsolutePath ( validating: " \( moduleName) Tests.swift " , relativeTo: testModule)
591
636
switch packageType {
592
- case . empty, . `extension` , . executable, . tool: break
637
+ case . empty, . commandPlugin , . executable, . tool: break
593
638
case . library:
594
639
try writeLibraryTestsFile ( testClassFile)
595
640
case . macro:
0 commit comments