Change onBeforeStartExternalProcess to a virtual method, so as to avoid expanding the set of constructor params in all hosting models

This commit is contained in:
SteveSandersonMS
2016-07-18 14:39:36 +01:00
parent 7119815d04
commit a14d9ba2df
3 changed files with 40 additions and 38 deletions

View File

@@ -32,15 +32,14 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private bool _disposed;
private int _portNumber;
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
: base(
EmbeddedResourceReader.Read(
typeof(HttpNodeInstance),
"/Content/Node/entrypoint-http.js"),
projectPath,
watchFileExtensions,
MakeCommandLineOptions(port),
onBeforeStartExternalProcess)
MakeCommandLineOptions(port))
{
_client = new HttpClient();
}

View File

@@ -31,11 +31,12 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
string entryPointScript,
string projectPath,
string[] watchFileExtensions,
string commandLineArguments,
Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
string commandLineArguments)
{
_entryPointScript = new StringAsTempFile(entryPointScript);
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess);
var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments);
_nodeProcess = LaunchNodeProcess(startInfo);
_watchFileExtensions = watchFileExtensions;
_fileSystemWatcher = BeginFileWatcher(projectPath);
ConnectToInputOutputStreams();
@@ -72,6 +73,37 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
protected abstract Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocationInfo);
// This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
string entryPointFilename, string projectPath, string commandLineArguments)
{
var startInfo = new ProcessStartInfo("node")
{
Arguments = "\"" + entryPointFilename + "\" " + (commandLineArguments ?? string.Empty),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = projectPath
};
// Append projectPath to NODE_PATH so it can locate node_modules
var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
if (existingNodePath != string.Empty)
{
existingNodePath += ":";
}
var nodePathValue = existingNodePath + Path.Combine(projectPath, "node_modules");
#if NET451
startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue;
#else
startInfo.Environment["NODE_PATH"] = nodePathValue;
#endif
return startInfo;
}
protected virtual void OnOutputDataReceived(string outputData)
{
Console.WriteLine("[Node] " + outputData);
@@ -112,36 +144,8 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
}
}
private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess)
private static Process LaunchNodeProcess(ProcessStartInfo startInfo)
{
var startInfo = new ProcessStartInfo("node")
{
Arguments = "\"" + entryPointFilename + "\" " + (commandLineArguments ?? string.Empty),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = projectPath
};
// Append projectPath to NODE_PATH so it can locate node_modules
var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
if (existingNodePath != string.Empty)
{
existingNodePath += ":";
}
var nodePathValue = existingNodePath + Path.Combine(projectPath, "node_modules");
#if NET451
startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue;
#else
startInfo.Environment["NODE_PATH"] = nodePathValue;
#endif
if (onBeforeStartExternalProcess != null)
{
onBeforeStartExternalProcess(startInfo);
}
var process = Process.Start(startInfo);
// On Mac at least, a killed child process is left open as a zombie until the parent

View File

@@ -36,14 +36,13 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private string _socketAddress;
private VirtualConnectionClient _virtualConnectionClient;
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null) : base(
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress) : base(
EmbeddedResourceReader.Read(
typeof(SocketNodeInstance),
"/Content/Node/entrypoint-socket.js"),
projectPath,
watchFileExtensions,
MakeNewCommandLineOptions(socketAddress),
onBeforeStartExternalProcess)
MakeNewCommandLineOptions(socketAddress))
{
_socketAddress = socketAddress;
}