Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Implement resource bundling. #9472

Merged
merged 3 commits into from
Aug 25, 2020
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
4 changes: 3 additions & 1 deletion dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<!-- Inject our custom logic into *DependsOn variables -->
<PropertyGroup>
<BuildDependsOn>
_CollectBundleResources;
_PackLibraryResources;
_UnpackLibraryResources;
$(BuildDependsOn);
_CreateAppBundle;
Codesign;
Expand All @@ -98,7 +101,6 @@
<!-- We re-use ComputeFilesToPublish & CopyFilesToPublishDirectory to copy files to the .app -->
<!-- ComputeFilesToPublish will run ILLink -->
<CreateAppBundleDependsOn>
_CollectBundleResources;
_DetectAppManifest;
_CopyResourcesToBundle;
_CompileAppManifest;
Expand Down
9 changes: 7 additions & 2 deletions tests/BundledResources/ResourcesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public void Bundled ()
// resources are removed by the linker or an extra step (e.g. "link sdk" or "don't link") but that
// extra step is done only on device (to keep the simulator builds as fast as possible)
var resources = typeof(ResourcesTest).Assembly.GetManifestResourceNames ();
if (Runtime.Arch == Arch.DEVICE) {
#if __MACOS__
var hasResources = false;
#else
var hasResources = Runtime.Arch != Arch.DEVICE;
#endif
if (!hasResources) {
Assert.That (resources.Length, Is.EqualTo (0), "No resources");
} else {
Assert.That (resources.Length, Is.GreaterThanOrEqualTo (2), "Resources");
Expand All @@ -41,4 +46,4 @@ public void Bundled ()
}
}
}
}
}
23 changes: 23 additions & 0 deletions tests/BundledResources/dotnet/iOS/BundledResources.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.iOS.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnitLite" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
<Link>basn3p08.png</Link>
</BundleResource>
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
<Link>xamvideotest.mp4</Link>
</BundleResource>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\ResourcesTest.cs" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions tests/BundledResources/dotnet/macOS/BundledResources.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.macOS.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnitLite" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
<Link>basn3p08.png</Link>
</BundleResource>
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
<Link>xamvideotest.mp4</Link>
</BundleResource>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\ResourcesTest.cs" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions tests/BundledResources/dotnet/tvOS/BundledResources.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.tvOS.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnitLite" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
<Link>basn3p08.png</Link>
</BundleResource>
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
<Link>xamvideotest.mp4</Link>
</BundleResource>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\ResourcesTest.cs" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions tests/BundledResources/dotnet/watchOS/BundledResources.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.watchOS.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnitLite" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<BundleResource Include="..\..\..\monotouch-test\basn3p08.png">
<Link>basn3p08.png</Link>
</BundleResource>
<BundleResource Include="..\..\..\monotouch-test\xamvideotest.mp4">
<Link>xamvideotest.mp4</Link>
</BundleResource>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\ResourcesTest.cs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions tests/dotnet/UnitTests/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,38 @@ public void BuildBindingsTest2 (string platform)
Assert.That (ad.MainModule.Resources [0].Name, Is.EqualTo ("libtest2.a"), "libtest2.a");
}

[TestCase ("iOS", "monotouch")]
[TestCase ("tvOS", "monotouch")]
[TestCase ("watchOS", "monotouch")]
[TestCase ("macOS", "xammac")]
public void BuildBundledResources (string platform, string prefix)
{
var assemblyName = "BundledResources";
var dotnet_bindings_dir = Path.Combine (Configuration.SourceRoot, "tests", assemblyName, "dotnet");
var project_dir = Path.Combine (dotnet_bindings_dir, platform);
var project_path = Path.Combine (project_dir, $"{assemblyName}.csproj");

Clean (project_path);
CopyDotNetSupportingFiles (dotnet_bindings_dir);
var result = DotNet.AssertBuild (project_path, verbosity);
var lines = result.StandardOutput.ToString ().Split ('\n');
// Find the resulting binding assembly from the build log
var assemblies = FilterToAssembly (lines, assemblyName);
Assert.That (assemblies, Is.Not.Empty, "Assemblies");
// Make sure there's no other assembly confusing our logic
Assert.That (assemblies.Distinct ().Count (), Is.EqualTo (1), "Unique assemblies");
var asm = assemblies.First ();
Assert.That (asm, Does.Exist, "Assembly existence");

// Verify that there's one resource in the binding assembly, and its name
var ad = AssemblyDefinition.ReadAssembly (asm, new ReaderParameters { ReadingMode = ReadingMode.Deferred });
Assert.That (ad.MainModule.Resources.Count, Is.EqualTo (2), "2 resources");
// Sort the resources before we assert, since we don't care about the order, and sorted order makes the asserts simpler.
var resources = ad.MainModule.Resources.OrderBy (v => v.Name).ToArray ();
Assert.That (resources [0].Name, Is.EqualTo ($"__{prefix}_content_basn3p08.png"), $"__{prefix}_content_basn3p08.png");
Assert.That (resources [1].Name, Is.EqualTo ($"__{prefix}_content_xamvideotest.mp4"), $"__{prefix}_content_xamvideotest.mp4");
}

[TestCase ("iOS")]
[TestCase ("tvOS")]
// [TestCase ("watchOS")] // No watchOS Touch.Client project for .NET yet
Expand Down