From e710958b793b61addf4a0d7e7eec5381d3dbac1d Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Wed, 14 Dec 2016 13:59:01 -0800 Subject: [PATCH] Adding a Exclude @(EmbeddedResource) during migration so that we don't get duplicate resources when building migrated apps. --- .../TestAppWithEmbeddedResources/Program.cs | 24 ++++ .../Resources/Strings.resx | 123 ++++++++++++++++++ .../TestAppWithEmbeddedResources/project.json | 20 +++ .../Rules/MigrateBuildOptionsRule.cs | 8 +- .../GivenThatIWantToMigrateBuildOptions.cs | 14 +- .../GivenThatIWantToMigrateTestApps.cs | 1 + 6 files changed, 185 insertions(+), 5 deletions(-) create mode 100755 TestAssets/TestProjects/TestAppWithEmbeddedResources/Program.cs create mode 100644 TestAssets/TestProjects/TestAppWithEmbeddedResources/Resources/Strings.resx create mode 100755 TestAssets/TestProjects/TestAppWithEmbeddedResources/project.json diff --git a/TestAssets/TestProjects/TestAppWithEmbeddedResources/Program.cs b/TestAssets/TestProjects/TestAppWithEmbeddedResources/Program.cs new file mode 100755 index 000000000..0e2502ce3 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithEmbeddedResources/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Linq; +using System.Reflection; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { + var thisAssembly = typeof(Program).GetTypeInfo().Assembly; + var resources = from resourceName in thisAssembly.GetManifestResourceNames() + select resourceName; + + if (resources.Count() > 1) + { + throw new Exception($"{resources.Count()} found in the assembly. Was expecting only 1."); + } + + var resourceNames = string.Join(",", resources); + Console.WriteLine($"{resources.Count()} Resources Found: {resourceNames}"); + } + } +} diff --git a/TestAssets/TestProjects/TestAppWithEmbeddedResources/Resources/Strings.resx b/TestAssets/TestProjects/TestAppWithEmbeddedResources/Resources/Strings.resx new file mode 100644 index 000000000..1f24a372f --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithEmbeddedResources/Resources/Strings.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Hello World! + + \ No newline at end of file diff --git a/TestAssets/TestProjects/TestAppWithEmbeddedResources/project.json b/TestAssets/TestProjects/TestAppWithEmbeddedResources/project.json new file mode 100755 index 000000000..3c89bd6fb --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithEmbeddedResources/project.json @@ -0,0 +1,20 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "debugType": "portable", + "emitEntryPoint": true, + "embed": [ "Resources/*.resx" ] + }, + "dependencies": {}, + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.0.0" + } + }, + "imports": "dnxcore50" + } + } +} diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs index 78a9592b1..1f85b8863 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs @@ -150,7 +150,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules }; private readonly string[] DefaultEmptyExcludeOption = new string[0]; - + private readonly ProjectPropertyGroupElement _configurationPropertyGroup; private readonly ProjectItemGroupElement _configurationItemGroup; private readonly CommonCompilerOptions _configurationBuildOptions; @@ -419,13 +419,17 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules private IncludeContext GetEmbedIncludeContext(CommonCompilerOptions compilerOptions, string projectDirectory) { // Defaults from src/Microsoft.DotNet.ProjectModel/ProjectReader.cs #L602 - return compilerOptions.EmbedInclude ?? + var embedIncludeContext = compilerOptions.EmbedInclude ?? new IncludeContext( projectDirectory, "embed", new JObject(), ProjectFilesCollection.DefaultResourcesBuiltInPatterns, DefaultEmptyExcludeOption); + + embedIncludeContext.BuiltInsExclude.Add("@(EmbeddedResource)"); + + return embedIncludeContext; } private IncludeContext GetCopyToOutputIncludeContext(CommonCompilerOptions compilerOptions, string projectDirectory) diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs index 1858c58d4..bee7bef76 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs @@ -60,7 +60,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests mockProj.Items.First(i => i.ItemType == "Compile").Include.Should().Be(@"**\*.cs"); mockProj.Items.First(i => i.ItemType == "Compile").Exclude.Should().BeEmpty(); mockProj.Items.First(i => i.ItemType == "EmbeddedResource").Include.Should().Be(@"compiler\resources\**\*;**\*.resx"); - mockProj.Items.First(i => i.ItemType == "EmbeddedResource").Exclude.Should().BeEmpty(); + mockProj.Items.First(i => i.ItemType == "EmbeddedResource").Exclude.Should().Be("@(EmbeddedResource)"); } [Fact] @@ -555,8 +555,16 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests private static IEnumerable GetDefaultExcludePatterns(string group) { - return group == "copyToOutput" ? ProjectFilesCollection.DefaultPublishExcludePatterns - : ProjectFilesCollection.DefaultBuiltInExcludePatterns; + var defaultExcludePatterns = new List(group == "copyToOutput" ? + ProjectFilesCollection.DefaultPublishExcludePatterns : + ProjectFilesCollection.DefaultBuiltInExcludePatterns); + + if (group == "embed") + { + defaultExcludePatterns.Add("@(EmbeddedResource)"); + } + + return defaultExcludePatterns; } private static IEnumerable GetDefaultIncludePatterns(string group) diff --git a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs index 1cca412e9..deb1ef0ed 100644 --- a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs +++ b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs @@ -22,6 +22,7 @@ namespace Microsoft.DotNet.Migration.Tests [InlineData("TestAppWithRuntimeOptions")] [InlineData("TestAppWithContents")] [InlineData("AppWithAssemblyInfo")] + [InlineData("TestAppWithEmbeddedResources")] public void ItMigratesApps(string projectName) { var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, identifier: projectName)