Added OnBeforeStartExternalProcess callback which to NodeServicesOptions (and OutOfProcessNodeInstance, SocketNodeInstance and HttpNodeInstance) to configure environment of the node.exe process to be started, and the path to the node executable itself. Fixes #20

This commit is contained in:
thunder7553
2016-07-13 12:49:09 +02:00
committed by SteveSandersonMS
parent 057efb43c8
commit 7119815d04
4 changed files with 31 additions and 23 deletions

View File

@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.NodeServices
HostingModel = DefaultNodeHostingModel; HostingModel = DefaultNodeHostingModel;
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone(); WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
} }
public Action<System.Diagnostics.ProcessStartInfo> OnBeforeStartExternalProcess { get; set; }
public NodeHostingModel HostingModel { get; set; } public NodeHostingModel HostingModel { get; set; }
public Func<INodeInstance> NodeInstanceFactory { get; set; } public Func<INodeInstance> NodeInstanceFactory { get; set; }
public string ProjectPath { get; set; } public string ProjectPath { get; set; }

View File

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

View File

@@ -31,10 +31,11 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
string entryPointScript, string entryPointScript,
string projectPath, string projectPath,
string[] watchFileExtensions, string[] watchFileExtensions,
string commandLineArguments) string commandLineArguments,
Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
{ {
_entryPointScript = new StringAsTempFile(entryPointScript); _entryPointScript = new StringAsTempFile(entryPointScript);
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments); _nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess);
_watchFileExtensions = watchFileExtensions; _watchFileExtensions = watchFileExtensions;
_fileSystemWatcher = BeginFileWatcher(projectPath); _fileSystemWatcher = BeginFileWatcher(projectPath);
ConnectToInputOutputStreams(); ConnectToInputOutputStreams();
@@ -111,7 +112,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
} }
} }
private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments) private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess)
{ {
var startInfo = new ProcessStartInfo("node") var startInfo = new ProcessStartInfo("node")
{ {
@@ -136,6 +137,10 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
#else #else
startInfo.Environment["NODE_PATH"] = nodePathValue; startInfo.Environment["NODE_PATH"] = nodePathValue;
#endif #endif
if (onBeforeStartExternalProcess != null)
{
onBeforeStartExternalProcess(startInfo);
}
var process = Process.Start(startInfo); var process = Process.Start(startInfo);

View File

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