Skip to content

Commit 95eebe3

Browse files
committed
WIP: Add a template for a command plugin
1 parent 366e79b commit 95eebe3

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

Sources/Commands/PackageTools/Init.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ extension SwiftPackageTool {
2525

2626
@Option(
2727
name: .customLong("type"),
28-
help: ArgumentHelp("Package type: empty | library | executable | system-module | manifest", discussion: """
28+
help: ArgumentHelp("Package type: empty | library | executable | system-module | manifest | command-plugin", discussion: """
2929
empty - Create an empty package
3030
library - Create a package that contains a library
3131
executable - Create a package that contains a binary executable
3232
system-module - Create a package that contains a system module
3333
manifest - Create a Package.swift file
34+
command-plugin - Create a package that contains a command plugin
3435
"""))
3536
var initMode: InitPackage.PackageType = .library
3637

Sources/Workspace/InitPackage.swift

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final class InitPackage {
4545
case executable = "executable"
4646
case systemModule = "system-module"
4747
case manifest = "manifest"
48-
case `extension` = "extension"
48+
case commandPlugin = "command-plugin"
4949

5050
public var description: String {
5151
return rawValue
@@ -121,6 +121,7 @@ public final class InitPackage {
121121

122122
try writeREADMEFile()
123123
try writeGitIgnore()
124+
try writePlugins()
124125
try writeSources()
125126
try writeModuleMap()
126127
try writeTests()
@@ -195,7 +196,7 @@ public final class InitPackage {
195196
]
196197
""")
197198

198-
if packageType == .library || packageType == .executable || packageType == .manifest {
199+
if packageType == .library || packageType == .executable || packageType == .manifest || packageType == .commandPlugin {
199200
var param = ""
200201

201202
param += """
@@ -204,24 +205,35 @@ public final class InitPackage {
204205
// Targets can depend on other targets in this package, and on products in packages this package depends on.
205206
206207
"""
207-
if packageType == .executable {
208+
209+
if packageType == .commandPlugin {
208210
param += """
209-
.executableTarget(
211+
.plugin(
212+
name: "\(typeName)",
213+
capability: .command(intent: .custom(verb: "\(typeName)", description: "prints hello world"))
214+
),
215+
]
210216
"""
211217
} else {
218+
if packageType == .executable {
219+
param += """
220+
.executableTarget(
221+
"""
222+
} else {
223+
param += """
224+
.target(
225+
"""
226+
}
212227
param += """
213-
.target(
228+
229+
name: "\(pkgname)",
230+
dependencies: []),
231+
.testTarget(
232+
name: "\(pkgname)Tests",
233+
dependencies: ["\(pkgname)"]),
234+
]
214235
"""
215236
}
216-
param += """
217-
218-
name: "\(pkgname)",
219-
dependencies: []),
220-
.testTarget(
221-
name: "\(pkgname)Tests",
222-
dependencies: ["\(pkgname)"]),
223-
]
224-
"""
225237

226238
pkgParams.append(param)
227239
}
@@ -280,8 +292,44 @@ public final class InitPackage {
280292
}
281293
}
282294

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+
283331
private func writeSources() throws {
284-
if packageType == .systemModule || packageType == .manifest {
332+
if packageType == .systemModule || packageType == .manifest || packageType == .commandPlugin {
285333
return
286334
}
287335
let sources = destinationPath.appending(component: "Sources")
@@ -325,7 +373,7 @@ public final class InitPackage {
325373
}
326374
327375
"""
328-
case .systemModule, .empty, .manifest, .`extension`:
376+
case .systemModule, .empty, .manifest, .commandPlugin:
329377
throw InternalError("invalid packageType \(packageType)")
330378
}
331379

@@ -356,7 +404,7 @@ public final class InitPackage {
356404
}
357405

358406
private func writeTests() throws {
359-
if packageType == .systemModule {
407+
if packageType == .systemModule || packageType == .commandPlugin {
360408
return
361409
}
362410
let tests = destinationPath.appending(component: "Tests")
@@ -367,7 +415,7 @@ public final class InitPackage {
367415
try makeDirectories(tests)
368416

369417
switch packageType {
370-
case .systemModule, .empty, .manifest, .`extension`: break
418+
case .systemModule, .empty, .manifest, .commandPlugin: break
371419
case .library, .executable:
372420
try writeTestFileStubs(testsPath: tests)
373421
}
@@ -418,7 +466,7 @@ public final class InitPackage {
418466

419467
let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule)
420468
switch packageType {
421-
case .systemModule, .empty, .manifest, .`extension`: break
469+
case .systemModule, .empty, .manifest, .commandPlugin: break
422470
case .library:
423471
try writeLibraryTestsFile(testClassFile)
424472
case .executable:

0 commit comments

Comments
 (0)