Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions src/Tasks.UnitTests/ResourceHandling/MSBuildResXReader_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,39 @@ public void AssemblyElementWithNoAliasInfersSimpleName()
resource.StringRepresentation.ShouldBe("Blue");
}

[Fact]
public void InlineStringIsNotLinkedFileResource()
{
var resources = MSBuildResXReader.GetResourcesFromString(
ResXHelper.SurroundWithBoilerplate(
@"<data name=""StringResource"" xml:space=""preserve"">
<value>StringValue</value>
</data>"), null, false);

resources.ShouldHaveSingleItem();
resources[0].ShouldBeOfType<StringResource>();
resources[0].ShouldBeAssignableTo<ILinkedFileResource>()
.LinkedFilePath.ShouldBeNull();
}

[Fact]
public void FileRefStringIsLinkedFileResource()
{
File.Exists(Path.Combine("ResourceHandling", "TextFile1.txt")).ShouldBeTrue("Test deployment is missing None files");

var resources = MSBuildResXReader.GetResourcesFromString(
ResXHelper.SurroundWithBoilerplate(
$@" <assembly alias=""System.Windows.Forms"" name=""System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" />
<data name=""TextFile1"" type=""System.Resources.ResXFileRef, System.Windows.Forms"">
<value>ResourceHandling\TextFile1.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>"), null, false);

resources.ShouldHaveSingleItem();
resources[0].ShouldBeOfType<StringResource>();
resources[0].ShouldBeAssignableTo<ILinkedFileResource>()
.LinkedFilePath.ShouldNotBeNull();
}

// TODO: invalid resx xml

// TODO: valid xml, but invalid resx-specific data
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/ResGenDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ private static string[] GetLinkedFiles(string filename, string baseLinkedFileDir
{
foreach (IResource resource in MSBuildResXReader.GetResourcesFromFile(filename, pathsRelativeToBasePath: baseLinkedFileDirectory == null, log, logWarningForBinaryFormatter))
{
if (resource is FileStreamResource linkedResource)
if (resource is ILinkedFileResource linked && linked.LinkedFilePath is not null)
{
retVal.Add(linkedResource.FileName);
retVal.Add(linked.LinkedFilePath);
Comment thread
OvesN marked this conversation as resolved.
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tasks/ResourceHandling/FileStreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

namespace Microsoft.Build.Tasks.ResourceHandling
{
internal class FileStreamResource : IResource
internal class FileStreamResource : ILinkedFileResource
Comment thread
OvesN marked this conversation as resolved.
{
public string Name { get; }

public string TypeAssemblyQualifiedName { get; }

public string OriginatingFile { get; }

public string FileName { get; }
public string LinkedFilePath { get; }

public string TypeFullName => NameUtilities.FullNameFromAssemblyQualifiedName(TypeAssemblyQualifiedName);

Expand All @@ -31,7 +31,7 @@ public FileStreamResource(string name, string assemblyQualifiedTypeName, string
{
Name = name;
TypeAssemblyQualifiedName = assemblyQualifiedTypeName;
FileName = fileName;
LinkedFilePath = fileName;
OriginatingFile = originatingFile;
}

Expand All @@ -40,7 +40,7 @@ public void AddTo(IResourceWriter writer)
if (writer is PreserializedResourceWriter preserializedResourceWriter)
{
#pragma warning disable CA2000 // Dispose objects before losing scope the stream is expected to be disposed by the PreserializedResourceWriter.ResourceDataRecord
FileStream fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
FileStream fileStream = new FileStream(LinkedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
#pragma warning restore CA2000 // Dispose objects before losing scope

preserializedResourceWriter.AddActivatorResource(Name, fileStream, TypeAssemblyQualifiedName, closeAfterWrite: true);
Expand Down
17 changes: 17 additions & 0 deletions src/Tasks/ResourceHandling/ILinkedFileResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Build.Tasks.ResourceHandling
{
/// <summary>
/// An <see cref="IResource"/> that originated from a ResXFileRef entry
/// and carries the path to the linked file.
/// </summary>
internal interface ILinkedFileResource : IResource
{
/// <summary>
/// The absolute path of the file this resource was read from.
Comment thread
OvesN marked this conversation as resolved.
Outdated
/// </summary>
string? LinkedFilePath { get; }
Comment thread
OvesN marked this conversation as resolved.
Outdated
}
}
6 changes: 4 additions & 2 deletions src/Tasks/ResourceHandling/LiveObjectResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace Microsoft.Build.Tasks.ResourceHandling
/// <summary>
/// Name value resource pair to go in resources list
/// </summary>
internal class LiveObjectResource : IResource
internal class LiveObjectResource : ILinkedFileResource
{
public LiveObjectResource(string name, object value)
public LiveObjectResource(string name, object value, string linkedFilePath = null)
Comment thread
OvesN marked this conversation as resolved.
Outdated
{
Name = name;
Value = value;
LinkedFilePath = linkedFilePath;
}

public string Name { get; }
public object Value { get; }
public string LinkedFilePath { get; }

public string TypeAssemblyQualifiedName => Value.GetType().AssemblyQualifiedName;

Expand Down
6 changes: 3 additions & 3 deletions src/Tasks/ResourceHandling/MSBuildResXReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private static void AddLinkedResource(string resxFilename, bool pathsRelativeToB
: Encoding.Default;
using (StreamReader sr = new StreamReader(fileName, textFileEncoding))
{
resources.Add(new StringResource(name, sr.ReadToEnd(), resxFilename));
resources.Add(new StringResource(name, sr.ReadToEnd(), resxFilename, fileName));

return;
}
Expand All @@ -269,7 +269,7 @@ private static void AddLinkedResource(string resxFilename, bool pathsRelativeToB
{
byte[] byteArray = FileSystems.Default.ReadFileAllBytes(fileName);

resources.Add(new LiveObjectResource(name, byteArray));
resources.Add(new LiveObjectResource(name, byteArray, fileName));
return;
}
else if (IsMemoryStream(fileRefType))
Expand All @@ -278,7 +278,7 @@ private static void AddLinkedResource(string resxFilename, bool pathsRelativeToB
// https://github.com/dotnet/winforms/blob/689cd9c69e632997bc85bf421af221d79b12ddd4/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L293-L297
byte[] byteArray = FileSystems.Default.ReadFileAllBytes(fileName);

resources.Add(new LiveObjectResource(name, new MemoryStream(byteArray)));
resources.Add(new LiveObjectResource(name, new MemoryStream(byteArray), fileName));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/ResourceHandling/StringResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class StringResource : LiveObjectResource

public new string TypeFullName => typeof(string).FullName;

public StringResource(string name, string value, string filename) :
base(name, value)
public StringResource(string name, string value, string filename, string linkedFilePath = null) :
Comment thread
OvesN marked this conversation as resolved.
Outdated
base(name, value, linkedFilePath)
{
OriginatingFile = filename;
}
Expand Down
Loading