Add support for migrating Microsoft.AspNetCore.Mvc.ViewCompilation

This commit is contained in:
Pranav K 2017-01-11 16:51:45 -08:00 committed by Livar Cunha
parent c9216f93ef
commit 80c435e6a1
4 changed files with 79 additions and 0 deletions

View file

@ -23,6 +23,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs) public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{ {
var csproj = migrationRuleInputs.OutputMSBuildProject; var csproj = migrationRuleInputs.OutputMSBuildProject;
var commonPropertyGroup = migrationRuleInputs.CommonPropertyGroup;
var projectContext = migrationRuleInputs.DefaultProjectContext; var projectContext = migrationRuleInputs.DefaultProjectContext;
var scripts = projectContext.ProjectFile.Scripts; var scripts = projectContext.ProjectFile.Scripts;
@ -30,6 +31,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{ {
MigrateScriptSet( MigrateScriptSet(
csproj, csproj,
commonPropertyGroup,
scriptSet.Value, scriptSet.Value,
scriptSet.Key, scriptSet.Key,
migrationRuleInputs.IsMultiTFM); migrationRuleInputs.IsMultiTFM);
@ -38,6 +40,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
public ProjectTargetElement MigrateScriptSet( public ProjectTargetElement MigrateScriptSet(
ProjectRootElement csproj, ProjectRootElement csproj,
ProjectPropertyGroupElement commonPropertyGroup,
IEnumerable<string> scriptCommands, IEnumerable<string> scriptCommands,
string scriptSetName, string scriptSetName,
bool isMultiTFM) bool isMultiTFM)
@ -49,6 +52,11 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{ {
continue; continue;
} }
else if (IsRazorPrecompilationCommand(scriptCommand))
{
EnableRazorCompilationOnPublish(commonPropertyGroup);
continue;
}
AddExec(target, FormatScriptCommand(scriptCommand)); AddExec(target, FormatScriptCommand(scriptCommand));
} }
@ -94,6 +102,16 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
return command.Contains("dotnet publish-iis"); return command.Contains("dotnet publish-iis");
} }
private static bool IsRazorPrecompilationCommand(string command)
{
return command.Contains("dotnet razor-precompile");
}
private static void EnableRazorCompilationOnPublish(ProjectPropertyGroupElement commonPropertyGroup)
{
commonPropertyGroup.AddProperty("MvcRazorCompileOnPublish", "true");
}
private bool IsPathRootedForAnyOS(string path) private bool IsPathRootedForAnyOS(string path)
{ {
return path.StartsWith("/") || path.Substring(1).StartsWith(":\\"); return path.StartsWith("/") || path.Substring(1).StartsWith(":\\");

View file

@ -51,6 +51,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration
}, },
null null
}, },
{
new PackageDependencyInfo
{
Name = "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools",
Version = "[1.0.0-*,)"
},
null
},
{ {
new PackageDependencyInfo new PackageDependencyInfo
{ {
@ -159,6 +167,17 @@ namespace Microsoft.DotNet.ProjectJsonMigration
Version = ConstantPackageVersions.AspNetToolsVersion Version = ConstantPackageVersions.AspNetToolsVersion
} }
}, },
{
new PackageDependencyInfo
{
Name = "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design",
Version = "[1.0.0-*,)"
},
new PackageDependencyInfo {
Name = "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation",
Version = ConstantPackageVersions.AspNet110ToolsVersion
}
},
{ {
new PackageDependencyInfo new PackageDependencyInfo
{ {

View file

@ -63,10 +63,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new string[] { "fakecommand" }; var commands = new string[] { "fakecommand" };
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
scriptName, scriptName,
IsMultiTFM); IsMultiTFM);
@ -83,10 +86,12 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] { "fakecommand" }; var commands = new[] { "fakecommand" };
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
scriptName, scriptName,
IsMultiTFM); IsMultiTFM);
@ -103,12 +108,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] { "fakecommand1", "fakecommand2", "mockcommand3" }; var commands = new[] { "fakecommand1", "fakecommand2", "mockcommand3" };
var commandsInTask = commands.ToDictionary(c => c, c => false); var commandsInTask = commands.ToDictionary(c => c, c => false);
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
scriptName, scriptName,
IsMultiTFM); IsMultiTFM);
@ -140,11 +147,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] { "%compile:FullTargetFramework%", "%compile:Configuration%"}; var commands = new[] { "%compile:FullTargetFramework%", "%compile:Configuration%"};
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
scriptName, scriptName,
IsMultiTFM); IsMultiTFM);
@ -167,6 +176,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] var commands = new[]
{ {
@ -175,12 +185,36 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
"postpublish", "postpublish",
IsMultiTFM); IsMultiTFM);
target.Tasks.Should().BeEmpty(); target.Tasks.Should().BeEmpty();
} }
[Fact]
public void MigratingScriptsReplacesRazorPrecompileWithProperty()
{
var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new string[] { "dotnet razor-precompile --configuration %publish:Configuration% --framework %publish:TargetFramework% --output-path %publish:OutputPath% %publish:ProjectPath%" };
var target = scriptMigrationRule.MigrateScriptSet(
mockProj,
commonPropertyGroup,
commands,
"postpublish",
IsMultiTFM);
target.Tasks.Should().BeEmpty();
commonPropertyGroup.Properties.Count().Should().Be(1);
var propertyElement = commonPropertyGroup.Properties.First();
propertyElement.Name.Should().Be("MvcRazorCompileOnPublish");
propertyElement.Value.Should().Be("true");
}
[Fact] [Fact]
public void FormattingScriptCommandsReplacesUnknownVariablesWithMSBuildPropertyForEnvironmentVariableSupport() public void FormattingScriptCommandsReplacesUnknownVariablesWithMSBuildPropertyForEnvironmentVariableSupport()
{ {
@ -193,11 +227,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] { "compile:FullTargetFramework", "compile:Configuration"}; var commands = new[] { "compile:FullTargetFramework", "compile:Configuration"};
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
"prepublish", "prepublish",
IsMultiTFM); IsMultiTFM);
@ -209,11 +245,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new[] { "compile:FullTargetFramework", "compile:Configuration"}; var commands = new[] { "compile:FullTargetFramework", "compile:Configuration"};
var target = scriptMigrationRule.MigrateScriptSet( var target = scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
"prepublish", "prepublish",
false); false);
@ -225,11 +263,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{ {
var scriptMigrationRule = new MigrateScriptsRule(); var scriptMigrationRule = new MigrateScriptsRule();
ProjectRootElement mockProj = ProjectRootElement.Create(); ProjectRootElement mockProj = ProjectRootElement.Create();
ProjectPropertyGroupElement commonPropertyGroup = mockProj.AddPropertyGroup();
var commands = new string[] { "fakecommand" }; var commands = new string[] { "fakecommand" };
Action action = () => scriptMigrationRule.MigrateScriptSet( Action action = () => scriptMigrationRule.MigrateScriptSet(
mockProj, mockProj,
commonPropertyGroup,
commands, commands,
"invalidScriptSet", "invalidScriptSet",
IsMultiTFM); IsMultiTFM);

View file

@ -18,6 +18,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
[InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.0.1", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)] [InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.0.1", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)]
[InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.0.0-preview3-final", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)] [InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.0.0-preview3-final", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)]
[InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.1.0-preview4-final", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNet110ToolsVersion)] [InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "1.1.0-preview4-final", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNet110ToolsVersion)]
[InlineData("Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design", "1.1.0-preview4-final", "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation", ConstantPackageVersions.AspNet110ToolsVersion)]
public void ItMigratesProjectDependenciesToANewNameAndVersion( public void ItMigratesProjectDependenciesToANewNameAndVersion(
string sourceToolName, string sourceToolName,
string sourceVersion, string sourceVersion,
@ -77,6 +78,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
[Theory] [Theory]
[InlineData("Microsoft.AspNetCore.Razor.Tools")] [InlineData("Microsoft.AspNetCore.Razor.Tools")]
[InlineData("Microsoft.AspNetCore.Server.IISIntegration.Tools")] [InlineData("Microsoft.AspNetCore.Server.IISIntegration.Tools")]
[InlineData("Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools")]
public void ItDoesNotMigrateAspProjectTool(string toolName) public void ItDoesNotMigrateAspProjectTool(string toolName)
{ {
var mockProj = RunPackageDependenciesRuleOnPj(@" var mockProj = RunPackageDependenciesRuleOnPj(@"