Design review: Change AddNodeServices to take an Action<NodeServicesOptions> like other aspects of MVC DI config

This commit is contained in:
SteveSandersonMS
2016-09-01 17:46:59 +01:00
parent f04fb8c421
commit f0d954b2a6
11 changed files with 150 additions and 141 deletions

View File

@@ -2,21 +2,45 @@ using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.NodeServices.HostingModels;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging.Console;
namespace Microsoft.AspNetCore.NodeServices
{
public class NodeServicesOptions
{
public const NodeHostingModel DefaultNodeHostingModel = NodeHostingModel.Http;
private const string LogCategoryName = "Microsoft.AspNetCore.NodeServices";
private static readonly string[] DefaultWatchFileExtensions = { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
public NodeServicesOptions()
public NodeServicesOptions(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof (serviceProvider));
}
EnvironmentVariables = new Dictionary<string, string>();
HostingModel = DefaultNodeHostingModel;
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
// In an ASP.NET environment, we can use the IHostingEnvironment data to auto-populate a few
// things that you'd otherwise have to specify manually
var hostEnv = serviceProvider.GetService<IHostingEnvironment>();
if (hostEnv != null)
{
ProjectPath = hostEnv.ContentRootPath;
EnvironmentVariables["NODE_ENV"] = hostEnv.IsDevelopment() ? "development" : "production"; // De-facto standard values for Node
}
// If the DI system gives us a logger, use it. Otherwise, set up a default one.
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
NodeInstanceOutputLogger = loggerFactory != null
? loggerFactory.CreateLogger(LogCategoryName)
: new ConsoleLogger(LogCategoryName, null, false);
}
public Action<System.Diagnostics.ProcessStartInfo> OnBeforeStartExternalProcess { get; set; }
public NodeHostingModel HostingModel { get; set; }
public Func<INodeInstance> NodeInstanceFactory { get; set; }
public string ProjectPath { get; set; }
@@ -24,6 +48,6 @@ namespace Microsoft.AspNetCore.NodeServices
public ILogger NodeInstanceOutputLogger { get; set; }
public bool LaunchWithDebugging { get; set; }
public IDictionary<string, string> EnvironmentVariables { get; set; }
public int? DebuggingPort { get; set; }
public int DebuggingPort { get; set; }
}
}