Skip to content
This repository has been archived by the owner on Nov 1, 2018. It is now read-only.

Commit

Permalink
Enabling publishing portable apps
Browse files Browse the repository at this point in the history
Addresses #109
  • Loading branch information
moozzyk committed Apr 15, 2016
1 parent ab50213 commit 1410a5a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ public static int Main(string[] args)
app.HelpOption("-h|--help");

var publishFolderOption = app.Option("--publish-folder|-p", "The path to the publish output folder", CommandOptionType.SingleValue);
var frameworkOption = app.Option("-f|--framework <FRAMEWORK>", "Target framework of application being published", CommandOptionType.SingleValue);
var projectPath = app.Argument("<PROJECT>", "The path to the project (project folder or project.json) being published. If empty the current directory is used.");

app.OnExecute(() =>
{
var publishFolder = publishFolderOption.Value();
var framework = frameworkOption.Value();
if (publishFolder == null)
if (publishFolder == null || framework == null)
{
app.ShowHelp();
return 2;
}
Reporter.Output.WriteLine($"Configuring the following project for use with IIS: '{publishFolder}'");
var exitCode = new PublishIISCommand(publishFolder, projectPath.Value).Run();
var exitCode = new PublishIISCommand(publishFolder, framework, projectPath.Value).Run();
Reporter.Output.WriteLine("Configuring project completed successfully");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.DotNet.ProjectModel;
using Microsoft.Extensions.Cli.Utils;
using NuGet.Frameworks;

namespace Microsoft.AspNetCore.Server.IISIntegration.Tools
{
public class PublishIISCommand
{
private readonly string _publishFolder;
private readonly string _projectPath;
private readonly string _framework;

public PublishIISCommand(string publishFolder, string projectPath)
public PublishIISCommand(string publishFolder, string framework, string projectPath)
{
_publishFolder = publishFolder;
_projectPath = projectPath;
_framework = framework;
}

public int Run()
Expand All @@ -42,8 +46,10 @@ public int Run()
Reporter.Output.WriteLine($"No web.config found. Creating '{webConfigPath}'");
}

var applicationName = GetApplicationName(applicationBasePath) + ".exe";
var transformedConfig = WebConfigTransform.Transform(webConfigXml, applicationName, ConfigureForAzure());
var projectContext = GetProjectContext(applicationBasePath, _framework);
var isPortable = !projectContext.TargetFramework.IsDesktop() && projectContext.IsPortable;
var applicationName = projectContext.ProjectFile.Name + (isPortable ? ".dll" : ".exe");
var transformedConfig = WebConfigTransform.Transform(webConfigXml, applicationName, ConfigureForAzure(), isPortable);

using (var f = new FileStream(webConfigPath, FileMode.Create))
{
Expand All @@ -67,9 +73,14 @@ private string GetApplicationBasePath()
return Directory.GetCurrentDirectory();
}

private string GetApplicationName(string applicationBasePath)
private static ProjectContext GetProjectContext(string applicationBasePath, string framework)
{
return ProjectReader.GetProject(Path.Combine(applicationBasePath, "project.json")).Name;
var project = ProjectReader.GetProject(Path.Combine(applicationBasePath, "project.json"));

return new ProjectContextBuilder()
.WithProject(project)
.WithTargetFramework(framework)
.Build();
}

private static bool ConfigureForAzure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools
{
public static class WebConfigTransform
{
public static XDocument Transform(XDocument webConfig, string appName, bool configureForAzure)
public static XDocument Transform(XDocument webConfig, string appName, bool configureForAzure, bool isPortable)
{
const string HandlersElementName = "handlers";
const string aspNetCoreElementName = "aspNetCore";
Expand All @@ -22,7 +22,7 @@ public static XDocument Transform(XDocument webConfig, string appName, bool conf
var webServerSection = GetOrCreateChild(webConfig.Root, "system.webServer");

TransformHandlers(GetOrCreateChild(webServerSection, HandlersElementName));
TransformAspNetCore(GetOrCreateChild(webServerSection, aspNetCoreElementName), appName, configureForAzure);
TransformAspNetCore(GetOrCreateChild(webServerSection, aspNetCoreElementName), appName, configureForAzure, isPortable);

// make sure that the aspNetCore element is after handlers element
var aspNetCoreElement = webServerSection.Element(HandlersElementName)
Expand Down Expand Up @@ -55,14 +55,32 @@ private static void TransformHandlers(XElement handlersElement)
SetAttributeValueIfEmpty(aspNetCoreElement, "resourceType", "Unspecified");
}

private static void TransformAspNetCore(XElement aspNetCoreElement, string appName, bool configureForAzure)
private static void TransformAspNetCore(XElement aspNetCoreElement, string appName, bool configureForAzure, bool isPortable)
{
// Forward slashes currently work neither in AspNetCoreModule nor in dotnet so they need to be
// replaced with backwards slashes when the application is published on a non-Windows machine
var appPath = Path.Combine(configureForAzure ? @"%home%\site" : ".", appName).Replace("/", "\\");
var logPath = Path.Combine(configureForAzure ? @"\\?\%home%\LogFiles" : @".\logs", "stdout").Replace("/", "\\");

aspNetCoreElement.SetAttributeValue("processPath", appPath);
if (!isPortable)
{
aspNetCoreElement.SetAttributeValue("processPath", appPath);
}
else
{
aspNetCoreElement.SetAttributeValue("processPath", "dotnet");

// In Xml the order of attributes does not matter but it is nice to have
// the `arguments` attribute next to the `processPath` attribute
aspNetCoreElement.Attribute("arguments")?.Remove();
var attributes = aspNetCoreElement.Attributes().ToList();
var processPathIndex = attributes.FindIndex(a => a.Name.LocalName == "processPath");
attributes.Insert(processPathIndex + 1, new XAttribute("arguments", appPath));

aspNetCoreElement.Attributes().Remove();
aspNetCoreElement.Add(attributes);
}

SetAttributeValueIfEmpty(aspNetCoreElement, "stdoutLogEnabled", "false");
SetAttributeValueIfEmpty(aspNetCoreElement, "stdoutLogFile", logPath);
SetAttributeValueIfEmpty(aspNetCoreElement, "startupTimeLimit", "3600");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,48 @@ private class Folders
public string ProjectPath;
}

[Fact]
public void PublishIIS_uses_default_values_if_options_not_specified()
[Theory]
[InlineData("netcoreapp1.0")]
[InlineData("netstandard1.5")]
public void PublishIIS_uses_default_values_if_options_not_specified(string targetFramework)
{
var folders = CreateTestDir("{}");
var folders = CreateTestDir($@"{{ ""frameworks"": {{ ""{targetFramework}"": {{ }} }} }}");

new PublishIISCommand(folders.PublishOutput, folders.ProjectPath).Run();
new PublishIISCommand(folders.PublishOutput, targetFramework, folders.ProjectPath).Run();

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);
}

[Fact]
public void PublishIIS_can_publish_for_portable_app()
{
var folders = CreateTestDir(
@"
{
""frameworks"": {
""netcoreapp1.0"": {
""dependencies"": {
""Microsoft.NETCore.App"": {
""version"": ""1.0.0-*"",
""type"": ""platform""
}
}
}
}
}");

new PublishIISCommand(folders.PublishOutput, "netcoreapp1.0", folders.ProjectPath).Run();

var aspNetCoreElement = GetPublishedWebConfig(folders.PublishOutput)
.Descendants("aspNetCore").Single();

Assert.Equal(@"dotnet", (string)aspNetCoreElement.Attribute("processPath"));
Assert.Equal(@".\projectDir.dll", (string)aspNetCoreElement.Attribute("arguments"));

Directory.Delete(folders.TestRoot, recursive: true);
}
Expand All @@ -35,9 +66,9 @@ public void PublishIIS_uses_default_values_if_options_not_specified()
[InlineData("awesome.App")]
public void PublishIIS_reads_application_name_from_project_json_if_exists(string projectName)
{
var folders = CreateTestDir($@"{{ ""name"": ""{projectName}"" }}");
var folders = CreateTestDir($@"{{ ""name"": ""{projectName}"", ""frameworks"": {{ ""netcoreapp1.0"": {{}} }} }}");

new PublishIISCommand(folders.PublishOutput, folders.ProjectPath).Run();
new PublishIISCommand(folders.PublishOutput, "netcoreapp1.0", folders.ProjectPath).Run();

var processPath = (string)GetPublishedWebConfig(folders.PublishOutput)
.Descendants("aspNetCore").Attributes("processPath").Single();
Expand All @@ -52,9 +83,10 @@ public void PublishIIS_reads_application_name_from_project_json_if_exists(string
[InlineData("project.Dir")]
public void PublishIIS_accepts_path_to_project_json_as_project_path(string projectDir)
{
var folders = CreateTestDir("{}", projectDir);
var folders = CreateTestDir(@"{ ""frameworks"": { ""netcoreapp1.0"": { } } }", projectDir);

new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json")).Run();
new PublishIISCommand(folders.PublishOutput, "netcoreapp1.0",
Path.Combine(folders.ProjectPath, "project.json")).Run();

var processPath = (string)GetPublishedWebConfig(folders.PublishOutput)
.Descendants("aspNetCore").Attributes("processPath").Single();
Expand All @@ -67,7 +99,7 @@ public void PublishIIS_accepts_path_to_project_json_as_project_path(string proje
[Fact]
public void PublishIIS_modifies_existing_web_config()
{
var folders = CreateTestDir("{}");
var folders = CreateTestDir(@"{ ""frameworks"": { ""netcoreapp1.0"": { } } }");

File.WriteAllText(Path.Combine(folders.PublishOutput, "web.config"),
@"<configuration>
Expand All @@ -79,7 +111,8 @@ public void PublishIIS_modifies_existing_web_config()
</system.webServer>
</configuration>");

new PublishIISCommand(folders.PublishOutput, Path.Combine(folders.ProjectPath, "project.json")).Run();
new PublishIISCommand(folders.PublishOutput, "netcoreapp1.0",
Path.Combine(folders.ProjectPath, "project.json")).Run();

var aspNetCoreElement = GetPublishedWebConfig(folders.PublishOutput)
.Descendants("aspNetCore").Single();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class WebConfigTransformFacts
public void WebConfigTransform_creates_new_config_if_one_does_not_exist()
{
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(null, "test.exe", configureForAzure: false)));
WebConfigTransform.Transform(null, "test.exe", configureForAzure: false, isPortable: false)));
}

[Fact]
public void WebConfigTransform_creates_new_config_if_one_has_unexpected_format()
{
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(XDocument.Parse("<unexpected />"), "test.exe", configureForAzure: false)));
WebConfigTransform.Transform(XDocument.Parse("<unexpected />"), "test.exe", configureForAzure: false, isPortable: false)));
}

[Theory]
Expand All @@ -47,7 +47,7 @@ public void WebConfigTransform_adds_missing_elements(string[] elementNames)
}

Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)));
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false)));
}

[Theory]
Expand All @@ -64,15 +64,15 @@ public void WebConfigTransform_wont_override_custom_values(string elementName, s
var input = WebConfigTemplate;
input.Descendants(elementName).Single().SetAttributeValue(attributeName, attributeValue);

var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false);
var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false);
Assert.Equal(attributeValue, (string)output.Descendants(elementName).Single().Attribute(attributeName));
}

[Fact]
public void WebConfigTransform_overwrites_processPath()
{
var newProcessPath =
(string)WebConfigTransform.Transform(WebConfigTemplate, "app.exe", configureForAzure: false)
(string)WebConfigTransform.Transform(WebConfigTemplate, "app.exe", configureForAzure: false, isPortable: false)
.Descendants("aspNetCore").Single().Attribute("processPath");

Assert.Equal(@".\app.exe", newProcessPath);
Expand All @@ -85,7 +85,7 @@ public void WebConfigTransform_fixes_aspnetcore_casing()
input.Descendants("add").Single().SetAttributeValue("name", "aspnetcore");

Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)));
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false)));
}

[Fact]
Expand All @@ -98,7 +98,7 @@ public void WebConfigTransform_does_not_remove_children_of_aspNetCore_element()
input.Descendants("aspNetCore").Single().Add(envVarElement);

Assert.True(XNode.DeepEquals(envVarElement,
WebConfigTransform.Transform(input, "app.exe", configureForAzure: false)
WebConfigTransform.Transform(input, "app.exe", configureForAzure: false, isPortable: false)
.Descendants("environmentVariable").SingleOrDefault(e => (string)e.Attribute("name") == "ENVVAR")));
}

Expand All @@ -110,7 +110,7 @@ public void WebConfigTransform_adds_stdoutLogEnabled_if_attribute_is_missing()

Assert.Equal(
"false",
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false)
.Descendants().Attributes("stdoutLogEnabled").Single());
}

Expand All @@ -131,7 +131,7 @@ public void WebConfigTransform_adds_stdoutLogFile_if_attribute_is_missing(string

Assert.Equal(
@".\logs\stdout",
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false)
.Descendants().Attributes("stdoutLogFile").Single());
}

Expand All @@ -153,7 +153,7 @@ public void WebConfigTransform_does_not_change_existing_stdoutLogEnabled(string

Assert.Equal(
"mylog.txt",
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false)
.Descendants().Attributes("stdoutLogFile").Single());
}

Expand All @@ -163,7 +163,7 @@ public void WebConfigTransform_correctly_configures_for_Azure()
var input = WebConfigTemplate;
input.Descendants("aspNetCore").Attributes().Remove();

var aspNetCoreElement = WebConfigTransform.Transform(input, "test.exe", configureForAzure: true)
var aspNetCoreElement = WebConfigTransform.Transform(input, "test.exe", configureForAzure: true, isPortable: false)
.Descendants("aspNetCore").Single();
aspNetCoreElement.Elements().Remove();

Expand All @@ -173,6 +173,35 @@ public void WebConfigTransform_correctly_configures_for_Azure()
aspNetCoreElement));
}

[Fact]
public void WebConfigTransform_configures_portable_apps_correctly()
{
var aspNetCoreElement =
WebConfigTransform.Transform(WebConfigTemplate, "test.exe", configureForAzure: false, isPortable: true)
.Descendants("aspNetCore").Single();

Assert.True(XNode.DeepEquals(
XDocument.Parse(@"<aspNetCore processPath=""dotnet"" arguments="".\test.exe"" stdoutLogEnabled=""false""
stdoutLogFile="".\logs\stdout"" startupTimeLimit=""3600""/>").Root,
aspNetCoreElement));
}

[Fact]
public void WebConfigTransform_overwrites_existing_arguments_attribute_for_portable_apps()
{
var input = WebConfigTemplate;
input.Descendants("aspNetCore").Single().SetAttributeValue("arguments", "42");

var aspNetCoreElement =
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: true)
.Descendants("aspNetCore").Single();

Assert.True(XNode.DeepEquals(
XDocument.Parse(@"<aspNetCore processPath=""dotnet"" arguments="".\test.exe"" stdoutLogEnabled=""false""
stdoutLogFile="".\logs\stdout"" startupTimeLimit=""3600""/>").Root,
aspNetCoreElement));
}

private bool VerifyMissingElementCreated(params string[] elementNames)
{
var input = WebConfigTemplate;
Expand All @@ -182,7 +211,7 @@ private bool VerifyMissingElementCreated(params string[] elementNames)
}

return XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false));
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: false));
}
}
}

0 comments on commit 1410a5a

Please sign in to comment.