|
| 1 | +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Runtime.Versioning; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Build.Framework; |
| 9 | +using Microsoft.Build.Utilities; |
| 10 | +using Microsoft.Android.Build.Tasks; |
| 11 | +using Mono.Cecil; |
| 12 | +using Mono.Cecil.Cil; |
| 13 | + |
| 14 | +namespace Xamarin.Android.Tasks |
| 15 | +{ |
| 16 | + public class GenerateResourceDesignerAssembly : AndroidTask |
| 17 | + { |
| 18 | + const string DesignerAssemblyName = "Xamarin.Android.Resource.Designer"; |
| 19 | + public override string TaskPrefix => "GRDA"; |
| 20 | + |
| 21 | + [Required] |
| 22 | + public ITaskItem RTxtFile { get; set; } |
| 23 | + |
| 24 | + public ITaskItem ResourceMap { get; set; } |
| 25 | + |
| 26 | + [Required] |
| 27 | + public bool IsApplication { get; set; } |
| 28 | + |
| 29 | + [Required] |
| 30 | + public bool DesignTimeBuild { get; set; } |
| 31 | + |
| 32 | + [Required] |
| 33 | + public ITaskItem OutputFile { get; set; } |
| 34 | + |
| 35 | + [Required] |
| 36 | + public string TargetFrameworkVersion { get; set; } |
| 37 | + |
| 38 | + [Required] |
| 39 | + public string TargetFrameworkIdentifier { get; set; } |
| 40 | + |
| 41 | + public bool UsingAndroidNETSdk { get; set; } = false; |
| 42 | + |
| 43 | + public string[] Namespaces { get; set; } |
| 44 | + |
| 45 | + public override bool RunTask () |
| 46 | + { |
| 47 | + // Generate an assembly which contains all the values in the provided |
| 48 | + // R.txt file. |
| 49 | + var assembly = AssemblyDefinition.CreateAssembly ( |
| 50 | + new AssemblyNameDefinition (DesignerAssemblyName, new Version ()), |
| 51 | + DesignerAssemblyName, |
| 52 | + ModuleKind.Dll); |
| 53 | + |
| 54 | + var module = assembly.MainModule; |
| 55 | + |
| 56 | + if (!IsApplication) { |
| 57 | + MethodReference referenceAssemblyConstructor = module.ImportReference ( typeof (ReferenceAssemblyAttribute).GetConstructor (Type.EmptyTypes)); |
| 58 | + module.Assembly.CustomAttributes.Add (new CustomAttribute (referenceAssemblyConstructor)); |
| 59 | + } |
| 60 | + |
| 61 | + MethodReference targetFrameworkConstructor = module.ImportReference (typeof (TargetFrameworkAttribute).GetConstructor(new [] { typeof (string) })); |
| 62 | + var attr = new CustomAttribute (targetFrameworkConstructor); |
| 63 | + attr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, $"{TargetFrameworkIdentifier},Version={TargetFrameworkVersion}")); |
| 64 | + module.Assembly.CustomAttributes.Add (attr); |
| 65 | + |
| 66 | + if (UsingAndroidNETSdk) { |
| 67 | + // add .net 6 specific attributes we might not need these. |
| 68 | + // TargetPlatform |
| 69 | + // SupportedOSPlatform |
| 70 | + } |
| 71 | + |
| 72 | + if (!UsingAndroidNETSdk) |
| 73 | + module.AssemblyReferences.Add(AssemblyNameReference.Parse("mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e")); |
| 74 | + module.AssemblyReferences.Add(AssemblyNameReference.Parse("Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065")); |
| 75 | + module.AssemblyReferences.Add(AssemblyNameReference.Parse("System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e")); |
| 76 | + |
| 77 | + var att = TypeAttributes.Class | TypeAttributes.Public; |
| 78 | + |
| 79 | + var resourceDesigner = new TypeDefinition( |
| 80 | + DesignerAssemblyName, |
| 81 | + "Resource", |
| 82 | + att, |
| 83 | + module.TypeSystem.Object); |
| 84 | + module.Types.Add(resourceDesigner); |
| 85 | + |
| 86 | + ProcessRtxtFile (RTxtFile.ItemSpec, resourceDesigner, module); |
| 87 | + |
| 88 | + foreach (var ns in (Namespaces ?? Array.Empty<string> ())) { |
| 89 | + module.Types.Add (new TypeDefinition (ns, "Resource", att, resourceDesigner)); |
| 90 | + } |
| 91 | + |
| 92 | + assembly.Write (OutputFile.ItemSpec); |
| 93 | + return !Log.HasLoggedErrors; |
| 94 | + } |
| 95 | + |
| 96 | + void ProcessRtxtFile (string file, TypeDefinition resourceDesigner, ModuleDefinition module) |
| 97 | + { |
| 98 | + var lines = System.IO.File.ReadLines (file); |
| 99 | + foreach (var line in lines) { |
| 100 | + var items = line.Split (new char [] { ' ' }, 4); |
| 101 | + int value = items [1] != "styleable" ? Convert.ToInt32 (items [3], 16) : -1; |
| 102 | + string itemName = items [2]; |
| 103 | + switch (items [1]) { |
| 104 | + case "anim": |
| 105 | + case "animator": |
| 106 | + case "attr": |
| 107 | + case "array": |
| 108 | + case "bool": |
| 109 | + case "color": |
| 110 | + case "dimen": |
| 111 | + case "drawable": |
| 112 | + case "font": |
| 113 | + case "id": |
| 114 | + case "integer": |
| 115 | + case "interpolator": |
| 116 | + case "layout": |
| 117 | + case "menu": |
| 118 | + case "mipmap": |
| 119 | + case "plurals": |
| 120 | + case "raw": |
| 121 | + case "string": |
| 122 | + case "style": |
| 123 | + case "transition": |
| 124 | + case "xml": |
| 125 | + CreateIntProperty (items [1], itemName, value, resourceDesigner, module); |
| 126 | + break; |
| 127 | + // case "styleable": |
| 128 | + // switch (items [0]) { |
| 129 | + // case "int": |
| 130 | + // CreateIntField (styleable, itemName, Convert.ToInt32 (items [3], 10)); |
| 131 | + // break; |
| 132 | + // case "int[]": |
| 133 | + // var arrayValues = items [3].Trim (new char [] { '{', '}' }) |
| 134 | + // .Replace (" ", "") |
| 135 | + // .Split (new char [] { ',' }); |
| 136 | + // CreateIntArrayField (styleable, itemName, arrayValues.Length, |
| 137 | + // arrayValues.Select (x => string.IsNullOrEmpty (x) ? -1 : Convert.ToInt32 (x, 16)).ToArray ()); |
| 138 | + // break; |
| 139 | + // } |
| 140 | + // break; |
| 141 | + // for custom views |
| 142 | + default: |
| 143 | + CreateIntProperty (items [1], itemName, value, resourceDesigner, module); |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + void CreateIntProperty (string resourceClass, string propertyName, int value, TypeDefinition resourceDesigner, ModuleDefinition module) |
| 150 | + { |
| 151 | + TypeDefinition nestedType = CreateResourceClass (resourceDesigner, resourceClass, module); |
| 152 | + PropertyDefinition p = CreateProperty (propertyName, value, module); |
| 153 | + nestedType.Properties.Add (p); |
| 154 | + nestedType.Methods.Add (p.GetMethod); |
| 155 | + } |
| 156 | + |
| 157 | + Dictionary<string, TypeDefinition> resourceClasses = new Dictionary<string, TypeDefinition> (); |
| 158 | + |
| 159 | + TypeDefinition CreateResourceClass (TypeDefinition resourceDesigner, string className, ModuleDefinition module) |
| 160 | + { |
| 161 | + string name = ResourceParser.GetNestedTypeName (className); |
| 162 | + if (resourceClasses.ContainsKey (name)) { |
| 163 | + return resourceClasses[name]; |
| 164 | + } |
| 165 | + var resourceClass = new TypeDefinition (DesignerAssemblyName, name, TypeAttributes.Class | TypeAttributes.Public, module.TypeSystem.Object); |
| 166 | + resourceDesigner.NestedTypes.Add(resourceClass); |
| 167 | + resourceClasses [name] = resourceClass; |
| 168 | + return resourceClass; |
| 169 | + } |
| 170 | + |
| 171 | + PropertyDefinition CreateProperty (string propertyName, int value, ModuleDefinition module) |
| 172 | + { |
| 173 | + var p = new PropertyDefinition (propertyName, PropertyAttributes.None, module.TypeSystem.Int32); |
| 174 | + var getter = new MethodDefinition ($"{propertyName}_get", MethodAttributes.Public | MethodAttributes.Static, module.TypeSystem.Int32); |
| 175 | + p.GetMethod = getter; |
| 176 | + p.SetMethod = null; |
| 177 | + var il = p.GetMethod.Body.GetILProcessor (); |
| 178 | + il.Emit (OpCodes.Ldc_I4, value); |
| 179 | + il.Emit (OpCodes.Ret); |
| 180 | + return p; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments