Modified plugin system code

This commit is contained in:
mdnapo 2024-07-05 12:35:36 +02:00
parent c220c214d2
commit 7f67201bb2
22 changed files with 214 additions and 56 deletions

View File

@ -10,18 +10,21 @@ 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 readonly ProjectContext _context;
public Init(ProjectContext context) : base("init", "Initialize a basic plugin project")
{
_context = context;
this.SetHandler(ExecuteAsync);
AddArgument(NameArgument);
this.SetHandler(ExecuteAsync, NameArgument);
}
private void ExecuteAsync()
private async Task ExecuteAsync(string name)
{
foreach (var plugin in Plugins.Loaded)
Console.WriteLine($"name: {plugin.Name}, command: {plugin.Command}");
await _context.Bash($"dotnet new m4gp -n {name}");
}
}
}

View File

@ -19,7 +19,7 @@ public partial class MycroForge
private void ExecuteAsync()
{
foreach (var plugin in Plugins.Loaded)
Console.WriteLine($"name: {plugin.Name}, command: {plugin.Command}");
Console.WriteLine($"{plugin.Name}");
}
}
}

View File

@ -1,11 +1,10 @@
using System.CommandLine;
using MycroForge.Core.Contract;
using Core_RootCommand = MycroForge.Core.RootCommand;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.CLI.Commands;
public sealed partial class MycroForge : Core_RootCommand
public sealed partial class MycroForge : RootCommand
{
public override string Name => "m4g";

View File

@ -50,6 +50,7 @@ public static class ServiceCollectionExtensions
// Register "m4g plugin"
services.AddScoped<ISubCommandOf<Commands.MycroForge>, Commands.MycroForge.Plugin>();
services.AddScoped<ISubCommandOf<Commands.MycroForge.Plugin>, Commands.MycroForge.Plugin.Init>();
services.AddScoped<ISubCommandOf<Commands.MycroForge.Plugin>, Commands.MycroForge.Plugin.List>();
services.AddScoped<ISubCommandOf<Commands.MycroForge.Plugin>, Commands.MycroForge.Plugin.Install>();
services.AddScoped<ISubCommandOf<Commands.MycroForge.Plugin>, Commands.MycroForge.Plugin.Uninstall>();

View File

@ -5,7 +5,6 @@ namespace MycroForge.Core.Contract;
public interface ICommandPlugin
{
public string? Name { get; }
public string Command { get; }
public void RegisterServices(IServiceCollection services);
}

View File

@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>MycroForge.Core</PackageId>
<Description>The MycroForge core package</Description>
<Version>1.0.0</Version>
<Authors>Donné Napo</Authors>
<Company>Dev Disciples</Company>

View File

@ -1,5 +1,6 @@
### 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
dotnet build -r Release
dotnet nuget push ./bin/Release/MycroForge.Core.1.0.0.nupkg --source http://localhost:5555/v3/index.json --api-key password
```

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- The package metadata. Fill in the properties marked as TODO below -->
<!-- Follow the instructions on https://learn.microsoft.com/nuget/create-packages/package-authoring-best-practices -->
<PackageId>MycroForge.Package.PluginTemplate</PackageId>
<PackageVersion>1.0</PackageVersion>
<Title>A template for generating MycroForge plugins</Title>
<Authors>Donné Napo</Authors>
<Description>Template to use when creating a plugin for the MycroForge CLI.</Description>
<PackageTags>dotnet-new;templates;</PackageTags>
<PackageProjectUrl>https://github.com/mdnapo/myrcoforge</PackageProjectUrl>
<PackageType>Template</PackageType>
<TargetFramework>net8.0</TargetFramework>
<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<NoWarn>$(NoWarn);NU5128</NoWarn>
<NoDefaultExcludes>true</NoDefaultExcludes>
</PropertyGroup>
<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**"/>
<Compile Remove="**\*"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
### Used resources
https://www.youtube.com/watch?v=XzD-95qfWJM
https://www.youtube.com/watch?v=rdWZo5PD9Ek
https://learn.microsoft.com/en-us/dotnet/core/tutorials/cli-templates-create-template-package?pivots=dotnet-8-0
### Build the package
`dotnet pack`
### Push to local nuget
`dotnet nuget push ./bin/Release/MycroForge.Package.PluginTemplate.1.0.0.nupkg --source http://localhost:5555/v3/index.json --api-key password`
### Install template package from local nuget
`dotnet new install MycroForge.Package.PluginTemplate --nuget-source http://localhost:5555/v3/index.json --force`

View File

@ -0,0 +1,21 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Donné Napo",
"defaultName": "My.Plugin",
"name": "MycroForge plugin template",
"description": "Creates a basic MycroForge plugin project",
"projectURL": "https://github.com/mdnapo/mycroforge",
"repository": {
"url": "https://github.com/",
"type": "GitHub"
},
"classifications": ["Console","Plugin"],
"identity": "MycroForge.PluginTemplate",
"shortName": "m4gp",
"sourceName": "MycroForge.PluginTemplate",
"tags": {
"language": "C#",
"type": "project"
},
"preferNameDirectory": true
}

View File

@ -0,0 +1,36 @@
using System.CommandLine;
using MycroForge.Core;
using MycroForge.Core.Contract;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
{
private readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the person to greet");
private readonly Option<bool> AllCapsOption =
new(aliases: ["-a", "--all-caps"], description: "Print the name in all caps");
private readonly ProjectContext _context;
public HelloWorldCommand(ProjectContext context) :
base("hello", "An example command generated by dotnet new using the m4gp template")
{
_context = context;
AddArgument(NameArgument);
AddOption(AllCapsOption);
this.SetHandler(ExecuteAsync, NameArgument, AllCapsOption);
}
private async Task ExecuteAsync(string name, bool allCaps)
{
name = allCaps ? name.ToUpper() : name;
await _context.CreateFile("hello_world.txt",
$"Hello {name}!",
"This file was generated by your custom command!"
);
}
}

View File

@ -2,12 +2,11 @@
using MycroForge.Core.Contract;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.Plugin.Template;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommandPlugin : ICommandPlugin
{
public string Name => "MycroForge.Plugin.Template";
public string Command => Constants.MainCommandName;
public string Name => "MycroForge.PluginTemplate";
public void RegisterServices(IServiceCollection services)
{

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Content Include=".template.config\template.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MycroForge.Core" Version="1.0.0" />
</ItemGroup>

View File

@ -1,5 +0,0 @@
namespace MycroForge.Plugin.Template.Package;
public class Class1
{
}

View File

@ -1,6 +0,0 @@
namespace MycroForge.Plugin.Template;
public static class Constants
{
public const string MainCommandName = "mj";
}

View File

@ -1,25 +0,0 @@
using System.CommandLine;
using MycroForge.Core;
using MycroForge.Core.Contract;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.Plugin.Template;
public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
{
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!"
);
}
}

View File

@ -0,0 +1,21 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Donné Napo",
"defaultName": "My.Plugin",
"name": "MycroForge plugin template",
"description": "Creates a basic MycroForge plugin project",
"projectURL": "https://github.com/mdnapo/mycroforge",
"repository": {
"url": "https://github.com/",
"type": "GitHub"
},
"classifications": ["Console","Plugin"],
"identity": "MycroForge.PluginTemplate",
"shortName": "m4gp",
"sourceName": "MycroForge.PluginTemplate",
"tags": {
"language": "C#",
"type": "project"
},
"preferNameDirectory": true
}

View File

@ -0,0 +1,36 @@
using System.CommandLine;
using MycroForge.Core;
using MycroForge.Core.Contract;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommand : Command, ISubCommandOf<RootCommand>
{
private readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the person to greet");
private readonly Option<bool> AllCapsOption =
new(aliases: ["-a", "--all-caps"], description: "Print the name in all caps");
private readonly ProjectContext _context;
public HelloWorldCommand(ProjectContext context) :
base("hello", "An example command generated by dotnet new using the m4gp template")
{
_context = context;
AddArgument(NameArgument);
AddOption(AllCapsOption);
this.SetHandler(ExecuteAsync, NameArgument, AllCapsOption);
}
private async Task ExecuteAsync(string name, bool allCaps)
{
name = allCaps ? name.ToUpper() : name;
await _context.CreateFile("hello_world.txt",
$"Hello {name}!",
"This file was generated by your custom command!"
);
}
}

View File

@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using MycroForge.Core.Contract;
using RootCommand = MycroForge.Core.RootCommand;
namespace MycroForge.PluginTemplate;
public class HelloWorldCommandPlugin : ICommandPlugin
{
public string Name => "MycroForge.PluginTemplate";
public void RegisterServices(IServiceCollection services)
{
services.AddScoped<ISubCommandOf<RootCommand>, HelloWorldCommand>();
}
}

View File

@ -6,4 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Content Include=".template.config\template.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MycroForge.Core" Version="1.0.0" />
</ItemGroup>
</Project>

View File

@ -6,9 +6,7 @@ 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}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MycroForge.PluginTemplate", "MycroForge.PluginTemplate\MycroForge.PluginTemplate.csproj", "{114A2B34-D77E-42AE-ADAF-0CD68C7B8D32}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -32,9 +30,5 @@ Global
{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

13
nuget.docker-compose.yml Normal file
View File

@ -0,0 +1,13 @@
version: '3.8'
services:
nuget:
image: bagetter/bagetter:latest
container_name: nuget
volumes:
- 'nuget_data:/var/baget'
ports:
- '5555:8080'
volumes:
nuget_data: