Skip to content

Commit 6cddb9a

Browse files
Lower the amout of public classes
1 parent 48717b6 commit 6cddb9a

File tree

8 files changed

+87
-33
lines changed

8 files changed

+87
-33
lines changed

nexus/Program.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Nexus.CommandLine;
77
using Nexus.Config;
8+
using Nexus.External.Apis.FileSystem;
9+
using Nexus.External.Apis.ProcessManagement;
810
using Nexus.Logging;
911
using Nexus.Startup;
1012

@@ -31,9 +33,11 @@ public static async Task<int> Main(string[] args)
3133
var cmd = new CommandLineContext(args);
3234

3335
var settings = new Settings();
36+
var filesystem = new FileSystem();
37+
var processManager = new ProcessManager();
3438

3539
// Use hosted service for ALL command types
36-
var host = CreateHostBuilder(cmd, settings).Build();
40+
var host = CreateHostBuilder(cmd, filesystem, processManager, settings).Build();
3741
await host.RunAsync();
3842

3943
return 0;
@@ -53,10 +57,12 @@ public static async Task<int> Main(string[] args)
5357
/// Creates and configures a host builder for the application.
5458
/// </summary>
5559
/// <param name="cmd">Command line context.</param>
60+
/// <param name="fileSystem">The file system abstraction.</param>
61+
/// <param name="processManager">The process manager abstraction.</param>
5662
/// <param name="settings">The product settings.</param>
5763
/// <returns>Configured host builder.</returns>
5864
[SupportedOSPlatform("windows")]
59-
internal static IHostBuilder CreateHostBuilder(CommandLineContext cmd, ISettings settings)
65+
internal static IHostBuilder CreateHostBuilder(CommandLineContext cmd, IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
6066
{
6167
var builder = Host.CreateDefaultBuilder(cmd.Args)
6268
.ConfigureLogging((context, logging) => logging.AddNexusLogging(
@@ -68,8 +74,9 @@ internal static IHostBuilder CreateHostBuilder(CommandLineContext cmd, ISettings
6874
var ununsed = services.AddSingleton(cmd);
6975

7076
// Register ONLY the main hosted service (no others)
71-
var ununsedHost = services.AddHostedService<MainHostedService>();
72-
});
77+
var ununsedHost = services.AddHostedService(sp =>
78+
new MainHostedService(cmd, fileSystem, processManager, settings));
79+
});
7380

7481
// Configure Windows Service support if in service mode
7582
if (cmd.IsServiceMode)

nexus/Startup/MainHostedService.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using Nexus.CommandLine;
66
using Nexus.Config;
7+
using Nexus.External.Apis.FileSystem;
8+
using Nexus.External.Apis.ProcessManagement;
79
using Nexus.Protocol;
810
using Nexus.Setup;
911

@@ -19,6 +21,8 @@ internal class MainHostedService : IHostedService
1921
{
2022
private readonly Logger m_Logger;
2123
private readonly ISettings m_Settings;
24+
private readonly IFileSystem m_FileSystem;
25+
private readonly IProcessManager m_ProcessManager;
2226
private readonly CommandLineContext m_CommandLineContext;
2327
private readonly IProtocolServer m_ProtocolServer;
2428
private readonly IProductInstallation m_ProductInstallation;
@@ -27,10 +31,12 @@ internal class MainHostedService : IHostedService
2731
/// Initializes a new instance of the <see cref="MainHostedService"/> class.
2832
/// </summary>
2933
/// <param name="commandLineContext">Command line context.</param>
34+
/// <param name="fileSystem">The file system abstraction.</param>
35+
/// <param name="processManager">The process manager abstraction.</param>
3036
/// <param name="settings">The product settings.</param>
3137
public MainHostedService(
32-
CommandLineContext commandLineContext, ISettings settings)
33-
: this(commandLineContext, new ProtocolServer(settings), new ProductInstallation(settings), settings)
38+
CommandLineContext commandLineContext, IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
39+
: this(commandLineContext, new ProtocolServer(fileSystem, processManager, settings), new ProductInstallation(settings), fileSystem, processManager, settings)
3440
{
3541
}
3642

@@ -40,15 +46,19 @@ public MainHostedService(
4046
/// <param name="commandLineContext">Command line context.</param>
4147
/// <param name="protocolServer">The command line server.</param>
4248
/// <param name="productInstallation">The product setup finctionality.</param>
49+
/// <param name="fileSystem">The file system abstraction.</param>
50+
/// <param name="processManager">The process manager abstraction.</param>
4351
/// <param name="settings">The product settings.</param>
4452
internal MainHostedService(
45-
CommandLineContext commandLineContext, IProtocolServer protocolServer, IProductInstallation productInstallation, ISettings settings)
53+
CommandLineContext commandLineContext, IProtocolServer protocolServer, IProductInstallation productInstallation, IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
4654
{
4755
m_Logger = LogManager.GetCurrentClassLogger();
4856
m_CommandLineContext = commandLineContext;
4957
m_ProtocolServer = protocolServer;
5058
m_ProductInstallation = productInstallation;
5159
m_Settings = settings;
60+
m_FileSystem = fileSystem;
61+
m_ProcessManager = processManager;
5262
}
5363

5464
/// <summary>

nexus_engine/nexus_engine/DebugEngine.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ public class DebugEngine : IDebugEngine
6767
/// <summary>
6868
/// Initializes a new instance of the <see cref="DebugEngine"/> class with default dependencies.
6969
/// </summary>
70+
/// <param name="fileSystem">The file system abstraction.</param>
71+
/// <param name="processManager">The process manager abstraction.</param>
7072
/// <param name="settings">The product settings.</param>
71-
public DebugEngine(ISettings settings)
72-
: this(new FileSystem(), new ProcessManager(), new BatchProcessor(settings), settings)
73+
public DebugEngine(IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
74+
: this(fileSystem, processManager, new BatchProcessor(settings), settings)
7375
{
7476
}
7577

@@ -87,7 +89,7 @@ internal DebugEngine(IFileSystem fileSystem, IProcessManager processManager, IBa
8789
m_Settings = settings ?? throw new ArgumentNullException(nameof(settings));
8890
m_BatchProcessor = batchProcessor;
8991

90-
m_ExtensionScripts = new ExtensionScripts(this, m_Settings);
92+
m_ExtensionScripts = new ExtensionScripts(this, fileSystem, processManager, m_Settings);
9193

9294
m_Logger = LogManager.GetCurrentClassLogger();
9395
m_Logger.Info("DebugEngine initialized with max {MaxSessions} concurrent sessions", m_Settings.Get().McpNexus.SessionManagement.MaxConcurrentSessions);

nexus_engine/nexus_engine_extensions/ExtensionScripts.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ public class ExtensionScripts : IExtensionScripts, IAsyncDisposable
8080
/// Initializes a new instance of the <see cref="ExtensionScripts"/> class with default dependencies.
8181
/// </summary>
8282
/// <param name="engine">The debug engine instance for callback operations.</param>
83+
/// <param name="fileSystem">The file system abstraction.</param>
84+
/// <param name="processManager">The process manager abstraction.</param>
8385
/// <param name="settings">The product settings.</param>
84-
public ExtensionScripts(IDebugEngine engine, ISettings settings)
85-
: this(engine, new TokenValidator(), settings)
86+
public ExtensionScripts(
87+
IDebugEngine engine,
88+
IFileSystem fileSystem,
89+
IProcessManager processManager,
90+
ISettings settings)
91+
: this(engine, new TokenValidator(), fileSystem, processManager, settings)
8692
{
8793
}
8894

@@ -91,9 +97,16 @@ public ExtensionScripts(IDebugEngine engine, ISettings settings)
9197
/// </summary>
9298
/// <param name="engine">The debug engine instance for callback operations.</param>
9399
/// <param name="tokenValidator">The token validator for validating extension script callbacks.</param>
100+
/// <param name="fileSystem">The file system abstraction.</param>
101+
/// <param name="processManager">The process manager abstraction.</param>
94102
/// <param name="settings">The product settings.</param>
95-
internal ExtensionScripts(IDebugEngine engine, TokenValidator tokenValidator, ISettings settings)
96-
: this(engine, tokenValidator, new CallbackServerManager(engine, tokenValidator, settings), new FileSystem(), new ProcessManager(), settings)
103+
internal ExtensionScripts(
104+
IDebugEngine engine,
105+
TokenValidator tokenValidator,
106+
IFileSystem fileSystem,
107+
IProcessManager processManager,
108+
ISettings settings)
109+
: this(engine, tokenValidator, new CallbackServerManager(engine, tokenValidator, settings), fileSystem, processManager, settings)
97110
{
98111
}
99112

nexus_protocol/ProtocolServer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Microsoft.Extensions.Hosting;
33

44
using Nexus.Config;
5+
using Nexus.External.Apis.FileSystem;
6+
using Nexus.External.Apis.ProcessManagement;
57
using Nexus.Protocol.Configuration;
68
using Nexus.Protocol.Services;
79

@@ -26,14 +28,16 @@ public class ProtocolServer : IProtocolServer
2628
/// <summary>
2729
/// Initializes a new instance of the <see cref="ProtocolServer"/> class.
2830
/// </summary>
31+
/// <param name="fileSystem">The file system abstraction.</param>
32+
/// <param name="processManager">The process manager abstraction.</param>
2933
/// <param name="settings">The product settings.</param>
3034
/// <exception cref="ArgumentNullException">Thrown when serviceProvider is null.</exception>
31-
public ProtocolServer(ISettings settings)
35+
public ProtocolServer(IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
3236
{
3337
IsRunning = false;
3438
m_Logger = LogManager.GetCurrentClassLogger();
3539
m_Settings = settings;
36-
EngineService.Initialize(m_Settings);
40+
EngineService.Initialize(fileSystem, processManager, m_Settings);
3741
}
3842

3943
/// <summary>

nexus_protocol/Services/EngineService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Nexus.Config;
22
using Nexus.Engine;
33
using Nexus.Engine.Share;
4+
using Nexus.External.Apis.FileSystem;
5+
using Nexus.External.Apis.ProcessManagement;
46

57
namespace Nexus.Protocol.Services
68
{
@@ -11,7 +13,7 @@ namespace Nexus.Protocol.Services
1113
internal static class EngineService
1214
{
1315
/// <summary>
14-
/// The debug engine instance. Null until initialized via <see cref="Initialize(ISettings)"/>.
16+
/// The debug engine instance. Null until initialized via <see cref="Initialize(IFileSystem, IProcessManager, ISettings)"/>.
1517
/// </summary>
1618
private static IDebugEngine? m_DebugEngine;
1719

@@ -25,7 +27,7 @@ internal static class EngineService
2527
/// Retrieves the debug engine instance in a thread-safe manner.
2628
/// </summary>
2729
/// <returns>The initialized debug engine instance.</returns>
28-
/// <exception cref="InvalidOperationException">Thrown when the debug engine has not been initialized via <see cref="Initialize(ISettings)"/>.</exception>
30+
/// <exception cref="InvalidOperationException">Thrown when the debug engine has not been initialized via <see cref="Initialize(IFileSystem, IProcessManager, ISettings)"/>.</exception>
2931
public static IDebugEngine Get()
3032
{
3133
m_DebugEngineLock.EnterReadLock();
@@ -43,14 +45,16 @@ public static IDebugEngine Get()
4345
/// Initializes the debug engine instance with the specified settings.
4446
/// This method must be called before using <see cref="Get()"/> to retrieve the engine.
4547
/// </summary>
46-
/// <param name="settings">The settings to use for initializing the debug engine.</param>
48+
/// <param name="fileSystem">The file system abstraction.</param>
49+
/// <param name="processManager">The process manager abstraction.</param>
50+
/// <param name="settings">The product settings.</param>
4751
/// <exception cref="ArgumentNullException">Thrown when <paramref name="settings"/> is null.</exception>
48-
public static void Initialize(ISettings settings)
52+
public static void Initialize(IFileSystem fileSystem, IProcessManager processManager, ISettings settings)
4953
{
5054
m_DebugEngineLock.EnterWriteLock();
5155
try
5256
{
53-
m_DebugEngine = new DebugEngine(settings);
57+
m_DebugEngine = new DebugEngine(fileSystem, processManager, settings);
5458
}
5559
finally
5660
{

unittests/nexus_unittests/ProgramTests.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Nexus.CommandLine;
99
using Nexus.Config;
1010
using Nexus.Config.Models;
11+
using Nexus.External.Apis.FileSystem;
12+
using Nexus.External.Apis.ProcessManagement;
1113
using Nexus.Startup;
1214

1315
using Xunit;
@@ -20,13 +22,18 @@ namespace Nexus.Tests;
2022
public class ProgramTests
2123
{
2224
private readonly Mock<ISettings> m_Settings;
25+
private readonly Mock<IFileSystem> m_FileSystem;
26+
private readonly Mock<IProcessManager> m_ProcessManager;
2327

2428
/// <summary>
2529
/// Initializes a new instance of the <see cref="ProgramTests"/> class.
2630
/// </summary>
2731
public ProgramTests()
2832
{
2933
m_Settings = new Mock<ISettings>();
34+
m_FileSystem = new Mock<IFileSystem>();
35+
m_ProcessManager = new Mock<IProcessManager>();
36+
3037
var sharedConfig = new SharedConfiguration
3138
{
3239
McpNexus = new McpNexusSettings
@@ -50,7 +57,7 @@ public void CreateHostBuilder_WithHttpMode_CreatesHostBuilder()
5057
var context = new CommandLineContext(new[] { "--http" });
5158

5259
// Act
53-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
60+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
5461

5562
// Assert
5663
_ = hostBuilder.Should().NotBeNull();
@@ -66,7 +73,7 @@ public void CreateHostBuilder_WithStdioMode_CreatesHostBuilder()
6673
var context = new CommandLineContext(new[] { "--stdio" });
6774

6875
// Act
69-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
76+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
7077

7178
// Assert
7279
_ = hostBuilder.Should().NotBeNull();
@@ -82,7 +89,7 @@ public void CreateHostBuilder_WithServiceMode_CreatesHostBuilder()
8289
var context = new CommandLineContext(new[] { "--service" });
8390

8491
// Act
85-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
92+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
8693

8794
// Assert
8895
_ = hostBuilder.Should().NotBeNull();
@@ -98,7 +105,7 @@ public void CreateHostBuilder_RegistersCommandLineContext()
98105
var context = new CommandLineContext(Array.Empty<string>());
99106

100107
// Act
101-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
108+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
102109
var host = hostBuilder.Build();
103110
var registeredContext = host.Services.GetService<CommandLineContext>();
104111

@@ -117,7 +124,7 @@ public void CreateHostBuilder_RegistersMainHostedService()
117124
var context = new CommandLineContext(Array.Empty<string>());
118125

119126
// Act
120-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
127+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
121128
_ = hostBuilder.ConfigureServices(services => services.AddSingleton(m_Settings.Object));
122129
var host = hostBuilder.Build();
123130
var hostedServices = host.Services.GetServices<IHostedService>();
@@ -136,7 +143,7 @@ public void CreateHostBuilder_WithEmptyArgs_CreatesHostBuilder()
136143
var context = new CommandLineContext(Array.Empty<string>());
137144

138145
// Act
139-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
146+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
140147

141148
// Assert
142149
_ = hostBuilder.Should().NotBeNull();
@@ -152,7 +159,7 @@ public void CreateHostBuilder_WithInstallMode_CreatesHostBuilder()
152159
var context = new CommandLineContext(new[] { "--install" });
153160

154161
// Act
155-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
162+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
156163

157164
// Assert
158165
_ = hostBuilder.Should().NotBeNull();
@@ -168,7 +175,7 @@ public void CreateHostBuilder_WithUpdateMode_CreatesHostBuilder()
168175
var context = new CommandLineContext(new[] { "--update" });
169176

170177
// Act
171-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
178+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
172179

173180
// Assert
174181
_ = hostBuilder.Should().NotBeNull();
@@ -184,7 +191,7 @@ public void CreateHostBuilder_WithUninstallMode_CreatesHostBuilder()
184191
var context = new CommandLineContext(new[] { "--uninstall" });
185192

186193
// Act
187-
var hostBuilder = Program.CreateHostBuilder(context, m_Settings.Object);
194+
var hostBuilder = Program.CreateHostBuilder(context, m_FileSystem.Object, m_ProcessManager.Object, m_Settings.Object);
188195

189196
// Assert
190197
_ = hostBuilder.Should().NotBeNull();

0 commit comments

Comments
 (0)