From 37f321cde33c0f1372e2f62e8614af56ebf25962 Mon Sep 17 00:00:00 2001 From: Ben Villalobos Date: Wed, 25 Sep 2019 16:19:02 -0700 Subject: [PATCH 1/4] Apply convention only to .resx files --- src/Tasks/CreateManifestResourceName.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Tasks/CreateManifestResourceName.cs b/src/Tasks/CreateManifestResourceName.cs index cc3aba62321..ac641341d33 100644 --- a/src/Tasks/CreateManifestResourceName.cs +++ b/src/Tasks/CreateManifestResourceName.cs @@ -146,8 +146,13 @@ CreateFileStream createFileStream string fileName = resourceFile.ItemSpec; string dependentUpon = resourceFile.GetMetadata(ItemMetadataNames.dependentUpon); - // If opted into convention and no DependentUpon metadata, reference ".cs" if it exists. - if (UseDependentUponConvention && string.IsNullOrEmpty(dependentUpon)) + string fileType = resourceFile.GetMetadata("Type"); + + // If it has a file type metadata is "Resx" or has the file extension ".resx", we know it's a resx file. + bool isResxFile = (!string.IsNullOrEmpty(fileType) && fileType == "Resx") || Path.GetExtension(fileName) == ".resx"; + + // If opted into convention and no DependentUpon metadata and is a resx file, reference ".cs" if it exists. + if (isResxFile && UseDependentUponConvention && string.IsNullOrEmpty(dependentUpon)) { string conventionDependentUpon = Path.ChangeExtension(Path.GetFileName(fileName), SourceFileExtension); From 7559b545778bcc0307f42f370bae9de67fa166b1 Mon Sep 17 00:00:00 2001 From: Ben Villalobos Date: Thu, 26 Sep 2019 13:34:14 -0700 Subject: [PATCH 2/4] Fixed edge case --- src/Tasks/CreateManifestResourceName.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Tasks/CreateManifestResourceName.cs b/src/Tasks/CreateManifestResourceName.cs index ac641341d33..3de3f340dc6 100644 --- a/src/Tasks/CreateManifestResourceName.cs +++ b/src/Tasks/CreateManifestResourceName.cs @@ -148,8 +148,14 @@ CreateFileStream createFileStream string fileType = resourceFile.GetMetadata("Type"); - // If it has a file type metadata is "Resx" or has the file extension ".resx", we know it's a resx file. - bool isResxFile = (!string.IsNullOrEmpty(fileType) && fileType == "Resx") || Path.GetExtension(fileName) == ".resx"; + // If it has a file type metadata is "Resx" + bool isResxFile = (!string.IsNullOrEmpty(fileType) && fileType == "Resx"); + + //fall back onto the extension + if (string.IsNullOrEmpty(fileType)) + { + isResxFile = Path.GetExtension(fileName) == ".resx"; + } // If opted into convention and no DependentUpon metadata and is a resx file, reference ".cs" if it exists. if (isResxFile && UseDependentUponConvention && string.IsNullOrEmpty(dependentUpon)) From 2301506ded7b6b789d93a8c7df1b9c30b22692e6 Mon Sep 17 00:00:00 2001 From: Ben Villalobos Date: Fri, 27 Sep 2019 13:54:06 -0700 Subject: [PATCH 3/4] Updated comments. --- src/Tasks/CreateManifestResourceName.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Tasks/CreateManifestResourceName.cs b/src/Tasks/CreateManifestResourceName.cs index 3de3f340dc6..933b59ab40f 100644 --- a/src/Tasks/CreateManifestResourceName.cs +++ b/src/Tasks/CreateManifestResourceName.cs @@ -148,10 +148,11 @@ CreateFileStream createFileStream string fileType = resourceFile.GetMetadata("Type"); - // If it has a file type metadata is "Resx" + // If it has "type" metadata and the value is "Resx" + // This value can be specified by the user, if not it will have been automatically assigned by the SplitResourcesByCulture target. bool isResxFile = (!string.IsNullOrEmpty(fileType) && fileType == "Resx"); - //fall back onto the extension + // If not, fall back onto the extension. if (string.IsNullOrEmpty(fileType)) { isResxFile = Path.GetExtension(fileName) == ".resx"; From 34d16ac0025092e67a19fb172a356bc20039a822 Mon Sep 17 00:00:00 2001 From: Rainer Sigwald Date: Tue, 1 Oct 2019 11:08:39 -0500 Subject: [PATCH 4/4] Test for non-resx DependentUpon convention --- .../CreateCSharpManifestResourceName_Tests.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs b/src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs index ba2fded7038..47f51fe6a00 100644 --- a/src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs +++ b/src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs @@ -416,6 +416,47 @@ public void DependentUponConvention_FindsMatch() } } + /// + /// Opt into DependentUpon convention but don't expect it to be used for this file. + /// + [Theory] + [InlineData(true)] + [InlineData(false)] + public void DependentUponConvention_DoesNotApplyToNonResx(bool explicitlySpecifyType) + { + using (var env = TestEnvironment.Create()) + { + var csFile = env.CreateFile("SR1.cs", "namespace MyStuff.Namespace { class Class { } }"); + const string ResourceFileName = "SR1.txt"; + var resourceFile = env.CreateFile(ResourceFileName, ""); + + // Default resource naming is based on the item include, so use a relative + // path here instead of a full path. + env.SetCurrentDirectory(Path.GetDirectoryName(resourceFile.Path)); + ITaskItem i = new TaskItem(ResourceFileName); + i.SetMetadata("BuildAction", "EmbeddedResource"); + if (explicitlySpecifyType) + { + i.SetMetadata("Type", "Non-Resx"); + } + // Don't set DependentUpon so it goes by convention + + CreateCSharpManifestResourceName t = new CreateCSharpManifestResourceName + { + BuildEngine = new MockEngine(_testOutput), + UseDependentUponConvention = true, + ResourceFiles = new ITaskItem[] { i } + }; + + t.Execute().ShouldBeTrue("Expected the task to succeed."); + + t.ManifestResourceNames.ShouldHaveSingleItem(); + + t.ManifestResourceNames[0].ItemSpec.ShouldBe(ResourceFileName, "Expecting to find the namespace & class name from SR1.cs"); + } + } + + /// /// Opt into DependentUpon convention and load the expected file properly when the file is in a subfolder. ///