Skip to content

Commit

Permalink
WIP: Add a template for a command plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
neonichu authored and abertelrud committed Mar 27, 2023
1 parent 6035562 commit d78fb5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions Sources/Commands/PackageTools/Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension SwiftPackageTool {
tool - A package with an executable that uses
Swift Argument Parser. Use this template if you
plan to have a rich set of command-line arguments.
command-plugin - A package that vends a command plugin.
macro - A package that vends a macro.
empty - An empty package with a Package.swift manifest.
"""))
Expand Down
53 changes: 49 additions & 4 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class InitPackage {
case library = "library"
case executable = "executable"
case tool = "tool"
case `extension` = "extension"
case commandPlugin = "command-plugin"
case macro = "macro"

public var description: String {
Expand Down Expand Up @@ -115,6 +115,7 @@ public final class InitPackage {
// none of it exists, and then act.
try writeManifestFile()
try writeGitIgnore()
try writePlugins()
try writeSources()
try writeTests()
}
Expand Down Expand Up @@ -269,6 +270,14 @@ public final class InitPackage {
]),
]
"""
} else if packageType == .commandPlugin {
param += """
.plugin(
name: "\(typeName)",
capability: .command(intent: .custom(verb: "\(typeName)", description: "prints hello world"))
),
]
"""
} else if packageType == .macro {
param += """
// Macro implementation, only built for the host and never part of a client program.
Expand Down Expand Up @@ -350,6 +359,42 @@ public final class InitPackage {
}
}

private func writePlugins() throws {
switch packageType {
case .commandPlugin:
let plugins = destinationPath.appending(component: "Plugins")
guard self.fileSystem.exists(plugins) == false else {
return
}
progressReporter?("Creating \(plugins.relative(to: destinationPath))/")
try makeDirectories(plugins)

let moduleDir = plugins.appending(component: "\(pkgname)")
try makeDirectories(moduleDir)

let sourceFileName = "\(typeName).swift"
let sourceFile = try AbsolutePath(validating: sourceFileName, relativeTo: moduleDir)

let content = """
import PackagePlugin
@main
struct \(typeName): CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {
print("Hello, World!")
}
}
"""

try writePackageFile(sourceFile) { stream in
stream.write(content)
}

case .empty, .library, .executable, .tool, .macro:
break
}
}

private func writeSources() throws {
if packageType == .empty {
return
Expand Down Expand Up @@ -427,7 +472,7 @@ public final class InitPackage {
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "\(moduleName)Macros", type: "StringifyMacro")
"""

case .empty, .`extension`:
case .empty, .commandPlugin:
throw InternalError("invalid packageType \(packageType)")
}

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

private func writeTests() throws {
switch packageType {
case .empty, .executable, .tool, .`extension`: return
case .empty, .executable, .tool, .commandPlugin: return
default: break
}
let tests = destinationPath.appending("Tests")
Expand Down Expand Up @@ -589,7 +634,7 @@ public final class InitPackage {

let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule)
switch packageType {
case .empty, .`extension`, .executable, .tool: break
case .empty, .commandPlugin, .executable, .tool: break
case .library:
try writeLibraryTestsFile(testClassFile)
case .macro:
Expand Down

0 comments on commit d78fb5f

Please sign in to comment.