mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-25 02:57:31 +00:00
Add new Microsoft.AspNetCore.SpaServices.Extensions package to host new runtime functionality needed for updated templates until 2.1 ships
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNetCore.NodeServices.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a <see cref="StreamReader"/> to expose an evented API, issuing notifications
|
||||
/// when the stream emits partial lines, completed lines, or finally closes.
|
||||
/// </summary>
|
||||
internal class EventedStreamReader
|
||||
{
|
||||
public delegate void OnReceivedChunkHandler(ArraySegment<char> chunk);
|
||||
public delegate void OnReceivedLineHandler(string line);
|
||||
public delegate void OnStreamClosedHandler();
|
||||
|
||||
public event OnReceivedChunkHandler OnReceivedChunk;
|
||||
public event OnReceivedLineHandler OnReceivedLine;
|
||||
public event OnStreamClosedHandler OnStreamClosed;
|
||||
|
||||
private readonly StreamReader _streamReader;
|
||||
private readonly StringBuilder _linesBuffer;
|
||||
|
||||
public EventedStreamReader(StreamReader streamReader)
|
||||
{
|
||||
_streamReader = streamReader ?? throw new ArgumentNullException(nameof(streamReader));
|
||||
_linesBuffer = new StringBuilder();
|
||||
Task.Factory.StartNew(Run);
|
||||
}
|
||||
|
||||
public Task<Match> WaitForMatch(Regex regex, TimeSpan timeout = default)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<Match>();
|
||||
var completionLock = new object();
|
||||
|
||||
OnReceivedLineHandler onReceivedLineHandler = null;
|
||||
OnStreamClosedHandler onStreamClosedHandler = null;
|
||||
|
||||
void ResolveIfStillPending(Action applyResolution)
|
||||
{
|
||||
lock (completionLock)
|
||||
{
|
||||
if (!tcs.Task.IsCompleted)
|
||||
{
|
||||
OnReceivedLine -= onReceivedLineHandler;
|
||||
OnStreamClosed -= onStreamClosedHandler;
|
||||
applyResolution();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onReceivedLineHandler = line =>
|
||||
{
|
||||
var match = regex.Match(line);
|
||||
if (match.Success)
|
||||
{
|
||||
ResolveIfStillPending(() => tcs.SetResult(match));
|
||||
}
|
||||
};
|
||||
|
||||
onStreamClosedHandler = () =>
|
||||
{
|
||||
ResolveIfStillPending(() => tcs.SetException(new EndOfStreamException()));
|
||||
};
|
||||
|
||||
OnReceivedLine += onReceivedLineHandler;
|
||||
OnStreamClosed += onStreamClosedHandler;
|
||||
|
||||
if (timeout != default)
|
||||
{
|
||||
var timeoutToken = new CancellationTokenSource(timeout);
|
||||
timeoutToken.Token.Register(() =>
|
||||
{
|
||||
ResolveIfStillPending(() => tcs.SetCanceled());
|
||||
});
|
||||
}
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private async Task Run()
|
||||
{
|
||||
var buf = new char[8 * 1024];
|
||||
while (true)
|
||||
{
|
||||
var chunkLength = await _streamReader.ReadAsync(buf, 0, buf.Length);
|
||||
if (chunkLength == 0)
|
||||
{
|
||||
OnClosed();
|
||||
break;
|
||||
}
|
||||
|
||||
OnChunk(new ArraySegment<char>(buf, 0, chunkLength));
|
||||
|
||||
var lineBreakPos = Array.IndexOf(buf, '\n', 0, chunkLength);
|
||||
if (lineBreakPos < 0)
|
||||
{
|
||||
_linesBuffer.Append(buf, 0, chunkLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
_linesBuffer.Append(buf, 0, lineBreakPos + 1);
|
||||
OnCompleteLine(_linesBuffer.ToString());
|
||||
_linesBuffer.Clear();
|
||||
_linesBuffer.Append(buf, lineBreakPos + 1, chunkLength - (lineBreakPos + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnChunk(ArraySegment<char> chunk)
|
||||
{
|
||||
var dlg = OnReceivedChunk;
|
||||
dlg?.Invoke(chunk);
|
||||
}
|
||||
|
||||
private void OnCompleteLine(string line)
|
||||
{
|
||||
var dlg = OnReceivedLine;
|
||||
dlg?.Invoke(line);
|
||||
}
|
||||
|
||||
private void OnClosed()
|
||||
{
|
||||
var dlg = OnStreamClosed;
|
||||
dlg?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.AspNetCore.NodeServices.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Captures the completed-line notifications from a <see cref="EventedStreamReader"/>,
|
||||
/// combining the data into a single <see cref="string"/>.
|
||||
/// </summary>
|
||||
internal class EventedStreamStringReader : IDisposable
|
||||
{
|
||||
private EventedStreamReader _eventedStreamReader;
|
||||
private bool _isDisposed;
|
||||
private StringBuilder _stringBuilder = new StringBuilder();
|
||||
|
||||
public EventedStreamStringReader(EventedStreamReader eventedStreamReader)
|
||||
{
|
||||
_eventedStreamReader = eventedStreamReader
|
||||
?? throw new ArgumentNullException(nameof(eventedStreamReader));
|
||||
_eventedStreamReader.OnReceivedLine += OnReceivedLine;
|
||||
}
|
||||
|
||||
public string ReadAsString() => _stringBuilder.ToString();
|
||||
|
||||
private void OnReceivedLine(string line) => _stringBuilder.AppendLine(line);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_eventedStreamReader.OnReceivedLine -= OnReceivedLine;
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.NodeServices.Util;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
// This is under the NodeServices namespace because post 2.1 it will be moved to that package
|
||||
namespace Microsoft.AspNetCore.NodeServices.Npm
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the <c>script</c> entries defined in a <c>package.json</c> file,
|
||||
/// capturing any output written to stdio.
|
||||
/// </summary>
|
||||
internal class NpmScriptRunner
|
||||
{
|
||||
public EventedStreamReader StdOut { get; }
|
||||
public EventedStreamReader StdErr { get; }
|
||||
|
||||
private static Regex AnsiColorRegex = new Regex("\x001b\\[[0-9;]*m", RegexOptions.None, TimeSpan.FromSeconds(1));
|
||||
|
||||
public NpmScriptRunner(string workingDirectory, string scriptName, string arguments)
|
||||
{
|
||||
if (string.IsNullOrEmpty(workingDirectory))
|
||||
{
|
||||
throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(scriptName))
|
||||
{
|
||||
throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
|
||||
}
|
||||
|
||||
var npmExe = "npm";
|
||||
var completeArguments = $"run {scriptName} -- {arguments ?? string.Empty}";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// On Windows, the NPM executable is a .cmd file, so it can't be executed
|
||||
// directly (except with UseShellExecute=true, but that's no good, because
|
||||
// it prevents capturing stdio). So we need to invoke it via "cmd /c".
|
||||
npmExe = "cmd";
|
||||
completeArguments = $"/c npm {completeArguments}";
|
||||
}
|
||||
|
||||
var process = LaunchNodeProcess(new ProcessStartInfo(npmExe)
|
||||
{
|
||||
Arguments = completeArguments,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
WorkingDirectory = workingDirectory
|
||||
});
|
||||
|
||||
StdOut = new EventedStreamReader(process.StandardOutput);
|
||||
StdErr = new EventedStreamReader(process.StandardError);
|
||||
}
|
||||
|
||||
public void AttachToLogger(ILogger logger)
|
||||
{
|
||||
// When the NPM task emits complete lines, pass them through to the real logger
|
||||
StdOut.OnReceivedLine += line =>
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
// NPM tasks commonly emit ANSI colors, but it wouldn't make sense to forward
|
||||
// those to loggers (because a logger isn't necessarily any kind of terminal)
|
||||
logger.LogInformation(StripAnsiColors(line));
|
||||
}
|
||||
};
|
||||
|
||||
StdErr.OnReceivedLine += line =>
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
logger.LogError(StripAnsiColors(line));
|
||||
}
|
||||
};
|
||||
|
||||
// But when it emits incomplete lines, assume this is progress information and
|
||||
// hence just pass it through to StdOut regardless of logger config.
|
||||
StdErr.OnReceivedChunk += chunk =>
|
||||
{
|
||||
var containsNewline = Array.IndexOf(
|
||||
chunk.Array, '\n', chunk.Offset, chunk.Count) >= 0;
|
||||
if (!containsNewline)
|
||||
{
|
||||
Console.Write(chunk.Array, chunk.Offset, chunk.Count);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static string StripAnsiColors(string line)
|
||||
=> AnsiColorRegex.Replace(line, string.Empty);
|
||||
|
||||
private static Process LaunchNodeProcess(ProcessStartInfo startInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
var process = Process.Start(startInfo);
|
||||
|
||||
// See equivalent comment in OutOfProcessNodeInstance.cs for why
|
||||
process.EnableRaisingEvents = true;
|
||||
|
||||
return process;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var message = $"Failed to start 'npm'. To resolve this:.\n\n"
|
||||
+ "[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.\n"
|
||||
+ $" Current PATH enviroment variable is: { Environment.GetEnvironmentVariable("PATH") }\n"
|
||||
+ " Make sure the executable is in one of those directories, or update your PATH.\n\n"
|
||||
+ "[2] See the InnerException for further details of the cause.";
|
||||
throw new InvalidOperationException(message, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user