Skip to content

Commit d78fb5f

Browse files
neonichuabertelrud
authored andcommitted
WIP: Add a template for a command plugin
1 parent 6035562 commit d78fb5f

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

Sources/Commands/PackageTools/Init.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extension SwiftPackageTool {
3131
tool - A package with an executable that uses
3232
Swift Argument Parser. Use this template if you
3333
plan to have a rich set of command-line arguments.
34+
command-plugin - A package that vends a command plugin.
3435
macro - A package that vends a macro.
3536
empty - An empty package with a Package.swift manifest.
3637
"""))

Sources/Workspace/InitPackage.swift

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class InitPackage {
4444
case library = "library"
4545
case executable = "executable"
4646
case tool = "tool"
47-
case `extension` = "extension"
47+
case commandPlugin = "command-plugin"
4848
case macro = "macro"
4949

5050
public var description: String {
@@ -115,6 +115,7 @@ public final class InitPackage {
115115
// none of it exists, and then act.
116116
try writeManifestFile()
117117
try writeGitIgnore()
118+
try writePlugins()
118119
try writeSources()
119120
try writeTests()
120121
}
@@ -269,6 +270,14 @@ public final class InitPackage {
269270
]),
270271
]
271272
"""
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+
"""
272281
} else if packageType == .macro {
273282
param += """
274283
// Macro implementation, only built for the host and never part of a client program.
@@ -350,6 +359,42 @@ public final class InitPackage {
350359
}
351360
}
352361

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+
353398
private func writeSources() throws {
354399
if packageType == .empty {
355400
return
@@ -427,7 +472,7 @@ public final class InitPackage {
427472
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "\(moduleName)Macros", type: "StringifyMacro")
428473
"""
429474

430-
case .empty, .`extension`:
475+
case .empty, .commandPlugin:
431476
throw InternalError("invalid packageType \(packageType)")
432477
}
433478

@@ -443,7 +488,7 @@ public final class InitPackage {
443488

444489
private func writeTests() throws {
445490
switch packageType {
446-
case .empty, .executable, .tool, .`extension`: return
491+
case .empty, .executable, .tool, .commandPlugin: return
447492
default: break
448493
}
449494
let tests = destinationPath.appending("Tests")
@@ -589,7 +634,7 @@ public final class InitPackage {
589634

590635
let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule)
591636
switch packageType {
592-
case .empty, .`extension`, .executable, .tool: break
637+
case .empty, .commandPlugin, .executable, .tool: break
593638
case .library:
594639
try writeLibraryTestsFile(testClassFile)
595640
case .macro:

0 commit comments

Comments
 (0)