Skip to content

Commit ab47edc

Browse files
authored
[dotnet] Implement resource bundling. (dotnet#9472)
* Port the BundledResources test to .NET. * Fix a minor issue in the BundledResources test to make sure it works on macOS. * Add a unit test to make sure resources are bundled as expected. * Modify the .NET build logic to bundle/unbundle resources.
1 parent 18ca278 commit ab47edc

File tree

7 files changed

+134
-3
lines changed

7 files changed

+134
-3
lines changed

dotnet/targets/Xamarin.Shared.Sdk.targets

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
<!-- Inject our custom logic into *DependsOn variables -->
9191
<PropertyGroup>
9292
<BuildDependsOn>
93+
_CollectBundleResources;
94+
_PackLibraryResources;
95+
_UnpackLibraryResources;
9396
$(BuildDependsOn);
9497
_CreateAppBundle;
9598
Codesign;
@@ -98,7 +101,6 @@
98101
<!-- We re-use ComputeFilesToPublish & CopyFilesToPublishDirectory to copy files to the .app -->
99102
<!-- ComputeFilesToPublish will run ILLink -->
100103
<CreateAppBundleDependsOn>
101-
_CollectBundleResources;
102104
_DetectAppManifest;
103105
_CopyResourcesToBundle;
104106
_CompileAppManifest;

tests/BundledResources/ResourcesTest.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public void Bundled ()
3232
// resources are removed by the linker or an extra step (e.g. "link sdk" or "don't link") but that
3333
// extra step is done only on device (to keep the simulator builds as fast as possible)
3434
var resources = typeof(ResourcesTest).Assembly.GetManifestResourceNames ();
35-
if (Runtime.Arch == Arch.DEVICE) {
35+
#if __MACOS__
36+
var hasResources = false;
37+
#else
38+
var hasResources = Runtime.Arch != Arch.DEVICE;
39+
#endif
40+
if (!hasResources) {
3641
Assert.That (resources.Length, Is.EqualTo (0), "No resources");
3742
} else {
3843
Assert.That (resources.Length, Is.GreaterThanOrEqualTo (2), "Resources");
@@ -41,4 +46,4 @@ public void Bundled ()
4146
}
4247
}
4348
}
44-
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.iOS.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NUnitLite" Version="3.12.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
14+
<Link>basn3p08.png</Link>
15+
</BundleResource>
16+
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
17+
<Link>xamvideotest.mp4</Link>
18+
</BundleResource>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<Compile Include="..\..\ResourcesTest.cs" />
22+
</ItemGroup>
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.macOS.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NUnitLite" Version="3.12.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
14+
<Link>basn3p08.png</Link>
15+
</BundleResource>
16+
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
17+
<Link>xamvideotest.mp4</Link>
18+
</BundleResource>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<Compile Include="..\..\ResourcesTest.cs" />
22+
</ItemGroup>
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.tvOS.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NUnitLite" Version="3.12.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
14+
<Link>basn3p08.png</Link>
15+
</BundleResource>
16+
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
17+
<Link>xamvideotest.mp4</Link>
18+
</BundleResource>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<Compile Include="..\..\ResourcesTest.cs" />
22+
</ItemGroup>
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.watchOS.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NUnitLite" Version="3.12.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
14+
<Link>basn3p08.png</Link>
15+
</BundleResource>
16+
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
17+
<Link>xamvideotest.mp4</Link>
18+
</BundleResource>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<Compile Include="..\..\ResourcesTest.cs" />
22+
</ItemGroup>
23+
</Project>

tests/dotnet/UnitTests/ProjectTest.cs

+32
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,38 @@ public void BuildBindingsTest2 (string platform)
211211
Assert.That (ad.MainModule.Resources [0].Name, Is.EqualTo ("libtest2.a"), "libtest2.a");
212212
}
213213

214+
[TestCase ("iOS", "monotouch")]
215+
[TestCase ("tvOS", "monotouch")]
216+
[TestCase ("watchOS", "monotouch")]
217+
[TestCase ("macOS", "xammac")]
218+
public void BuildBundledResources (string platform, string prefix)
219+
{
220+
var assemblyName = "BundledResources";
221+
var dotnet_bindings_dir = Path.Combine (Configuration.SourceRoot, "tests", assemblyName, "dotnet");
222+
var project_dir = Path.Combine (dotnet_bindings_dir, platform);
223+
var project_path = Path.Combine (project_dir, $"{assemblyName}.csproj");
224+
225+
Clean (project_path);
226+
CopyDotNetSupportingFiles (dotnet_bindings_dir);
227+
var result = DotNet.AssertBuild (project_path, verbosity);
228+
var lines = result.StandardOutput.ToString ().Split ('\n');
229+
// Find the resulting binding assembly from the build log
230+
var assemblies = FilterToAssembly (lines, assemblyName);
231+
Assert.That (assemblies, Is.Not.Empty, "Assemblies");
232+
// Make sure there's no other assembly confusing our logic
233+
Assert.That (assemblies.Distinct ().Count (), Is.EqualTo (1), "Unique assemblies");
234+
var asm = assemblies.First ();
235+
Assert.That (asm, Does.Exist, "Assembly existence");
236+
237+
// Verify that there's one resource in the binding assembly, and its name
238+
var ad = AssemblyDefinition.ReadAssembly (asm, new ReaderParameters { ReadingMode = ReadingMode.Deferred });
239+
Assert.That (ad.MainModule.Resources.Count, Is.EqualTo (2), "2 resources");
240+
// Sort the resources before we assert, since we don't care about the order, and sorted order makes the asserts simpler.
241+
var resources = ad.MainModule.Resources.OrderBy (v => v.Name).ToArray ();
242+
Assert.That (resources [0].Name, Is.EqualTo ($"__{prefix}_content_basn3p08.png"), $"__{prefix}_content_basn3p08.png");
243+
Assert.That (resources [1].Name, Is.EqualTo ($"__{prefix}_content_xamvideotest.mp4"), $"__{prefix}_content_xamvideotest.mp4");
244+
}
245+
214246
[TestCase ("iOS")]
215247
[TestCase ("tvOS")]
216248
// [TestCase ("watchOS")] // No watchOS Touch.Client project for .NET yet

0 commit comments

Comments
 (0)