replace deprecated aspire class

This commit is contained in:
Matt Parker
2025-12-19 10:57:12 +10:00
parent f662437310
commit 8f96163e61

View File

@@ -1,4 +1,5 @@
using Aspire.Hosting.Lifecycle; using Aspire.Hosting.Eventing;
using Aspire.Hosting.Lifecycle;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -42,14 +43,14 @@ public static class GodotExtensions
command: godotPath, command: godotPath,
workingDirectory: projectDirectory, workingDirectory: projectDirectory,
args: arguments.ToArray()); args: arguments.ToArray());
//.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") ?? throw new InvalidOperationException("OTEL_EXPORTER_OTLP_ENDPOINT environment variable is not set")); //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") ?? throw new InvalidOperationException("OTEL_EXPORTER_OTLP_ENDPOINT environment variable is not set"));
// Add a lifecycle hook to handle building before startup // Add a lifecycle hook to handle building before startup
builder.Services.AddSingleton<IDistributedApplicationLifecycleHook>(sp => builder.Services.AddSingleton<IDistributedApplicationEventingSubscriber>(sp =>
new GodotBuildHook( new GodotBuildEventSubscriber(
projectPath, projectPath,
projectDirectory, projectDirectory,
sp.GetRequiredService<ILogger<GodotBuildHook>>())); sp.GetRequiredService<ILogger<GodotBuildEventSubscriber>>()));
return godotResource; return godotResource;
} }
@@ -83,73 +84,69 @@ public static class GodotExtensions
throw new PlatformNotSupportedException("Current platform is not supported for Godot execution."); throw new PlatformNotSupportedException("Current platform is not supported for Godot execution.");
} }
/// <summary> private class GodotBuildEventSubscriber : IDistributedApplicationEventingSubscriber
/// Lifecycle hook to build the Godot project before launching
/// </summary>
private class GodotBuildHook : IDistributedApplicationLifecycleHook
{ {
private readonly string _projectPath; private readonly string _projectPath;
private readonly string _projectDirectory; private readonly string _projectDirectory;
private readonly ILogger<GodotBuildHook> _logger; private readonly ILogger<GodotBuildEventSubscriber> _logger;
public GodotBuildHook(string projectPath, string projectDirectory, ILogger<GodotBuildHook> logger) public GodotBuildEventSubscriber(string projectPath, string projectDirectory, ILogger<GodotBuildEventSubscriber> logger)
{ {
_projectPath = projectPath; _projectPath = projectPath;
_projectDirectory = projectDirectory; _projectDirectory = projectDirectory;
_logger = logger; _logger = logger;
} }
public Task SubscribeAsync(IDistributedApplicationEventing eventing, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
public async Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
{ {
_logger.LogInformation("Building Godot project: {ProjectPath}", _projectPath); eventing.Subscribe<BeforeStartEvent>(async (@event, ct) =>
// Execute dotnet build on the project
var buildProcess = new System.Diagnostics.Process
{ {
StartInfo = new System.Diagnostics.ProcessStartInfo _logger.LogInformation("Building Godot project: {ProjectPath}", _projectPath);
// Execute dotnet build on the project
var buildProcess = new System.Diagnostics.Process
{ {
FileName = "dotnet", StartInfo = new System.Diagnostics.ProcessStartInfo
Arguments = $"build \"{_projectPath}\" --configuration Debug", {
UseShellExecute = false, FileName = "dotnet",
CreateNoWindow = true, Arguments = $"build \"{_projectPath}\" --configuration Debug",
RedirectStandardOutput = true, UseShellExecute = false,
RedirectStandardError = true, CreateNoWindow = true,
WorkingDirectory = _projectDirectory RedirectStandardOutput = true,
} RedirectStandardError = true,
}; WorkingDirectory = _projectDirectory
}
};
buildProcess.OutputDataReceived += (sender, e) => buildProcess.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{ {
_logger.LogInformation("[Godot Build] {Data}", e.Data); if (!string.IsNullOrEmpty(e.Data))
} {
}; _logger.LogInformation("[Godot Build] {Data}", e.Data);
}
};
buildProcess.ErrorDataReceived += (sender, e) => buildProcess.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{ {
_logger.LogError("[Godot Build] {Data}", e.Data); if (!string.IsNullOrEmpty(e.Data))
{
_logger.LogError("[Godot Build] {Data}", e.Data);
}
};
buildProcess.Start();
buildProcess.BeginOutputReadLine();
buildProcess.BeginErrorReadLine();
await buildProcess.WaitForExitAsync(ct);
if (buildProcess.ExitCode != 0)
{
_logger.LogError("Failed to build Godot project: {ProjectPath}", _projectPath);
return;
} }
};
buildProcess.Start(); _logger.LogInformation("Successfully built Godot project: {ProjectPath}", _projectPath);
buildProcess.BeginOutputReadLine(); });
buildProcess.BeginErrorReadLine();
await buildProcess.WaitForExitAsync(cancellationToken);
if (buildProcess.ExitCode != 0)
{
_logger.LogError("Failed to build Godot project: {ProjectPath}", _projectPath);
return;
}
_logger.LogInformation("Successfully built Godot project: {ProjectPath}", _projectPath);
}
public Task AfterStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
{
return Task.CompletedTask; return Task.CompletedTask;
} }
} }