Fixed command plugins section

This commit is contained in:
mdnapo 2024-10-13 13:22:00 +02:00
parent 84b2628bd7
commit 0747a730ee

View File

@ -27,58 +27,26 @@ dotnet add package --source devdisciples --version 1.0.0 MycroForge.PluginTempla
## Initialize a plugin package ## Initialize a plugin package
Generate a template plugin project by running the following command. Generate a plugin project by running the following command.
``` ```
m4g plugin init Dotenv.Plugin m4g plugin init Dotenv.Plugin DotenvCommand dotenv
``` ```
This should generate the following folder structure. This should generate the following folder structure.
``` ```
Dotenv.Plugin Dotenv.Plugin
┣ 📜HelloWorldCommand.cs ┣ 📜Dotenv.Plugin.csproj
┣ 📜HelloWorldCommandPlugin.cs ┣ 📜DotenvCommand.cs
┗ 📜Dotenv.Plugin.csproj ┗ 📜DotenvCommandPlugin.cs
```
Rename the following files. Also rename the classes in these files, the easiest way in `vscode` is to right click the class name and select the `Rename symbol` action. Note that this action does not (necessarily) rename the files!
```
HelloWorldCommand.cs => DotenvCommand.cs
HelloWorldCommandPlugin.cs => DotenvCommandPlugin.cs
```
Modify the `Name` property and the reference to `HelloWorldCommand` in `DotenvCommandPlugin.cs`.
```cs
// Before
public class DotenvCommandPlugin : ICommandPlugin
{
public string Name => "My.Plugin";
public void RegisterServices(IServiceCollection services)
{
services.AddScoped<ISubCommandOf<RootCommand>, HelloWorldCommand>();
}
}
// After
public class DotenvCommandPlugin : ICommandPlugin
{
public string Name => "Dotenv.Plugin";
public void RegisterServices(IServiceCollection services)
{
services.AddScoped<ISubCommandOf<RootCommand>, DotenvCommand>();
}
}
``` ```
Modify `DotenvCommand.cs`. Modify `DotenvCommand.cs`.
```cs ```cs
// Before // Before
public class DotenvCommand : Command, ISubCommandOf<RootCommand> public class DotenvCommand : Command, ISubCommandOf<RootCommand>
{ {
private readonly Argument<string> NameArgument = private readonly Argument<string> NameArgument =
@ -90,7 +58,7 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
private readonly ProjectContext _context; private readonly ProjectContext _context;
public DotenvCommand(ProjectContext context) : public DotenvCommand(ProjectContext context) :
base("hello", "An example command generated by dotnet new using the m4gp template") base("dotenv", "A basic command plugin generated by the 'm4g plugin init' command")
{ {
_context = context; _context = context;
AddArgument(NameArgument); AddArgument(NameArgument);
@ -102,37 +70,38 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
{ {
name = allCaps ? name.ToUpper() : name; name = allCaps ? name.ToUpper() : name;
await _context.CreateFile("hello_world.txt", await _context.CreateFile("dotenv.txt",
$"Hello {name}!", $"Hello {name}!",
"This file was generated by your custom command!" "This file was generated by the 'm4g dotenv' command!"
); );
} }
} }
// After // After
public class DotenvCommand : Command, ISubCommandOf<RootCommand> public class DotenvCommand : Command, ISubCommandOf<RootCommand>
{ {
private readonly Argument<string> VarsArgument = private readonly Argument<string> VarsArgument =
new(name: "vars", description: "Env vars to include in the .env file separated by ';'"); new(name: "vars", description: "Env vars to include in the .env file separated by ';'");
private readonly Option<bool> PrintOption = private readonly Option<bool> OverwriteOption =
new(aliases: ["-o", "--overwrite"], description: "Overwrite the .env file if it exists"); new(aliases: ["-o", "--overwrite"], description: "Overwrite the .env file if it exists");
private readonly ProjectContext _context; private readonly ProjectContext _context;
public DotenvCommand(ProjectContext context) : public DotenvCommand(ProjectContext context) :
// dotenv = the name of the sub command that will be added to the m4g command
base("dotenv", "Generate a .env file in the current directory") base("dotenv", "Generate a .env file in the current directory")
{ {
_context = context; _context = context;
AddArgument(VarsArgument); AddArgument(VarsArgument);
AddOption(PrintOption); AddOption(OverwriteOption);
this.SetHandler(ExecuteAsync, VarsArgument, PrintOption); this.SetHandler(ExecuteAsync, VarsArgument, OverwriteOption);
} }
private async Task ExecuteAsync(string vars, bool overwrite) private async Task ExecuteAsync(string vars, bool overwrite)
{ {
var path = Path.Join(Environment.CurrentDirectory, ".env"); var path = Path.Join(Environment.CurrentDirectory, ".env");
var content = string.Join(Environment.NewLine, vars.Split(';'));
if (File.Exists(path)) if (File.Exists(path))
{ {
@ -143,9 +112,9 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
} }
Console.WriteLine($"File {path} already exists, add the -o or --overwrite flag to overwrite it."); Console.WriteLine($"File {path} already exists, add the -o or --overwrite flag to overwrite it.");
return;
} }
var content = string.Join(Environment.NewLine, vars.Split(';'));
await _context.CreateFile(".env", content); await _context.CreateFile(".env", content);
} }
} }