From c220c214d24d497c31f417c11ee864c74a21f6b6 Mon Sep 17 00:00:00 2001 From: mdnapo Date: Sun, 30 Jun 2024 13:59:40 +0200 Subject: [PATCH] Making plugin system more robust --- .../Commands/MycroForge.Plugin.Init.cs | 28 +++++++++++++++++++ MycroForge.Core/MycroForge.Core.csproj | 5 ++++ MycroForge.Core/README.md | 5 ++++ MycroForge.Plugin.Template.Package/Class1.cs | 5 ++++ .../MycroForge.Plugin.Template.Package.csproj | 9 ++++++ MycroForge.Plugin.Template/Constants.cs | 6 ++++ .../HelloWorldCommand.cs | 25 +++++++++++++++++ .../HelloWorldCommandPlugin.cs | 16 +++++++++++ .../MycroForge.Plugin.Template.csproj | 13 +++++++++ MycroForge.sln | 12 ++++++++ 10 files changed, 124 insertions(+) create mode 100644 MycroForge.CLI/Commands/MycroForge.Plugin.Init.cs create mode 100644 MycroForge.Core/README.md create mode 100644 MycroForge.Plugin.Template.Package/Class1.cs create mode 100644 MycroForge.Plugin.Template.Package/MycroForge.Plugin.Template.Package.csproj create mode 100644 MycroForge.Plugin.Template/Constants.cs create mode 100644 MycroForge.Plugin.Template/HelloWorldCommand.cs create mode 100644 MycroForge.Plugin.Template/HelloWorldCommandPlugin.cs create mode 100644 MycroForge.Plugin.Template/MycroForge.Plugin.Template.csproj diff --git a/MycroForge.CLI/Commands/MycroForge.Plugin.Init.cs b/MycroForge.CLI/Commands/MycroForge.Plugin.Init.cs new file mode 100644 index 0000000..a1a0ef3 --- /dev/null +++ b/MycroForge.CLI/Commands/MycroForge.Plugin.Init.cs @@ -0,0 +1,28 @@ +using System.CommandLine; +using MycroForge.Core; +using MycroForge.Core.Contract; + +namespace MycroForge.CLI.Commands; + +public partial class MycroForge +{ + public partial class Plugin + { + public class Init : Command, ISubCommandOf + { + private readonly ProjectContext _context; + + public Init(ProjectContext context) : base("init", "Initialize a basic plugin project") + { + _context = context; + this.SetHandler(ExecuteAsync); + } + + private void ExecuteAsync() + { + foreach (var plugin in Plugins.Loaded) + Console.WriteLine($"name: {plugin.Name}, command: {plugin.Command}"); + } + } + } +} \ No newline at end of file diff --git a/MycroForge.Core/MycroForge.Core.csproj b/MycroForge.Core/MycroForge.Core.csproj index 2dac34a..f889ea0 100644 --- a/MycroForge.Core/MycroForge.Core.csproj +++ b/MycroForge.Core/MycroForge.Core.csproj @@ -4,6 +4,11 @@ net8.0 enable enable + MycroForge.Core + 1.0.0 + Donné Napo + Dev Disciples + true diff --git a/MycroForge.Core/README.md b/MycroForge.Core/README.md new file mode 100644 index 0000000..c4ac56f --- /dev/null +++ b/MycroForge.Core/README.md @@ -0,0 +1,5 @@ +### Publish to local nuget repo + +```shell +dotnet nuget push ./bin/Debug/MycroForge.Core.1.0.0.nupkg --source http://localhost:5555/v3/index.json --api-key password +``` \ No newline at end of file diff --git a/MycroForge.Plugin.Template.Package/Class1.cs b/MycroForge.Plugin.Template.Package/Class1.cs new file mode 100644 index 0000000..5bc03f9 --- /dev/null +++ b/MycroForge.Plugin.Template.Package/Class1.cs @@ -0,0 +1,5 @@ +namespace MycroForge.Plugin.Template.Package; + +public class Class1 +{ +} \ No newline at end of file diff --git a/MycroForge.Plugin.Template.Package/MycroForge.Plugin.Template.Package.csproj b/MycroForge.Plugin.Template.Package/MycroForge.Plugin.Template.Package.csproj new file mode 100644 index 0000000..595335a --- /dev/null +++ b/MycroForge.Plugin.Template.Package/MycroForge.Plugin.Template.Package.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/MycroForge.Plugin.Template/Constants.cs b/MycroForge.Plugin.Template/Constants.cs new file mode 100644 index 0000000..6c9c63b --- /dev/null +++ b/MycroForge.Plugin.Template/Constants.cs @@ -0,0 +1,6 @@ +namespace MycroForge.Plugin.Template; + +public static class Constants +{ + public const string MainCommandName = "mj"; +} \ No newline at end of file diff --git a/MycroForge.Plugin.Template/HelloWorldCommand.cs b/MycroForge.Plugin.Template/HelloWorldCommand.cs new file mode 100644 index 0000000..6db9a9a --- /dev/null +++ b/MycroForge.Plugin.Template/HelloWorldCommand.cs @@ -0,0 +1,25 @@ +using System.CommandLine; +using MycroForge.Core; +using MycroForge.Core.Contract; +using RootCommand = MycroForge.Core.RootCommand; + +namespace MycroForge.Plugin.Template; + +public class HelloWorldCommand : Command, ISubCommandOf +{ + private readonly ProjectContext _context; + + public HelloWorldCommand(ProjectContext context) : + base(Constants.MainCommandName, "Custom command for My Jewellery specific stuff") + { + _context = context; + this.SetHandler(ExecuteAsync); + } + + private async Task ExecuteAsync() + { + await _context.CreateFile("hello_world.txt", + "My Jewellery command plugin is working!" + ); + } +} \ No newline at end of file diff --git a/MycroForge.Plugin.Template/HelloWorldCommandPlugin.cs b/MycroForge.Plugin.Template/HelloWorldCommandPlugin.cs new file mode 100644 index 0000000..274477e --- /dev/null +++ b/MycroForge.Plugin.Template/HelloWorldCommandPlugin.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using MycroForge.Core.Contract; +using RootCommand = MycroForge.Core.RootCommand; + +namespace MycroForge.Plugin.Template; + +public class HelloWorldCommandPlugin : ICommandPlugin +{ + public string Name => "MycroForge.Plugin.Template"; + public string Command => Constants.MainCommandName; + + public void RegisterServices(IServiceCollection services) + { + services.AddScoped, HelloWorldCommand>(); + } +} \ No newline at end of file diff --git a/MycroForge.Plugin.Template/MycroForge.Plugin.Template.csproj b/MycroForge.Plugin.Template/MycroForge.Plugin.Template.csproj new file mode 100644 index 0000000..86f4080 --- /dev/null +++ b/MycroForge.Plugin.Template/MycroForge.Plugin.Template.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/MycroForge.sln b/MycroForge.sln index 9229270..0c8f317 100644 --- a/MycroForge.sln +++ b/MycroForge.sln @@ -6,6 +6,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MycroForge.Core", "MycroFor EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MycroForge.TestPlugin", "MycroForge.TestPlugin\MycroForge.TestPlugin.csproj", "{7C479E68-98FA-4FBC-B5E4-7116015774B3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MycroForge.Plugin.Template", "MycroForge.Plugin.Template\MycroForge.Plugin.Template.csproj", "{114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MycroForge.Plugin.Template.Package", "MycroForge.Plugin.Template.Package\MycroForge.Plugin.Template.Package.csproj", "{FF110508-7D12-48DB-9484-8D2A0A5D29B8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +28,13 @@ Global {7C479E68-98FA-4FBC-B5E4-7116015774B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C479E68-98FA-4FBC-B5E4-7116015774B3}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C479E68-98FA-4FBC-B5E4-7116015774B3}.Release|Any CPU.Build.0 = Release|Any CPU + {114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}.Debug|Any CPU.Build.0 = Debug|Any CPU + {114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}.Release|Any CPU.ActiveCfg = Release|Any CPU + {114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}.Release|Any CPU.Build.0 = Release|Any CPU + {FF110508-7D12-48DB-9484-8D2A0A5D29B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF110508-7D12-48DB-9484-8D2A0A5D29B8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF110508-7D12-48DB-9484-8D2A0A5D29B8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF110508-7D12-48DB-9484-8D2A0A5D29B8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal