From 21e5218ea3ee1d9bc6206e67088846e2f6162025 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Fri, 24 Oct 2025 19:33:18 +1000 Subject: [PATCH] replace more console logs --- .../Features/Analysis/RoslynAnalysis.cs | 9 +++++---- .../Features/Build/BuildService.cs | 12 +++++------- src/SharpIDE.Godot/IdeRoot.cs | 1 - 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs index 356ba99..caa6b89 100644 --- a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs +++ b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs @@ -33,9 +33,10 @@ using DiagnosticSeverity = Microsoft.CodeAnalysis.DiagnosticSeverity; namespace SharpIDE.Application.Features.Analysis; -public class RoslynAnalysis(ILogger logger) +public class RoslynAnalysis(ILogger logger, BuildService buildService) { private readonly ILogger _logger = logger; + private readonly BuildService _buildService = buildService; public static AdhocWorkspace? _workspace; private static CustomMsBuildProjectLoader? _msBuildProjectLoader; @@ -99,7 +100,7 @@ public class RoslynAnalysis(ILogger logger) //_msBuildProjectLoader!.LoadMetadataForReferencedProjects = true; // MsBuildProjectLoader doesn't do a restore which is absolutely required for resolving PackageReferences, if they have changed. I am guessing it just reads from project.assets.json - await BuildService.Instance.MsBuildAsync(_sharpIdeSolutionModel.FilePath, BuildType.Restore, cancellationToken); + await _buildService.MsBuildAsync(_sharpIdeSolutionModel.FilePath, BuildType.Restore, cancellationToken); var solutionInfo = await _msBuildProjectLoader!.LoadSolutionInfoAsync(_sharpIdeSolutionModel.FilePath, cancellationToken: cancellationToken); _workspace.ClearSolution(); var solution = _workspace.AddSolution(solutionInfo); @@ -171,7 +172,7 @@ public class RoslynAnalysis(ILogger logger) Guard.Against.Null(_msBuildProjectLoader, nameof(_msBuildProjectLoader)); // It is important to note that a Workspace has no concept of MSBuild, nuget packages etc. It is just told about project references and "metadata" references, which are dlls. This is the what MSBuild does - it reads the csproj, and most importantly resolves nuget package references to dlls - await BuildService.Instance.MsBuildAsync(_sharpIdeSolutionModel!.FilePath, BuildType.Restore, cancellationToken); + await _buildService.MsBuildAsync(_sharpIdeSolutionModel!.FilePath, BuildType.Restore, cancellationToken); var __ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.MSBuildProjectLoader.LoadSolutionInfoAsync"); // This call is the expensive part - MSBuild is slow. There doesn't seem to be any incrementalism for solutions. // The best we could do to speed it up is do .LoadProjectInfoAsync for the single project, and somehow munge that into the existing solution @@ -197,7 +198,7 @@ public class RoslynAnalysis(ILogger logger) Guard.Against.Null(_workspace, nameof(_workspace)); Guard.Against.Null(_msBuildProjectLoader, nameof(_msBuildProjectLoader)); - await BuildService.Instance.MsBuildAsync(_sharpIdeSolutionModel!.FilePath, BuildType.Restore, cancellationToken); + await _buildService.MsBuildAsync(_sharpIdeSolutionModel!.FilePath, BuildType.Restore, cancellationToken); var __ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(CustomMsBuildProjectLoader)}.{nameof(CustomMsBuildProjectLoader.LoadProjectInfosAsync)}"); var thisProject = _workspace.CurrentSolution.Projects.Single(s => s.FilePath == projectModel.FilePath); diff --git a/src/SharpIDE.Application/Features/Build/BuildService.cs b/src/SharpIDE.Application/Features/Build/BuildService.cs index 542bf02..ee6769b 100644 --- a/src/SharpIDE.Application/Features/Build/BuildService.cs +++ b/src/SharpIDE.Application/Features/Build/BuildService.cs @@ -3,6 +3,7 @@ using Ardalis.GuardClauses; using Microsoft.Build.Execution; using Microsoft.Build.Framework; using Microsoft.Build.Logging; +using Microsoft.Extensions.Logging; using SharpIDE.Application.Features.Logging; namespace SharpIDE.Application.Features.Build; @@ -14,9 +15,10 @@ public enum BuildType Clean, Restore } -public class BuildService +public class BuildService(ILogger logger) { - public static BuildService Instance { get; set; } = null!; + private readonly ILogger _logger = logger; + public event Func BuildStarted = () => Task.CompletedTask; public ChannelTextWriter BuildTextWriter { get; } = new ChannelTextWriter(); public async Task MsBuildAsync(string solutionOrProjectFilePath, BuildType buildType = BuildType.Build, CancellationToken cancellationToken = default) @@ -54,11 +56,7 @@ public class BuildService var timer = Stopwatch.StartNew(); var buildResult = await BuildManager.DefaultBuildManager.BuildAsync(buildParameters, buildRequest, cancellationToken).ConfigureAwait(false); timer.Stop(); - Console.WriteLine($"Build result: {buildResult.OverallResult} in {timer.ElapsedMilliseconds}ms"); - if (buildResult.OverallResult != BuildResultCode.Success) - { - Console.WriteLine($"{buildResult.Exception}"); - } + _logger.LogInformation(buildResult.Exception, "Build result: {BuildResult} in {ElapsedMilliseconds}ms", buildResult.OverallResult, timer.ElapsedMilliseconds); } private static string[] TargetsToBuild(BuildType buildType) diff --git a/src/SharpIDE.Godot/IdeRoot.cs b/src/SharpIDE.Godot/IdeRoot.cs index ed8bf92..a5995dd 100644 --- a/src/SharpIDE.Godot/IdeRoot.cs +++ b/src/SharpIDE.Godot/IdeRoot.cs @@ -51,7 +51,6 @@ public partial class IdeRoot : Control { GodotGlobalEvents.Instance = new GodotGlobalEvents(); GlobalEvents.Instance = new GlobalEvents(); - BuildService.Instance = new BuildService(); // TODO: Sort out this mess with singletons, especially access across Application services } public override void _ExitTree()