Use Terminal Logger

This commit is contained in:
Matt Parker
2025-08-01 19:02:26 +10:00
parent abc4213500
commit e3fb90a870
6 changed files with 97 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text;
using System.Threading.Channels;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
@@ -17,18 +18,26 @@ public enum BuildType
public class BuildService
{
public event Func<Task> BuildStarted = () => Task.CompletedTask;
public Channel<string> BuildOutputChannel { get; } = Channel.CreateUnbounded<string>();
public ChannelTextWriter BuildTextWriter { get; } = new ChannelTextWriter();
public async Task MsBuildSolutionAsync(string solutionFilePath, BuildType buildType = BuildType.Build)
{
var normalOut = Console.Out;
Console.SetOut(BuildTextWriter);
var terminalLogger = InternalTerminalLoggerFactory.CreateLogger();
terminalLogger.Parameters = "FORCECONSOLECOLOR";
terminalLogger.Verbosity = LoggerVerbosity.Minimal;
var buildParameters = new BuildParameters
{
Loggers =
[
//new BinaryLogger { Parameters = "msbuild.binlog" },
new ConsoleLogger(LoggerVerbosity.Minimal, message => BuildOutputChannel.Writer.TryWrite(message), s => { }, () => { }),
//new ConsoleLogger(LoggerVerbosity.Minimal, message => BuildOutputChannel.Writer.TryWrite(message), s => { }, () => { }) {Parameters = "FORCECONSOLECOLOR"},
terminalLogger
//new InMemoryLogger(LoggerVerbosity.Normal)
],
};
Console.SetOut(normalOut);
string[] targetsToBuild = buildType switch
{
BuildType.Build => ["Restore", "Build"],

View File

@@ -0,0 +1,33 @@
using System.Text;
using System.Threading.Channels;
namespace SharpIDE.Application.Features.Logging;
// I could do this, or provide an XTermWriter. 🤷‍♂️
public class ChannelTextWriter : TextWriter
{
public override Encoding Encoding { get; } = Encoding.UTF8;
public Channel<string> ConsoleChannel { get; } = Channel.CreateUnbounded<string>();
public override void Write(char value)
{
ConsoleChannel.Writer.TryWrite(value.ToString());
}
public override void Write(string? value)
{
ConsoleChannel.Writer.TryWrite(value!);
}
public override void WriteLine(string? value)
{
throw new NotImplementedException();
}
public override void WriteLine(char value)
{
throw new NotImplementedException();
}
public override void WriteLine()
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,31 @@
using System.Reflection;
using Microsoft.Build.Framework;
namespace SharpIDE.Application.Features.Logging;
public class InternalTerminalLoggerFactory
{
public static ILogger CreateLogger()
{
var type = Type.GetType("Microsoft.Build.Logging.TerminalLogger, Microsoft.Build");
if (type == null) throw new Exception("TerminalLogger type not found");
var method = type.GetMethod(
"CreateTerminalOrConsoleLogger",
BindingFlags.NonPublic | BindingFlags.Static);
if (method == null) throw new Exception("CreateTerminalOrConsoleLogger method not found");
string[]? args = [];
bool supportsAnsi = true;
bool outputIsScreen = true;
uint? originalConsoleMode = 0x0007;
object? logger = method.Invoke(
obj: null,
parameters: [args, supportsAnsi, outputIsScreen, originalConsoleMode]);
return (ILogger)logger!; // This will be an ILogger (or INodeLogger) instance
}
}