Improved plugin feature
All checks were successful
Build and publish MycroForge.CLI / test (push) Has been skipped
Test MycroForge.CLI / test (push) Successful in 5m39s

This commit is contained in:
mdnapo 2024-10-13 13:39:56 +02:00
parent 8f82360cc7
commit 457429f7ec
6 changed files with 42 additions and 17 deletions

View File

@ -10,21 +10,29 @@ public partial class MycroForge
{
public class Init : Command, ISubCommandOf<Plugin>
{
private static readonly Argument<string> NameArgument =
new(name: "name", description: "The name of your project");
private static readonly Argument<string> NamespaceArgument =
new(name: "namespace", description: "The namespace of your project");
private static readonly Argument<string> ClassArgument =
new(name: "class", description: "The class name of the generated command");
private static readonly Argument<string> CommandArgument =
new(name: "command", description: "The command name that will be added to 'm4g'");
private readonly ProjectContext _context;
public Init(ProjectContext context) : base("init", "Initialize a basic plugin project")
{
_context = context;
AddArgument(NameArgument);
this.SetHandler(ExecuteAsync, NameArgument);
AddArgument(NamespaceArgument);
AddArgument(ClassArgument);
AddArgument(CommandArgument);
this.SetHandler(ExecuteAsync, NamespaceArgument, ClassArgument, CommandArgument);
}
private async Task ExecuteAsync(string name)
private async Task ExecuteAsync(string @namespace, string @class, string command)
{
await _context.Bash($"dotnet new m4gp -n {name}");
await _context.Bash($"dotnet new m4gp -n {@namespace} --class {@class} --command {command}");
}
}
}

View File

@ -44,9 +44,13 @@ public partial class MycroForge
var assemblyName = GetAssemblyName();
var pluginInstallPath = Path.Join(Plugins.RootDirectory, assemblyName);
var platform = target.ToString().Dasherize();
await _context.Bash($"dotnet publish -c Release -r {platform} --output {pluginInstallPath}");
Console.WriteLine($"Successfully installed plugin {assemblyName}");
var exitCode = await _context.Bash(
$"dotnet publish -c Release -r {platform} --output {pluginInstallPath}"
);
Console.WriteLine(exitCode == 0
? $"Successfully installed plugin {assemblyName}"
: $"Could not install {assemblyName}, process exited with code {exitCode}.");
}
private string GetAssemblyName()

View File

@ -70,7 +70,7 @@ public class ProjectContext
Console.WriteLine($"Modified file {path}");
}
public async Task Bash(params string[] script)
public async Task<int> Bash(params string[] script)
{
var info = new ProcessStartInfo
{
@ -112,6 +112,8 @@ public class ProjectContext
await process.WaitForExitAsync();
Environment.ExitCode = process.ExitCode;
return process.ExitCode;
}
public async Task SaveConfig(ProjectConfig config)

View File

@ -17,5 +17,16 @@
"language": "C#",
"type": "project"
},
"preferNameDirectory": true
"preferNameDirectory": true,
"symbols": {
"class": {
"type": "parameter",
"replaces": "ExampleCommand",
"fileRename": "ExampleCommand"
},
"command": {
"type": "parameter",
"replaces": "example"
}
}
}

View File

@ -5,7 +5,7 @@ using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
public class ExampleCommand : Command, ISubCommandOf<RootCommand>
{
private readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the person to greet");
@ -15,8 +15,8 @@ public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
private readonly ProjectContext _context;
public HelloWorldCommand(ProjectContext context) :
base("hello", "An example command generated by dotnet new using the m4gp template")
public ExampleCommand(ProjectContext context) :
base("example", "A basic command plugin generated by the 'm4g plugin init' command")
{
_context = context;
AddArgument(NameArgument);
@ -28,9 +28,9 @@ public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
{
name = allCaps ? name.ToUpper() : name;
await _context.CreateFile("hello_world.txt",
await _context.CreateFile("example.txt",
$"Hello {name}!",
"This file was generated by your custom command!"
"This file was generated by the 'm4g example' command!"
);
}
}

View File

@ -4,12 +4,12 @@ using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommandPlugin : ICommandPlugin
public class ExampleCommandPlugin : ICommandPlugin
{
public string Name => "MycroForge.PluginTemplate";
public void RegisterServices(IServiceCollection services)
{
services.AddScoped<ISubCommandOf<RootCommand>, HelloWorldCommand>();
services.AddScoped<ISubCommandOf<RootCommand>, ExampleCommand>();
}
}