diff --git a/docs/command_plugins.md b/docs/command_plugins.md index 1068263..fd72a3b 100644 --- a/docs/command_plugins.md +++ b/docs/command_plugins.md @@ -27,58 +27,26 @@ dotnet add package --source devdisciples --version 1.0.0 MycroForge.PluginTempla ## 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. ``` Dotenv.Plugin - ┣ 📜HelloWorldCommand.cs - ┣ 📜HelloWorldCommandPlugin.cs - ┗ 📜Dotenv.Plugin.csproj -``` - -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, HelloWorldCommand>(); - } -} - -// After -public class DotenvCommandPlugin : ICommandPlugin -{ - public string Name => "Dotenv.Plugin"; - - public void RegisterServices(IServiceCollection services) - { - services.AddScoped, DotenvCommand>(); - } -} + ┣ 📜Dotenv.Plugin.csproj + ┣ 📜DotenvCommand.cs + ┗ 📜DotenvCommandPlugin.cs ``` Modify `DotenvCommand.cs`. ```cs // Before + public class DotenvCommand : Command, ISubCommandOf { private readonly Argument NameArgument = @@ -90,7 +58,7 @@ public class DotenvCommand : Command, ISubCommandOf private readonly 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; AddArgument(NameArgument); @@ -102,37 +70,38 @@ public class DotenvCommand : Command, ISubCommandOf { name = allCaps ? name.ToUpper() : name; - await _context.CreateFile("hello_world.txt", + await _context.CreateFile("dotenv.txt", $"Hello {name}!", - "This file was generated by your custom command!" + "This file was generated by the 'm4g dotenv' command!" ); } } // After + public class DotenvCommand : Command, ISubCommandOf { private readonly Argument VarsArgument = new(name: "vars", description: "Env vars to include in the .env file separated by ';'"); - private readonly Option PrintOption = + private readonly Option OverwriteOption = new(aliases: ["-o", "--overwrite"], description: "Overwrite the .env file if it exists"); private readonly 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") { _context = context; AddArgument(VarsArgument); - AddOption(PrintOption); - this.SetHandler(ExecuteAsync, VarsArgument, PrintOption); + AddOption(OverwriteOption); + this.SetHandler(ExecuteAsync, VarsArgument, OverwriteOption); } private async Task ExecuteAsync(string vars, bool overwrite) { var path = Path.Join(Environment.CurrentDirectory, ".env"); + var content = string.Join(Environment.NewLine, vars.Split(';')); if (File.Exists(path)) { @@ -143,9 +112,9 @@ public class DotenvCommand : Command, ISubCommandOf } 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); } }