From 10822b3b4f4a7343cbf9eec6773f611e443e07b2 Mon Sep 17 00:00:00 2001 From: Chris R Date: Tue, 20 Oct 2015 16:59:26 -0700 Subject: [PATCH] #411 Move webroot from project.json to config. --- .../HostingEnvironmentExtensions.cs | 14 +++++++-- .../Internal/HostingUtilities.cs | 31 ------------------- .../WebHostBuilder.cs | 5 +-- src/Microsoft.AspNet.Hosting/project.json | 1 - .../HostingEngineTests.cs | 13 ++++++-- .../HostingUtilitiesTests.cs | 18 ----------- .../project.json | 3 +- .../TestServerTests.cs | 2 +- 8 files changed, 25 insertions(+), 62 deletions(-) delete mode 100644 src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs delete mode 100644 test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs diff --git a/src/Microsoft.AspNet.Hosting/HostingEnvironmentExtensions.cs b/src/Microsoft.AspNet.Hosting/HostingEnvironmentExtensions.cs index 98a839afdc..59aae860dc 100644 --- a/src/Microsoft.AspNet.Hosting/HostingEnvironmentExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/HostingEnvironmentExtensions.cs @@ -3,20 +3,28 @@ using System.IO; using Microsoft.AspNet.FileProviders; -using Microsoft.AspNet.Hosting.Internal; +using Microsoft.Extensions.Configuration; namespace Microsoft.AspNet.Hosting { public static class HostingEnvironmentExtensions { - public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, string environmentName) + private const string OldEnvironmentKey = "ASPNET_ENV"; + private const string EnvironmentKey = "Hosting:Environment"; + + private const string WebRootKey = "Hosting:WebRoot"; + + public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, IConfiguration config) { - hostingEnvironment.WebRootPath = HostingUtilities.GetWebRoot(applicationBasePath); + var webRoot = config?[WebRootKey] ?? string.Empty; + hostingEnvironment.WebRootPath = Path.Combine(applicationBasePath, webRoot); if (!Directory.Exists(hostingEnvironment.WebRootPath)) { Directory.CreateDirectory(hostingEnvironment.WebRootPath); } hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath); + + var environmentName = config?[EnvironmentKey] ?? config?[OldEnvironmentKey]; hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName; } } diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs deleted file mode 100644 index 305888997d..0000000000 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs +++ /dev/null @@ -1,31 +0,0 @@ -// 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 Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace Microsoft.AspNet.Hosting.Internal -{ - public static class HostingUtilities - { - public static string GetWebRoot(string applicationBasePath) - { - var webroot = applicationBasePath; - using (var stream = File.OpenRead(Path.Combine(applicationBasePath, "project.json"))) - { - using (var reader = new JsonTextReader(new StreamReader(stream))) - { - var project = JObject.Load(reader); - JToken token; - if (project.TryGetValue("webroot", out token)) - { - webroot = Path.Combine(applicationBasePath, token.ToString()); - } - } - } - return Path.GetFullPath(webroot); - } - } -} diff --git a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs index ef7aeb0ecb..d19ca7668b 100644 --- a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs @@ -21,9 +21,6 @@ namespace Microsoft.AspNet.Hosting { public class WebHostBuilder { - public const string OldEnvironmentKey = "ASPNET_ENV"; - public const string EnvironmentKey = "Hosting:Environment"; - public const string OldApplicationKey = "app"; public const string ApplicationKey = "Hosting:Application"; @@ -115,7 +112,7 @@ namespace Microsoft.AspNet.Hosting var appEnvironment = hostingContainer.GetRequiredService(); var startupLoader = hostingContainer.GetRequiredService(); - _hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _config?[EnvironmentKey] ?? _config?[OldEnvironmentKey]); + _hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _config); var engine = new HostingEngine(hostingServices, startupLoader, _config, _captureStartupErrors); // Only one of these should be set, but they are used in priority diff --git a/src/Microsoft.AspNet.Hosting/project.json b/src/Microsoft.AspNet.Hosting/project.json index 01bf705d81..148bec393f 100644 --- a/src/Microsoft.AspNet.Hosting/project.json +++ b/src/Microsoft.AspNet.Hosting/project.json @@ -23,7 +23,6 @@ "Microsoft.Extensions.Logging": "1.0.0-*", "Microsoft.Dnx.Compilation.Abstractions": "1.0.0-*", "Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*", - "Newtonsoft.Json": "6.0.6", "System.Diagnostics.DiagnosticSource": "4.0.0-beta-*" }, "frameworks": { diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 11fa37b5fe..afd3a1fc94 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -248,9 +248,18 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebRootCanBeResolvedFromTheProjectJson() + public void WebRootCanBeResolvedFromTheConfig() { - var engine = CreateBuilder().UseServer(this).Build(); + var vals = new Dictionary + { + { "Hosting:WebRoot", "testroot" } + }; + + var builder = new ConfigurationBuilder() + .AddInMemoryCollection(vals); + var config = builder.Build(); + + var engine = CreateBuilder(config).UseServer(this).Build(); var env = engine.ApplicationServices.GetRequiredService(); Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath); Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists); diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs deleted file mode 100644 index 86de386a1e..0000000000 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.AspNet.Hosting.Internal; -using Xunit; - -namespace Microsoft.AspNet.Hosting.Tests -{ - public class HostingUtilitiesTests - { - [Fact] - public void ReadWebRootFromProjectJson() - { - var root = HostingUtilities.GetWebRoot("."); - Assert.True(root.EndsWith("testroot")); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/project.json b/test/Microsoft.AspNet.Hosting.Tests/project.json index f4cb38366d..935b44acbd 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/project.json +++ b/test/Microsoft.AspNet.Hosting.Tests/project.json @@ -13,6 +13,5 @@ }, "commands": { "test": "xunit.runner.aspnet" - }, - "webroot": "testroot" + } } diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index 626ae24631..ffbdb0c470 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -344,7 +344,7 @@ namespace Microsoft.AspNet.TestHost } [Fact] - public void WebRootCanBeResolvedWhenNotInTheProjectJson() + public void WebRootCanBeResolvedWhenNotInTheConfig() { TestServer server = TestServer.Create(app => {