Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,47 @@ public void DependentUponConvention_FindsMatch()
}
}

/// <summary>
/// Opt into DependentUpon convention but don't expect it to be used for this file.
/// </summary>
[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");
}
}


/// <summary>
/// Opt into DependentUpon convention and load the expected file properly when the file is in a subfolder.
/// </summary>
Expand Down
16 changes: 14 additions & 2 deletions src/Tasks/CreateManifestResourceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,20 @@ CreateFileStream createFileStream
string fileName = resourceFile.ItemSpec;
string dependentUpon = resourceFile.GetMetadata(ItemMetadataNames.dependentUpon);

// If opted into convention and no DependentUpon metadata, reference "<filename>.cs" if it exists.
if (UseDependentUponConvention && string.IsNullOrEmpty(dependentUpon))
string fileType = resourceFile.GetMetadata("Type");

// 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");

// If not, 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 "<filename>.cs" if it exists.
if (isResxFile && UseDependentUponConvention && string.IsNullOrEmpty(dependentUpon))
{
string conventionDependentUpon = Path.ChangeExtension(Path.GetFileName(fileName), SourceFileExtension);

Expand Down