-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathCheckForInvalidDesignerConfig.cs
More file actions
61 lines (53 loc) · 1.61 KB
/
CheckForInvalidDesignerConfig.cs
File metadata and controls
61 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#nullable enable
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.IO;
namespace Xamarin.Android.Tasks
{
/// <summary>
/// Emits XA1034 is an assembly has a reference to _Microsoft.Android.Resource.Designer.dll
/// * NOTE: only called when $(AndroidUseDesignerAssembly) is false
/// </summary>
public class CheckForInvalidDesignerConfig : AndroidTask
{
public override string TaskPrefix => "CIRF";
public ITaskItem[]? Assemblies { get; set; }
public override bool RunTask ()
{
if (Assemblies == null)
return true;
foreach (var assembly in Assemblies) {
if (HasResourceDesignerAssemblyReference (assembly)) {
Log.LogCodedError ("XA1034", Properties.Resources.XA1034, assembly);
}
}
return !Log.HasLoggedErrors;
}
static bool HasResourceDesignerAssemblyReference (ITaskItem assembly)
{
if (!File.Exists (assembly.ItemSpec)) {
return false;
}
using var pe = new PEReader (File.OpenRead (assembly.ItemSpec));
if (!pe.HasMetadata) {
return false;
}
var reader = pe.GetMetadataReader ();
return HasResourceDesignerAssemblyReference (reader);
}
static bool HasResourceDesignerAssemblyReference (MetadataReader reader)
{
foreach (var handle in reader.AssemblyReferences) {
var reference = reader.GetAssemblyReference (handle);
var name = reader.GetString (reference.Name);
if (string.CompareOrdinal (name, "_Microsoft.Android.Resource.Designer") == 0) {
return true;
}
}
return false;
}
}
}