From b2ce6c794acb1052bd1418aea4c1c64640f9dbaf Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Sun, 18 Dec 2016 18:45:35 -0800 Subject: [PATCH] Only adding rids for full framework TFMs if the project.json does not already have Runtimes in its Runtimes section. --- .../.noautobuild | 0 .../Program.cs | 17 +++++++++ .../project.json | 38 +++++++++++++++++++ .../Rules/MigrateTFMRule.cs | 28 +++++++++----- .../Rules/GivenThatIWantToMigrateTFMs.cs | 32 ++++++++++++++-- 5 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/.noautobuild create mode 100644 TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/Program.cs create mode 100644 TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/project.json diff --git a/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/.noautobuild b/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/.noautobuild new file mode 100644 index 000000000..e69de29bb diff --git a/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/Program.cs b/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/Program.cs new file mode 100644 index 000000000..bb536dcc4 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/Program.cs @@ -0,0 +1,17 @@ +using System; +using System.Xml; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main() + { + Console.WriteLine("Hello World!"); +#if NET20 || NET35 || NET45 || NET461 + // Force XmlDocument to be used + var doc = new XmlDocument(); +#endif + } + } +} diff --git a/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/project.json b/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/project.json new file mode 100644 index 000000000..0fca6f2a3 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithMultipleFrameworksAndRuntimes/project.json @@ -0,0 +1,38 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true + }, + "dependencies": {}, + "frameworks": { + "net20": { + "frameworkAssemblies": { + "System.Xml": {} + } + }, + "net35": { + "frameworkAssemblies": { + "System.Xml": {} + } + }, + "net40": { + "frameworkAssemblies": { + "System.Xml": {} + } + }, + "net461": { + "frameworkAssemblies": { + "System.Xml": {} + } + }, + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NetCore.App": "1.0.1" + } + } + }, + "runtimes": { + "win7-x64": {}, + "win7-x86": {} + } +} diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs index 8bde76352..dbeaf4c02 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.Linq; using System.Text; using Microsoft.Build.Construction; +using Microsoft.DotNet.Internal.ProjectModel; using Microsoft.DotNet.ProjectJsonMigration.Transforms; using NuGet.Frameworks; using System.Collections.Generic; @@ -42,7 +43,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules mergeExisting: true); _transformApplicator.Execute( FrameworkRuntimeIdentifiersTransform.Transform( - migrationRuleInputs.ProjectContexts.Single().TargetFramework), + migrationRuleInputs.ProjectContexts.Single()), propertyGroup, mergeExisting: true); } @@ -53,9 +54,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)), propertyGroup, mergeExisting: true); + + + var runtimes = string.Join(",", migrationRuleInputs.ProjectContexts.Select(p => p.RuntimeIdentifier)); + Console.WriteLine($"Runtimes = {runtimes}"); + _transformApplicator.Execute( FrameworksRuntimeIdentifiersTransform.Transform( - migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)), + migrationRuleInputs.ProjectContexts), propertyGroup, mergeExisting: true); } @@ -122,11 +128,12 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules frameworks => string.Join(";", frameworks.Select(f => f.GetShortFolderName())), frameworks => true); - private AddPropertyTransform> FrameworksRuntimeIdentifiersTransform => - new AddPropertyTransform>( + private AddPropertyTransform> FrameworksRuntimeIdentifiersTransform => + new AddPropertyTransform>( "RuntimeIdentifiers", - frameworks => RuntimeIdentifiers, - frameworks => frameworks.Any(f => !f.IsPackageBased)); + projectContexts => RuntimeIdentifiers, + projectContexts => projectContexts.All(p => !p.ProjectFile.Runtimes.Any()) && + projectContexts.Any(p => !p.TargetFramework.IsPackageBased)); private AddPropertyTransform FrameworkTransform => new AddPropertyTransform( @@ -134,10 +141,11 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules framework => framework.GetShortFolderName(), framework => true); - private AddPropertyTransform FrameworkRuntimeIdentifiersTransform => - new AddPropertyTransform( + private AddPropertyTransform FrameworkRuntimeIdentifiersTransform => + new AddPropertyTransform( "RuntimeIdentifiers", - framework => RuntimeIdentifiers, - framework => !framework.IsPackageBased); + projectContext => RuntimeIdentifiers, + projectContext => !projectContext.ProjectFile.Runtimes.Any() && + !projectContext.TargetFramework.IsPackageBased); } } diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateTFMs.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateTFMs.cs index 8efd27482..ba6410771 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateTFMs.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateTFMs.cs @@ -16,7 +16,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests public class GivenThatIWantToMigrateTFMs : TestBase { [Fact(Skip="Emitting this until x-targetting full support is in")] - public void Migrating_netcoreapp_project_Does_not_populate_TargetFrameworkIdentifier_and_TargetFrameworkVersion() + public void MigratingNetcoreappProjectDoesNotPopulateTargetFrameworkIdentifierAndTargetFrameworkVersion() { var testDirectory = Temp.CreateDirectory().Path; var testPJ = new ProjectJsonBuilder(TestAssetsManager) @@ -44,7 +44,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } [Fact] - public void Migrating_MultiTFM_project_Populates_TargetFrameworks_with_short_tfms() + public void MigratingMultiTFMProjectPopulatesTargetFrameworksWithShortTfms() { var testDirectory = Temp.CreateDirectory().Path; var testPJ = new ProjectJsonBuilder(TestAssetsManager) @@ -69,7 +69,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } [Fact] - public void Migrating_desktop_TFMs_adds_RuntimeIdentifiers() + public void MigratingDesktopTFMsAddsAllRuntimeIdentifiersIfTheProjectDoesNothaveAnyAlready() { var testDirectory = Temp.CreateDirectory().Path; var testPJ = new ProjectJsonBuilder(TestAssetsManager) @@ -94,7 +94,31 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } [Fact] - public void Migrating_Single_TFM_project_Populates_TargetFramework() + public void MigrateTFMRuleDoesNotAddRuntimesWhenMigratingDesktopTFMsWithRuntimesAlready() + { + var testDirectory = Temp.CreateDirectory().Path; + var testPJ = new ProjectJsonBuilder(TestAssetsManager) + .FromTestAssetBase("TestAppWithMultipleFrameworksAndRuntimes") + .SaveToDisk(testDirectory); + + var projectContexts = ProjectContext.CreateContextForEachFramework(testDirectory); + var mockProj = ProjectRootElement.Create(); + + var migrationSettings = + MigrationSettings.CreateMigrationSettingsTestHook(testDirectory, testDirectory, mockProj); + var migrationInputs = new MigrationRuleInputs( + projectContexts, + mockProj, + mockProj.AddItemGroup(), + mockProj.AddPropertyGroup()); + + new MigrateTFMRule().Apply(migrationSettings, migrationInputs); + + mockProj.Properties.Count(p => p.Name == "RuntimeIdentifiers").Should().Be(0); + } + + [Fact] + public void MigratingSingleTFMProjectPopulatesTargetFramework() { var testDirectory = Temp.CreateDirectory().Path; var testPJ = new ProjectJsonBuilder(TestAssetsManager)