Use Terminal Logger
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Channels;
|
using System.Threading.Channels;
|
||||||
using Microsoft.Build.Execution;
|
using Microsoft.Build.Execution;
|
||||||
using Microsoft.Build.Framework;
|
using Microsoft.Build.Framework;
|
||||||
@@ -17,18 +18,26 @@ public enum BuildType
|
|||||||
public class BuildService
|
public class BuildService
|
||||||
{
|
{
|
||||||
public event Func<Task> BuildStarted = () => Task.CompletedTask;
|
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)
|
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
|
var buildParameters = new BuildParameters
|
||||||
{
|
{
|
||||||
Loggers =
|
Loggers =
|
||||||
[
|
[
|
||||||
//new BinaryLogger { Parameters = "msbuild.binlog" },
|
//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)
|
//new InMemoryLogger(LoggerVerbosity.Normal)
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
Console.SetOut(normalOut);
|
||||||
|
|
||||||
string[] targetsToBuild = buildType switch
|
string[] targetsToBuild = buildType switch
|
||||||
{
|
{
|
||||||
BuildType.Build => ["Restore", "Build"],
|
BuildType.Build => ["Restore", "Build"],
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,35 +1,45 @@
|
|||||||
@using SharpIDE.Application.Features.Build
|
@using SharpIDE.Application.Features.Build
|
||||||
|
@using XtermBlazor
|
||||||
|
|
||||||
@inject BuildService BuildService
|
@inject BuildService BuildService
|
||||||
@inject IJSRuntime JsRuntime
|
@inject IJSRuntime JsRuntime
|
||||||
|
|
||||||
@implements IDisposable
|
@implements IDisposable
|
||||||
|
|
||||||
<div @ref="_logContainer" style="height: 15rem; overflow-y: auto; padding: 1rem; background-color: #303030; border: 1px solid #ccc;">
|
<div style="width: 100%">
|
||||||
@foreach (var line in _outputLines)
|
<Xterm @ref="@_terminalRef" Options="@_options" />
|
||||||
{
|
|
||||||
<MudText Typo="Typo.body2">@line</MudText>
|
|
||||||
}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private List<string> _outputLines = [];
|
private List<string> _outputLines = [];
|
||||||
private ElementReference _logContainer;
|
private ElementReference _logContainer;
|
||||||
|
|
||||||
|
private Xterm _terminalRef;
|
||||||
|
|
||||||
|
private TerminalOptions _options = new TerminalOptions
|
||||||
|
{
|
||||||
|
CursorBlink = true,
|
||||||
|
CursorStyle = CursorStyle.Bar,
|
||||||
|
Columns = 140,
|
||||||
|
Theme =
|
||||||
|
{
|
||||||
|
Background = "#303030",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
BuildService.BuildStarted += ClearPreviousOutput;
|
BuildService.BuildStarted += ClearPreviousOutput;
|
||||||
await foreach (var logLine in BuildService.BuildOutputChannel.Reader.ReadAllAsync())
|
await foreach (var logLine in BuildService.BuildTextWriter.ConsoleChannel.Reader.ReadAllAsync())
|
||||||
{
|
{
|
||||||
_outputLines.Add(logLine);
|
await _terminalRef.Write(logLine);
|
||||||
await InvokeAsync(StateHasChanged);
|
|
||||||
await ScrollToBottomAsync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ClearPreviousOutput()
|
private async Task ClearPreviousOutput()
|
||||||
{
|
{
|
||||||
_outputLines.Clear();
|
_outputLines.Clear();
|
||||||
|
await _terminalRef.Clear();
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebView" Version="9.0.7" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebView" Version="9.0.7" />
|
||||||
<PackageReference Include="MudBlazor" Version="8.10.0" />
|
<PackageReference Include="MudBlazor" Version="8.10.0" />
|
||||||
<PackageReference Include="Photino.Blazor" Version="4.0.13" />
|
<PackageReference Include="Photino.Blazor" Version="4.0.13" />
|
||||||
|
<PackageReference Include="XtermBlazor" Version="2.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
|
||||||
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
|
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
|
||||||
<link href="css/app.css" rel="stylesheet" />
|
<link href="css/app.css" rel="stylesheet" />
|
||||||
|
<link href="_content/XtermBlazor/XtermBlazor.min.css" rel="stylesheet" />
|
||||||
<link href="SharpIDE.Photino.styles.css" rel="stylesheet" />
|
<link href="SharpIDE.Photino.styles.css" rel="stylesheet" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="scripts2.js"></script>
|
<script src="scripts2.js"></script>
|
||||||
|
<script src="_content/XtermBlazor/XtermBlazor.min.js"></script>
|
||||||
<script src="_content/BlazorMonaco/jsInterop.js"></script>
|
<script src="_content/BlazorMonaco/jsInterop.js"></script>
|
||||||
<script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js"></script>
|
<script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js"></script>
|
||||||
<script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js"></script>
|
<script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js"></script>
|
||||||
|
|||||||
Reference in New Issue
Block a user