Compare commits

..

No commits in common. "0747a730eee0a9982bcee5031df76c3d3aa73d8e" and "f547a0e132349886022260cd9e66ccbb7c07d9da" have entirely different histories.

View File

@ -27,26 +27,58 @@ dotnet add package --source devdisciples --version 1.0.0 MycroForge.PluginTempla
## Initialize a plugin package ## Initialize a plugin package
Generate a plugin project by running the following command. Generate a template plugin project by running the following command.
``` ```
m4g plugin init Dotenv.Plugin DotenvCommand dotenv m4g plugin init My.Dotenv.Plugin
``` ```
This should generate the following folder structure. This should generate the following folder structure.
``` ```
Dotenv.Plugin My.Dotenv.Plugin
┣ 📜Dotenv.Plugin.csproj ┣ 📜HelloWorldCommand.cs
┣ 📜DotenvCommand.cs ┣ 📜HelloWorldCommandPlugin.cs
┗ 📜DotenvCommandPlugin.cs ┗ 📜My.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 `Name` property 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 => "My.Dotenv.Plugin";
public void RegisterServices(IServiceCollection services)
{
services.AddScoped<ISubCommandOf<RootCommand>, HelloWorldCommand>();
}
}
``` ```
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 =
@ -58,7 +90,7 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
private readonly ProjectContext _context; private readonly ProjectContext _context;
public DotenvCommand(ProjectContext context) : public DotenvCommand(ProjectContext context) :
base("dotenv", "A basic command plugin generated by the 'm4g plugin init' command") base("hello", "An example command generated by dotnet new using the m4gp template")
{ {
_context = context; _context = context;
AddArgument(NameArgument); AddArgument(NameArgument);
@ -70,38 +102,37 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
{ {
name = allCaps ? name.ToUpper() : name; name = allCaps ? name.ToUpper() : name;
await _context.CreateFile("dotenv.txt", await _context.CreateFile("hello_world.txt",
$"Hello {name}!", $"Hello {name}!",
"This file was generated by the 'm4g dotenv' command!" "This file was generated by your custom 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> OverwriteOption = private readonly Option<bool> PrintOption =
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(OverwriteOption); AddOption(PrintOption);
this.SetHandler(ExecuteAsync, VarsArgument, OverwriteOption); this.SetHandler(ExecuteAsync, VarsArgument, PrintOption);
} }
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))
{ {
@ -112,9 +143,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);
} }
} }
@ -122,7 +153,7 @@ public class DotenvCommand : Command, ISubCommandOf<RootCommand>
## Install the plugin ## Install the plugin
Open a terminal an make sure you're in the root directory of the plugin, i.e. the `Dotenv.Plugin` folder. Open a terminal an make sure you're in the root directory of the plugin, i.e. the `My.Dotenv.Plugin` folder.
Run the following command to install the plugin. Run the following command to install the plugin.
``` ```
@ -138,7 +169,7 @@ Try running `m4g dotenv "FIRSTNAME=JOHN;LASTNAME=JOE"`, this should generate a `
## Uninstall the plugin ## Uninstall the plugin
Uninstall the plugin by running `m4g plugin uninstall Dotenv.Plugin`. Uninstall the plugin by running `m4g plugin install My.Dotenv.Plugin`.
## Resources ## Resources