Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[build] post-build fix of the assembly #2880

Merged
merged 1 commit into from
Jun 1, 2018
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
64 changes: 64 additions & 0 deletions XFCorePostProcessor.Tasks/FixXFCoreAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Microsoft.Build.Utilities;
using Mono.Cecil;
using Mono.Cecil.Rocks;

using Mono.Cecil.Cil;
using Microsoft.Build.Framework;
using System.IO;

namespace XFCorePostProcessor.Tasks
{
public class FixXFCoreAssembly : Task
{
[Required]
public string Assembly { get; set; }
public string ReferencePath { get; set; }

public override bool Execute()
{
Log.LogMessage("Generating backcompat code for #2835");

var resolver = new AssemblyResolver();

if (!string.IsNullOrEmpty(ReferencePath)) {
var paths = ReferencePath.Replace("//", "/").Split(';');
foreach (var p in paths) {
var searchpath = Path.GetDirectoryName(p);
resolver.AddSearchDirectory(searchpath);
}
}

using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(Assembly, new ReaderParameters { AssemblyResolver = resolver, ReadWrite = true, ReadSymbols = true })) {
var resourceLoader = assemblyDefinition.MainModule.GetType("Xamarin.Forms.Internals.ResourceLoader");
var module = assemblyDefinition.MainModule;
var methodDef = new MethodDefinition("get_ResourceProvider",
MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
module.ImportReference(module.ImportReference(typeof(Func<,>)).MakeGenericInstanceType(module.ImportReference(typeof(string)),
module.ImportReference(typeof(string)))));

var body = new MethodBody(methodDef);
var il = body.GetILProcessor();
il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Ret);
methodDef.Body = body;
resourceLoader.Methods.Add(methodDef);

assemblyDefinition.Write(new WriterParameters {
WriteSymbols = true,
});
}
return true;
}
}

class AssemblyResolver : DefaultAssemblyResolver
{
public void AddAssembly(string p)
{
RegisterAssembly(AssemblyDefinition.ReadAssembly(p, new ReaderParameters {
AssemblyResolver = this
}));
}
}
}
15 changes: 15 additions & 0 deletions XFCorePostProcessor.Tasks/XFCorePostProcessor.Tasks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="15.7.179" />
<PackageReference Include="Microsoft.Build.Framework" Version="15.7.179" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.7.179" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.7.179" />
<PackageReference Include="Mono.Cecil" Version="0.10.0-beta7" />
</ItemGroup>
</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions Xamarin.Forms.Core/Xamarin.Forms.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
</ItemGroup>
<Import Project="..\Xamarin.Flex\Xamarin.Flex.projitems" Label="Shared" Condition="Exists('..\Xamarin.Flex\Xamarin.Flex.projitems')" />
<UsingTask TaskName="XFCorePostProcessor.Tasks.FixXFCoreAssembly" AssemblyFile="..\XFCorePostProcessor.Tasks\bin\Debug\netstandard2.0\XFCorePostProcessor.Tasks.dll" />
<Target AfterTargets="AfterCompile" Name="ShameHat">
<FixXFCoreAssembly Assembly="$(IntermediateOutputPath)$(TargetFileName)" ReferencePath="@(ReferencePath)" />
</Target>
</Project>
22 changes: 22 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/XFCorePostProcessorCodeInjected.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Reflection;
using System.Linq;

using NUnit.Framework;

namespace Xamarin.Forms.Xaml.UnitTests
{
[TestFixture]
public class XFCorePostProcessorCodeInjected
{
[Test]
public void InjectedCodeIsPresent()
{
var resLoader = typeof(Xamarin.Forms.Internals.ResourceLoader);
Assert.True(resLoader.GetMethods().Any(mi => mi.Name == "get_ResourceProvider" && mi.ReturnType == typeof(Func<string, string>)));
Assert.True(resLoader.GetMethods().Any(mi => mi.Name == "get_ResourceProvider" && mi.ReturnType == typeof(Func<AssemblyName, string, string>)));
Assert.True(resLoader.GetProperty("ResourceProvider") != null);
Assert.True(resLoader.GetProperty("ResourceProvider").GetMethod.ReturnType == typeof(Func<AssemblyName, string, string>));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
<Compile Include="Issues\Gh2632.xaml.cs">
<DependentUpon>Gh2632.xaml</DependentUpon>
</Compile>
<Compile Include="XFCorePostProcessorCodeInjected.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" Condition="'$(BuildingInsideVisualStudio)' == 'true' AND Exists('..\.nuspec\Xamarin.Forms.Build.Tasks.dll')" />
Expand Down
2 changes: 2 additions & 0 deletions Xamarin.Forms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Forms.Maps.Design",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Forms.Xaml.Design", "Xamarin.Forms.Xaml.Design\Xamarin.Forms.Xaml.Design.csproj", "{65BC4888-CC59-428A-9B75-540CF1C09480}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFCorePostProcessor.Tasks", "XFCorePostProcessor.Tasks\XFCorePostProcessor.Tasks.csproj", "{5BBF4A3F-4AD1-47FD-B250-05EA793F939D}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Xamarin.Forms.Controls.Issues\Xamarin.Forms.Controls.Issues.Shared\Xamarin.Forms.Controls.Issues.Shared.projitems*{0a39a74b-6f7a-4d41-84f2-b0ccdce899df}*SharedItemsImports = 4
Expand Down