2
2
//
3
3
// This source file is part of the Swift open source project
4
4
//
5
- // Copyright (c) 2014-2020 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See http://swift.org/LICENSE.txt for license information
@@ -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
}
@@ -270,6 +271,14 @@ public final class InitPackage {
270
271
]),
271
272
]
272
273
"""
274
+ } else if packageType == . commandPlugin {
275
+ param += """
276
+ .plugin(
277
+ name: " \( typeName) " ,
278
+ capability: .command(intent: .custom(verb: " \( typeName) " , description: " prints hello world " ))
279
+ ),
280
+ ]
281
+ """
273
282
} else if packageType == . macro {
274
283
param += """
275
284
// Macro implementation, only built for the host and never part of a client program.
@@ -351,8 +360,44 @@ public final class InitPackage {
351
360
}
352
361
}
353
362
363
+ private func writePlugins( ) throws {
364
+ switch packageType {
365
+ case . commandPlugin:
366
+ let plugins = destinationPath. appending ( component: " Plugins " )
367
+ guard self . fileSystem. exists ( plugins) == false else {
368
+ return
369
+ }
370
+ progressReporter ? ( " Creating \( plugins. relative ( to: destinationPath) ) / " )
371
+ try makeDirectories ( plugins)
372
+
373
+ let moduleDir = plugins. appending ( component: " \( pkgname) " )
374
+ try makeDirectories ( moduleDir)
375
+
376
+ let sourceFileName = " plugin.swift "
377
+ let sourceFile = try AbsolutePath ( validating: sourceFileName, relativeTo: moduleDir)
378
+
379
+ let content = """
380
+ import PackagePlugin
381
+
382
+ @main
383
+ struct \( typeName) : CommandPlugin {
384
+ func performCommand(context: PluginContext, arguments: [String]) async throws {
385
+ print( " Hello, World! " )
386
+ }
387
+ }
388
+ """
389
+
390
+ try writePackageFile ( sourceFile) { stream in
391
+ stream. write ( content)
392
+ }
393
+
394
+ case . empty, . library, . executable, . tool, . macro:
395
+ break
396
+ }
397
+ }
398
+
354
399
private func writeSources( ) throws {
355
- if packageType == . empty {
400
+ if packageType == . empty || packageType == . commandPlugin {
356
401
return
357
402
}
358
403
@@ -428,7 +473,7 @@ public final class InitPackage {
428
473
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: " \( moduleName) Macros " , type: " StringifyMacro " )
429
474
"""
430
475
431
- case . empty, . `extension` :
476
+ case . empty, . commandPlugin :
432
477
throw InternalError ( " invalid packageType \( packageType) " )
433
478
}
434
479
@@ -444,7 +489,7 @@ public final class InitPackage {
444
489
445
490
private func writeTests( ) throws {
446
491
switch packageType {
447
- case . empty, . executable, . tool, . `extension` : return
492
+ case . empty, . executable, . tool, . commandPlugin : return
448
493
default : break
449
494
}
450
495
let tests = destinationPath. appending ( " Tests " )
@@ -590,7 +635,7 @@ public final class InitPackage {
590
635
591
636
let testClassFile = try AbsolutePath ( validating: " \( moduleName) Tests.swift " , relativeTo: testModule)
592
637
switch packageType {
593
- case . empty, . `extension` , . executable, . tool: break
638
+ case . empty, . commandPlugin , . executable, . tool: break
594
639
case . library:
595
640
try writeLibraryTestsFile ( testClassFile)
596
641
case . macro:
0 commit comments