From 50eae5e275666f31163316ddf5467581113f4cdc Mon Sep 17 00:00:00 2001
From: acite <1498045907@qq.com>
Date: Sun, 5 Oct 2025 11:41:06 +0800
Subject: [PATCH] [feat] Abyssctl Automatic module discovery
---
.idea/.idea.Abyss/.idea/workspace.xml | 37 +++++++++-----------------
Abyss.sln.DotSettings.user | 1 +
abyssctl/App/App.cs | 22 ++++++++++++---
abyssctl/App/Interfaces/IOptions.cs | 6 +++++
abyssctl/App/Modules/HelloOptions.cs | 10 ++++---
abyssctl/App/Modules/VersionOptions.cs | 5 ++--
6 files changed, 47 insertions(+), 34 deletions(-)
create mode 100644 abyssctl/App/Interfaces/IOptions.cs
diff --git a/.idea/.idea.Abyss/.idea/workspace.xml b/.idea/.idea.Abyss/.idea/workspace.xml
index 21de6d0..5196781 100644
--- a/.idea/.idea.Abyss/.idea/workspace.xml
+++ b/.idea/.idea.Abyss/.idea/workspace.xml
@@ -11,29 +11,12 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
@@ -59,6 +42,8 @@
+
+
@@ -101,6 +86,7 @@
+
@@ -122,7 +108,7 @@
".NET Launch Settings Profile.Abyss: http.executor": "Debug",
".NET Launch Settings Profile.Abyss: https.executor": "Debug",
".NET Project.AbyssCli.executor": "Run",
- ".NET Project.abyssctl.executor": "Run",
+ ".NET Project.abyssctl.executor": "Debug",
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
"ModuleVcsDetector.initialDetectionPerformed": "true",
"Publish to folder.Publish Abyss to folder x86.executor": "Run",
@@ -142,7 +128,7 @@
"vue.rearranger.settings.migration": "true"
}
}]]>
-
+
@@ -153,7 +139,7 @@
-
+
@@ -288,7 +274,8 @@
-
+
+
diff --git a/Abyss.sln.DotSettings.user b/Abyss.sln.DotSettings.user
index 726d3e2..f5d1b68 100644
--- a/Abyss.sln.DotSettings.user
+++ b/Abyss.sln.DotSettings.user
@@ -6,6 +6,7 @@
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
diff --git a/abyssctl/App/App.cs b/abyssctl/App/App.cs
index 70b05e0..d7f19da 100644
--- a/abyssctl/App/App.cs
+++ b/abyssctl/App/App.cs
@@ -1,6 +1,8 @@
using System.Net.Sockets;
+using System.Reflection;
using System.Text;
+using abyssctl.App.Interfaces;
using abyssctl.App.Modules;
using abyssctl.Model;
using abyssctl.Static;
@@ -39,10 +41,24 @@ public class App
{
return await Task.Run(() =>
{
- return Parser.Default.ParseArguments(args)
+ Assembly assembly = Assembly.GetExecutingAssembly();
+ Type attributeType = typeof(VerbAttribute);
+ const string targetNamespace = "abyssctl.App.Modules";
+
+ var moduleTypes = assembly.GetTypes()
+ .Where(t => t is { IsClass: true, IsAbstract: false, IsInterface: false })
+ .Where(t => t.Namespace == targetNamespace)
+ .Where(t => typeof(IOptions).IsAssignableFrom(t))
+ .Where(t => t.IsDefined(attributeType, inherit: true))
+ .ToArray();
+
+ return Parser.Default.ParseArguments(args, moduleTypes)
.MapResult(
- (HelloOptions opt) => HelloOptions.Run(opt),
- (VersionOptions opt) => VersionOptions.Run(opt),
+ (object obj) =>
+ {
+ var s = (obj as IOptions)?.Run().GetAwaiter().GetResult();
+ return s!.Value;
+ },
_ => 1);
});
}
diff --git a/abyssctl/App/Interfaces/IOptions.cs b/abyssctl/App/Interfaces/IOptions.cs
new file mode 100644
index 0000000..9b014b6
--- /dev/null
+++ b/abyssctl/App/Interfaces/IOptions.cs
@@ -0,0 +1,6 @@
+namespace abyssctl.App.Interfaces;
+
+public interface IOptions
+{
+ public Task Run();
+}
\ No newline at end of file
diff --git a/abyssctl/App/Modules/HelloOptions.cs b/abyssctl/App/Modules/HelloOptions.cs
index 610a37a..b765f84 100644
--- a/abyssctl/App/Modules/HelloOptions.cs
+++ b/abyssctl/App/Modules/HelloOptions.cs
@@ -1,18 +1,20 @@
+using abyssctl.App.Interfaces;
using abyssctl.Model;
using CommandLine;
namespace abyssctl.App.Modules;
[Verb("hello", HelpText = "Say hello to abyss server")]
-public class HelloOptions
+public class HelloOptions: IOptions
{
- public static int Run(HelloOptions opts)
+ public async Task Run()
{
- var r = App.CtlWriteRead(new Ctl
+ var r = await App.CtlWriteRead(new Ctl
{
Head = 100,
Params = []
- }).GetAwaiter().GetResult();
+ });
+
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
return 0;
diff --git a/abyssctl/App/Modules/VersionOptions.cs b/abyssctl/App/Modules/VersionOptions.cs
index ea2e9a6..3615a87 100644
--- a/abyssctl/App/Modules/VersionOptions.cs
+++ b/abyssctl/App/Modules/VersionOptions.cs
@@ -1,11 +1,12 @@
+using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Modules;
[Verb("ver", HelpText = "Get server version")]
-public class VersionOptions
+public class VersionOptions: IOptions
{
- public static int Run(VersionOptions opts)
+ public async Task Run()
{
Console.WriteLine("Version");
return 0;