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
30 changes: 28 additions & 2 deletions src/mono/wasm/Wasm.Build.Tests/SatelliteLoadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public async Task LoadSatelliteAssembly(bool loadAllSatelliteResources)
// So there messages are should be present only when we are lazily loading satellites.
expectedOutput.Add(m => Assert.Equal("default: hello", m));
expectedOutput.Add(m => Assert.Equal("es-ES without satellite: hello", m));
expectedOutput.Add(m => Assert.Equal("fr-FR without satellite: hello", m));
}

expectedOutput.Add(m => Assert.Equal("default: hello", m));
expectedOutput.Add(m => Assert.Equal("es-ES with satellite: hola", m));
expectedOutput.Add(m => Assert.Equal("fr-FR with satellite: bonjour", m));

Assert.Collection(
result.TestOutput,
Expand All @@ -68,7 +70,7 @@ public async Task LoadSatelliteAssemblyFromReference()
var appCsprojPath = Path.Combine(_projectDir, "WasmBasicTestApp.csproj");
var appCsproj = XDocument.Load(appCsprojPath);

var projectReference = appCsproj.Descendants("ProjectReference").Where(pr => pr.Attribute("Include")?.Value?.Contains("ResourceLibrary") ?? false).Single();
var projectReference = appCsproj.Descendants("ProjectReference").Single(pr => pr.Attribute("Include")?.Value?.Contains("ResourceLibrary") ?? false);
var itemGroup = projectReference.Parent!;
projectReference.Remove();

Expand All @@ -94,8 +96,32 @@ public async Task LoadSatelliteAssemblyFromReference()
result.TestOutput,
m => Assert.Equal("default: hello", m),
m => Assert.Equal("es-ES without satellite: hello", m),
m => Assert.Equal("fr-FR without satellite: hello", m),
m => Assert.Equal("default: hello", m),
m => Assert.Equal("es-ES with satellite: hola", m)
m => Assert.Equal("es-ES with satellite: hola", m),
m => Assert.Equal("fr-FR with satellite: bonjour", m)
);
}

[Fact, TestCategory("no-fingerprinting")]
public void SatelliteAssembliesFromPackageReference()
{
Configuration config = Configuration.Release;
ProjectInfo info = CopyTestAsset(config, false, TestAsset.WasmBasicTestApp, "SatelliteLoadingTestsFromPackageReference");
BuildProject(info, config, new BuildOptions(ExtraMSBuildArgs: "-p:TestSatelliteAssembliesFromPackage=true"));

string binFrameworkDir = GetBinFrameworkDir(config, forPublish: false);

// Microsoft.CodeAnalysis.CSharp has satellite assemblies for multiple locales
// Verify that at least some of them are present in the AppBundle
string[] expectedLocales = ["cs", "de", "es", "fr", "it", "ja", "ko", "pl", "pt-BR", "ru", "tr", "zh-Hans", "zh-Hant"];
foreach (string locale in expectedLocales)
{
string satelliteDir = Path.Combine(binFrameworkDir, locale);
Assert.True(Directory.Exists(satelliteDir), $"Expected satellite directory '{locale}' to exist in {binFrameworkDir}");

string[] satelliteFiles = Directory.GetFiles(satelliteDir, "Microsoft.CodeAnalysis.CSharp.resources*");
Assert.True(satelliteFiles.Length > 0, $"Expected Microsoft.CodeAnalysis.CSharp.resources.dll in {satelliteDir}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static async Task Run(bool loadSatelliteAssemblies)
if (loadSatelliteAssemblies)
{
ResourceLibrary.ResourceAccessor.Read(TestOutput.WriteLine, false);
await LoadSatelliteAssemblies(new[] { "es-ES" });
await LoadSatelliteAssemblies(new[] { "es-ES", "fr-FR" });
}

ResourceLibrary.ResourceAccessor.Read(TestOutput.WriteLine, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public static void Read(Action<string> testOuput, bool hasSatellites)
var rm = new ResourceManager("ResourceLibrary.words", typeof(ResourceAccessor).Assembly);
testOuput($"default: {rm.GetString("hello", CultureInfo.CurrentCulture)}");
testOuput($"es-ES {(hasSatellites ? "with" : "without")} satellite: {rm.GetString("hello", new CultureInfo("es-ES"))}");
testOuput($"fr-FR {(hasSatellites ? "with" : "without")} satellite: {rm.GetString("hello", new CultureInfo("fr-FR"))}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<OutputType>Library</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup Condition="'$(TestSatelliteAssembliesFromPackage)' == 'true'">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute ref="xml:space" />
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="bye" xml:space="preserve">
<value>au revoir</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="hello" xml:space="preserve">
<value>bonjour</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override bool Execute()
{
var filesToRemove = new List<ITaskItem>();
var assetCandidates = new List<ITaskItem>();
var uniqueFileNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var uniqueRelativePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

try
{
Expand Down Expand Up @@ -117,46 +117,49 @@ public override bool Execute()

assetCandidate.SetMetadata("RelatedAsset", Path.GetFullPath(Path.Combine(OutputPath, "wwwroot", "_framework", Path.GetFileName(resolvedFrom))));

assetCandidates.Add(assetCandidate);
continue;
candidate = assetCandidate;
}

string relativePath = AssetsComputingHelper.GetCandidateRelativePath(candidate, FingerprintAssets, FingerprintDotNetJs);
candidate.SetMetadata("RelativePath", relativePath);

// Workaround for https://github.com/dotnet/aspnetcore/issues/37574.
// For items added as "Reference" in project references, the OriginalItemSpec is incorrect.
// Ignore it, and use the FullPath instead.
if (candidate.GetMetadata("ReferenceSourceTarget") == "ProjectReference")
else
{
candidate.SetMetadata("OriginalItemSpec", candidate.ItemSpec);
var culture = candidate.GetMetadata("Culture");
if (!string.IsNullOrEmpty(culture))
{
candidate.SetMetadata("AssetKind", "Build");
candidate.SetMetadata("AssetRole", "Related");
candidate.SetMetadata("AssetTraitName", "Culture");
candidate.SetMetadata("AssetTraitValue", culture);

var fileName = candidate.GetMetadata("FileName");
var suffixIndex = fileName.Length - ".resources".Length;
var relatedAssetPath = Path.GetFullPath(Path.Combine(
OutputPath,
"wwwroot",
"_framework",
fileName.Substring(0, suffixIndex) + ProjectAssembly[0].GetMetadata("Extension")));

candidate.SetMetadata("RelatedAsset", relatedAssetPath);
candidate.SetMetadata("RelativePath", $"_framework/{culture}/{fileName}{candidate.GetMetadata("Extension")}");

Log.LogMessage(MessageImportance.Low, "Found satellite assembly '{0}' asset for inferred candidate '{1}' with culture '{2}'", candidate.ItemSpec, relatedAssetPath, culture);
}
else
{
// Workaround for https://github.com/dotnet/aspnetcore/issues/37574.
// For items added as "Reference" in project references, the OriginalItemSpec is incorrect.
// Ignore it, and use the FullPath instead.
if (candidate.GetMetadata("ReferenceSourceTarget") == "ProjectReference")
{
candidate.SetMetadata("OriginalItemSpec", candidate.ItemSpec);
}

candidate.SetMetadata("RelativePath", AssetsComputingHelper.GetCandidateRelativePath(candidate, FingerprintAssets, FingerprintDotNetJs));
}
}

var culture = candidate.GetMetadata("Culture");
if (!string.IsNullOrEmpty(culture))
var relativePath = candidate.GetMetadata("RelativePath");
if (!uniqueRelativePaths.Add(relativePath))
{
candidate.SetMetadata("AssetKind", "Build");
candidate.SetMetadata("AssetRole", "Related");
candidate.SetMetadata("AssetTraitName", "Culture");
candidate.SetMetadata("AssetTraitValue", culture);
var fileName = candidate.GetMetadata("FileName");
var suffixIndex = fileName.Length - ".resources".Length;
var relatedAssetPath = Path.GetFullPath(Path.Combine(
OutputPath,
"wwwroot",
"_framework",
fileName.Substring(0, suffixIndex) + ProjectAssembly[0].GetMetadata("Extension")));

candidate.SetMetadata("RelatedAsset", relatedAssetPath);

Log.LogMessage(MessageImportance.Low, "Found satellite assembly '{0}' asset for inferred candidate '{1}' with culture '{2}'", candidate.ItemSpec, relatedAssetPath, culture);
}

// Check for unique file name before adding candidate
var candidateFileName = Path.GetFileName(candidate.ItemSpec);
if (!uniqueFileNames.Add(candidateFileName))
{
Log.LogMessage(MessageImportance.Low, "Skipping duplicate file name '{0}' for candidate '{1}'", candidateFileName, candidate.ItemSpec);
Log.LogMessage(MessageImportance.Low, "Skipping duplicate relative path '{0}' for candidate '{1}'", relativePath, candidate.ItemSpec);
continue;
}

Expand Down Expand Up @@ -191,13 +194,18 @@ public override bool Execute()
"_framework",
ProjectAssembly[0].GetMetadata("FileName") + ProjectAssembly[0].GetMetadata("Extension")));

var normalizedPath = assetCandidate.GetMetadata("TargetPath").Replace('\\', '/');
var relativePath = $"_framework/{assetCandidate.GetMetadata("TargetPath").Replace('\\', '/')}";
if (!uniqueRelativePaths.Add(relativePath))
{
Log.LogMessage(MessageImportance.Low, "Skipping duplicate relative path '{0}' for candidate '{1}'", relativePath, projectSatelliteAssembly.ItemSpec);
continue;
}

assetCandidate.SetMetadata("AssetKind", "Build");
assetCandidate.SetMetadata("AssetRole", "Related");
assetCandidate.SetMetadata("AssetTraitName", "Culture");
assetCandidate.SetMetadata("AssetTraitValue", candidateCulture);
assetCandidate.SetMetadata("RelativePath", Path.Combine("_framework", normalizedPath));
assetCandidate.SetMetadata("RelativePath", relativePath);
assetCandidate.SetMetadata("RelatedAsset", projectAssemblyAssetPath);

assetCandidates.Add(assetCandidate);
Expand Down