From 7a923d1d6fd105a3a7bb1bd992499716958f23bd Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 5 Apr 2016 14:13:59 -0700 Subject: [PATCH] Published web.config to application root instead of web root #115 --- samples/IISSample/project.json | 21 ++--- samples/IISSample/{wwwroot => }/web.config | 0 .../Program.cs | 7 +- .../PublishIISCommand.cs | 40 +--------- .../WebConfigTransform.cs | 23 +----- .../PublishIISCommandFacts.cs | 80 +++++-------------- .../WebConfigTransformFacts.cs | 22 +---- 7 files changed, 37 insertions(+), 156 deletions(-) rename samples/IISSample/{wwwroot => }/web.config (100%) diff --git a/samples/IISSample/project.json b/samples/IISSample/project.json index 56c97356c7..80f8b836a6 100644 --- a/samples/IISSample/project.json +++ b/samples/IISSample/project.json @@ -9,25 +9,16 @@ "compilationOptions": { "emitEntryPoint": true }, - "commands": { - "web": "IISSample" - }, "frameworks": { "net451": { }, "netstandardapp1.5": { - "imports": "portable-net451+win8" + "dependencies": { + "NETStandard.Library": "1.5.0-*" + }, + "imports": "dnxcore50" } }, - "publishExclude": [ - "node_modules", - "bower_components", - "**.xproj", - "**.user", - "**.vspscc" - ], - "exclude": [ - "wwwroot", - "node_modules", - "bower_components" + "content": [ + "web.config" ] } diff --git a/samples/IISSample/wwwroot/web.config b/samples/IISSample/web.config similarity index 100% rename from samples/IISSample/wwwroot/web.config rename to samples/IISSample/web.config diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs index cd1b7153a2..665846b1ea 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs @@ -14,13 +14,12 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools var app = new CommandLineApplication { Name = "dotnet publish-iis", - FullName = "Asp.Net IIS Publisher", - Description = "IIS Publisher for the Asp.Net web applications", + FullName = "Asp.Net Core IIS Publisher", + Description = "IIS Publisher for the Asp.Net Core web applications", }; app.HelpOption("-h|--help"); var publishFolderOption = app.Option("--publish-folder|-p", "The path to the publish output folder", CommandOptionType.SingleValue); - var webRootOption = app.Option("--webroot|-w", "The name of webroot folder", CommandOptionType.SingleValue); var projectPath = app.Argument("", "The path to the project (project folder or project.json) being published. If empty the current directory is used."); app.OnExecute(() => @@ -35,7 +34,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools Reporter.Output.WriteLine($"Configuring the following project for use with IIS: '{publishFolder}'"); - var exitCode = new PublishIISCommand(publishFolder, projectPath.Value, webRootOption.Value()).Run(); + var exitCode = new PublishIISCommand(publishFolder, projectPath.Value).Run(); Reporter.Output.WriteLine("Configuring project completed successfully"); diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs index 6ead3cf364..a07b870d3b 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs @@ -15,23 +15,19 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools { private readonly string _publishFolder; private readonly string _projectPath; - private readonly string _webRoot; - public PublishIISCommand(string publishFolder, string projectPath, string webRoot) + public PublishIISCommand(string publishFolder, string projectPath) { _publishFolder = publishFolder; _projectPath = projectPath; - _webRoot = webRoot; } public int Run() { var applicationBasePath = GetApplicationBasePath(); - var webRoot = GetWebRoot(applicationBasePath); XDocument webConfigXml = null; - var webRootDirectory = Path.Combine(_publishFolder, webRoot); - var webConfigPath = Path.Combine(webRootDirectory, "web.config"); + var webConfigPath = Path.Combine(_publishFolder, "web.config"); if (File.Exists(webConfigPath)) { Reporter.Output.WriteLine($"Updating web.config at '{webConfigPath}'"); @@ -44,12 +40,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools } else { - if (!Directory.Exists(webRootDirectory)) - { - Reporter.Output.WriteLine($"No webroot directory found. Creating '{webRootDirectory}'"); - Directory.CreateDirectory(webRootDirectory); - } - Reporter.Output.WriteLine($"No web.config found. Creating '{webConfigPath}'"); } @@ -83,32 +73,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools return ProjectReader.GetProject(Path.Combine(applicationBasePath, "project.json")).Name; } - private string GetWebRoot(string applicationBasePath) - { - if (!string.IsNullOrEmpty(_webRoot)) - { - return _webRoot; - } - - var builder = new ConfigurationBuilder() - .SetBasePath(applicationBasePath) - .AddJsonFile("hosting.json", optional: true); - - var webroot = builder.Build()["webroot"]; - - if (!string.IsNullOrEmpty(webroot)) - { - return webroot; - } - - if (Directory.Exists(Path.Combine(applicationBasePath, "wwwroot"))) - { - return "wwwroot"; - } - - return string.Empty; - } - private static bool ConfigureForAzure() { var configureForAzureValue = Environment.GetEnvironmentVariable("DOTNET_CONFIGURE_AZURE"); diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs index 30e0e4c755..2b13c4249c 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs @@ -57,32 +57,13 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools private static void TransformAspNetCore(XElement aspNetCoreElement, string appName, bool configureForAzure) { - var appPath = Path.Combine(configureForAzure ? @"%home%\site" : "..", appName); - var logPath = Path.Combine(configureForAzure ? @"\\?\%home%\LogFiles" : @"..\logs", "stdout.log"); + var appPath = Path.Combine(configureForAzure ? @"%home%\site" : ".", appName); + var logPath = Path.Combine(configureForAzure ? @"\\?\%home%\LogFiles" : @".\logs", "stdout.log"); aspNetCoreElement.SetAttributeValue("processPath", appPath); SetAttributeValueIfEmpty(aspNetCoreElement, "stdoutLogEnabled", "false"); SetAttributeValueIfEmpty(aspNetCoreElement, "stdoutLogFile", logPath); SetAttributeValueIfEmpty(aspNetCoreElement, "startupTimeLimit", "3600"); - - AddApplicationBase(aspNetCoreElement); - } - - private static void AddApplicationBase(XElement aspNetCoreElement) - { - const string contentRootKeyName = "ASPNETCORE_CONTENTROOT"; - - var envVariables = GetOrCreateChild(aspNetCoreElement, "environmentVariables"); - var appBaseElement = envVariables.Elements("environmentVariable").SingleOrDefault(e => - string.Equals((string)e.Attribute("name"), contentRootKeyName, StringComparison.CurrentCultureIgnoreCase)); - - if (appBaseElement == null) - { - appBaseElement = new XElement("environmentVariable", new XAttribute("name", contentRootKeyName)); - envVariables.AddFirst(appBaseElement); - } - - appBaseElement.SetAttributeValue("value", "."); } private static XElement GetOrCreateChild(XElement parent, string childName) diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/PublishIISCommandFacts.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/PublishIISCommandFacts.cs index b22aa68b52..5e88a88aa8 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/PublishIISCommandFacts.cs +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/PublishIISCommandFacts.cs @@ -18,15 +18,14 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests [Fact] public void PublishIIS_uses_default_values_if_options_not_specified() { - var webRoot = "wwwroot"; - var folders = CreateTestDir("{}", webRoot); + var folders = CreateTestDir("{}"); - new PublishIISCommand(folders.PublishOutput, folders.ProjectPath, null).Run(); + new PublishIISCommand(folders.PublishOutput, folders.ProjectPath).Run(); - var processPath = (string)GetPublishedWebConfig(folders.PublishOutput, webRoot) + var processPath = (string)GetPublishedWebConfig(folders.PublishOutput) .Descendants("aspNetCore").Attributes("processPath").Single(); - Assert.Equal($@"..\projectDir.exe", processPath); + Assert.Equal($@".\projectDir.exe", processPath); Directory.Delete(folders.TestRoot, recursive: true); } @@ -36,49 +35,14 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests [InlineData("awesome.App")] public void PublishIIS_reads_application_name_from_project_json_if_exists(string projectName) { - var webRoot = "wwwroot"; - var folders = CreateTestDir($@"{{ ""name"": ""{projectName}"" }}", webRoot); + var folders = CreateTestDir($@"{{ ""name"": ""{projectName}"" }}"); - new PublishIISCommand(folders.PublishOutput, folders.ProjectPath, null).Run(); + new PublishIISCommand(folders.PublishOutput, folders.ProjectPath).Run(); - var processPath = (string)GetPublishedWebConfig(folders.PublishOutput, webRoot) + var processPath = (string)GetPublishedWebConfig(folders.PublishOutput) .Descendants("aspNetCore").Attributes("processPath").Single(); - Assert.Equal($@"..\{projectName}.exe", processPath); - - Directory.Delete(folders.TestRoot, recursive: true); - } - - [Fact] - public void PublishIIS_uses_webroot_from_hosting_json() - { - var webRoot = "mywebroot"; - var folders = CreateTestDir("{}", webRoot); - File.WriteAllText(Path.Combine(folders.ProjectPath, "hosting.json"), $"{{ \"webroot\": \"{webRoot}\"}}"); - - new PublishIISCommand(folders.PublishOutput, folders.ProjectPath, null).Run(); - - var processPath = (string)GetPublishedWebConfig(folders.PublishOutput, webRoot) - .Descendants("aspNetCore").Attributes("processPath").Single(); - - Assert.Equal(@"..\projectDir.exe", processPath); - - Directory.Delete(folders.TestRoot, recursive: true); - } - - [Fact] - public void PublishIIS_webroot_switch_takes_precedence_over_hosting_json() - { - var webRoot = "mywebroot"; - var folders = CreateTestDir("{}", webRoot); - File.WriteAllText(Path.Combine(folders.ProjectPath, "hosting.json"), $"{{ \"webroot\": \"wwwroot\"}}"); - - new PublishIISCommand(folders.PublishOutput, folders.ProjectPath, webRoot).Run(); - - var processPath = (string)GetPublishedWebConfig(folders.PublishOutput, webRoot) - .Descendants("aspNetCore").Attributes("processPath").Single(); - - Assert.Equal(@"..\projectDir.exe", processPath); + Assert.Equal($@".\{projectName}.exe", processPath); Directory.Delete(folders.TestRoot, recursive: true); } @@ -88,15 +52,14 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests [InlineData("project.Dir")] public void PublishIIS_accepts_path_to_project_json_as_project_path(string projectDir) { - var webRoot = "wwwroot"; - var folders = CreateTestDir("{}", webRoot, projectDir); + var folders = CreateTestDir("{}", projectDir); - new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json"), null).Run(); + new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json")).Run(); - var processPath = (string)GetPublishedWebConfig(folders.PublishOutput, webRoot) + var processPath = (string)GetPublishedWebConfig(folders.PublishOutput) .Descendants("aspNetCore").Attributes("processPath").Single(); - Assert.Equal($@"..\{projectDir}.exe", processPath); + Assert.Equal($@".\{projectDir}.exe", processPath); Directory.Delete(folders.TestRoot, recursive: true); } @@ -104,10 +67,9 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests [Fact] public void PublishIIS_modifies_existing_web_config() { - var webRoot = "wwwroot"; - var folders = CreateTestDir("{}", webRoot); + var folders = CreateTestDir("{}"); - File.WriteAllText(Path.Combine(folders.PublishOutput, webRoot, "web.config"), + File.WriteAllText(Path.Combine(folders.PublishOutput, "web.config"), @" @@ -117,35 +79,33 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests "); - new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json"), null).Run(); + new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json")).Run(); - var aspNetCoreElement = GetPublishedWebConfig(folders.PublishOutput, webRoot) + var aspNetCoreElement = GetPublishedWebConfig(folders.PublishOutput) .Descendants("aspNetCore").Single(); - Assert.Equal(@"..\projectDir.exe", (string)aspNetCoreElement.Attribute("processPath")); + Assert.Equal(@".\projectDir.exe", (string)aspNetCoreElement.Attribute("processPath")); Assert.Equal(@"1234", (string)aspNetCoreElement.Attribute("startupTimeLimit")); Directory.Delete(folders.TestRoot, recursive: true); } - private XDocument GetPublishedWebConfig(string publishOut, string webRoot) + private XDocument GetPublishedWebConfig(string publishOut) { - return XDocument.Load(Path.Combine(publishOut, webRoot, "web.config")); + return XDocument.Load(Path.Combine(publishOut, "web.config")); } - private Folders CreateTestDir(string projectJson, string webRoot, string projectDir = "projectDir") + private Folders CreateTestDir(string projectJson, string projectDir = "projectDir") { var testRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(testRoot); var projectPath = Path.Combine(testRoot, projectDir); Directory.CreateDirectory(projectPath); - Directory.CreateDirectory(Path.Combine(projectPath, webRoot)); File.WriteAllText(Path.Combine(projectPath, "project.json"), projectJson); var publishOut = Path.Combine(testRoot, "publishOut"); Directory.CreateDirectory(publishOut); - Directory.CreateDirectory(Path.Combine(publishOut, webRoot)); return new Folders { TestRoot = testRoot, ProjectPath = projectPath, PublishOutput = publishOut }; } diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs index a7b31c6097..0dbdea3839 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs @@ -12,11 +12,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests - - - - - + "); @@ -79,7 +75,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests (string)WebConfigTransform.Transform(WebConfigTemplate, "app.exe", configureForAzure: false) .Descendants("aspNetCore").Single().Attribute("processPath"); - Assert.Equal(@"..\app.exe", newProcessPath); + Assert.Equal(@".\app.exe", newProcessPath); } [Fact] @@ -99,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests new XElement("environmentVariable", new XAttribute("name", "ENVVAR"), new XAttribute("value", "123")); var input = WebConfigTemplate; - input.Descendants("environmentVariable").Single().Add(envVarElement); + input.Descendants("aspNetCore").Single().Add(envVarElement); Assert.True(XNode.DeepEquals(envVarElement, WebConfigTransform.Transform(input, "app.exe", configureForAzure: false) @@ -134,7 +130,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests } Assert.Equal( - @"..\logs\stdout.log", + @".\logs\stdout.log", (string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false) .Descendants().Attributes("stdoutLogFile").Single()); } @@ -177,16 +173,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests aspNetCoreElement)); } - [Fact] - public void WebConfigTransform_overrites_value_for_ASPNET_APPLICATIONBASE() - { - var input = WebConfigTemplate; - input.Descendants("environmentVariable").Single().SetAttributeValue("value", "abc"); - - Assert.True(XNode.DeepEquals(WebConfigTemplate, - WebConfigTransform.Transform(input, "test.exe", configureForAzure: false))); - } - private bool VerifyMissingElementCreated(params string[] elementNames) { var input = WebConfigTemplate;