-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathConvertCustomView.cs
More file actions
176 lines (157 loc) · 6.32 KB
/
ConvertCustomView.cs
File metadata and controls
176 lines (157 loc) · 6.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2018 Microsoft, Inc. All rights reserved.
#nullable enable
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Monodroid;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks {
public class ConvertCustomView : AndroidTask {
public override string TaskPrefix => "CCV";
[Required]
public string CustomViewMapFile { get; set; } = "";
[Required]
public string AcwMapFile { get; set; } = "";
public ITaskItem []? ResourceDirectories { get; set; }
[Output]
public ITaskItem []? Processed { get; set; }
Dictionary<string, string>? _resource_name_case_map;
Dictionary<string, string> resource_name_case_map => _resource_name_case_map ??= MonoAndroidHelper.LoadResourceCaseMap (BuildEngine4, ProjectSpecificTaskObjectKey);
public override bool RunTask ()
{
var acw_map = MonoAndroidHelper.LoadMapFile (BuildEngine4, Path.GetFullPath (AcwMapFile), StringComparer.Ordinal);
var customViewMap = MonoAndroidHelper.LoadCustomViewMapFile (BuildEngine4, CustomViewMapFile);
var processed = new HashSet<string> ();
foreach (var kvp in acw_map) {
var key = kvp.Key;
var value = kvp.Value;
if (key == value)
continue;
if (customViewMap.TryGetValue (key, out HashSet<string> resourceFiles)) {
foreach (var file in resourceFiles) {
if (processed.Contains (file))
continue;
if (!File.Exists (file))
continue;
var document = XDocument.Load (file, options : LoadOptions.SetLineInfo);
var e = document.Root;
bool update = false;
foreach (var elem in AndroidResource.GetElements (e).Prepend (e)) {
update |= TryFixCustomView (elem, acw_map, (level, message) => {
ITaskItem? resdir = ResourceDirectories?.FirstOrDefault (x => file.StartsWith (x.ItemSpec, StringComparison.OrdinalIgnoreCase));
switch (level) {
case TraceLevel.Error:
if (resdir != null)
Log.FixupResourceFilenameAndLogCodedError ("XA1002", message, file, resdir.ItemSpec, resource_name_case_map);
else
Log.LogCodedError ("XA1002", file: file, lineNumber: 0, message: message);
break;
case TraceLevel.Warning:
if (resdir != null)
Log.FixupResourceFilenameAndLogCodedError ("XA1001", message, file, resdir.ItemSpec, resource_name_case_map);
else
Log.LogCodedWarning ("XA1001", file: file, lineNumber: 0, message: message);
break;
default:
Log.LogDebugMessage (message);
break;
}
});
}
foreach (XAttribute a in AndroidResource.GetAttributes (e)) {
update |= TryFixCustomClassAttribute (a, acw_map);
update |= TryFixFragment (a, acw_map);
}
if (update) {
var lastModified = File.GetLastWriteTimeUtc (file);
if (document.SaveIfChanged (file)) {
Log.LogDebugMessage ($"Fixed up Custom Views in {file}");
File.SetLastWriteTimeUtc (file, lastModified);
}
}
processed.Add (file);
}
}
}
var output = new Dictionary<string, ITaskItem> (processed.Count);
foreach (var file in processed) {
ITaskItem? resdir = ResourceDirectories?.FirstOrDefault (x => file.StartsWith (x.ItemSpec, StringComparison.OrdinalIgnoreCase));
var hash = resdir?.GetMetadata ("Hash");
var stamp = resdir?.GetMetadata ("StampFile");
var filename = !hash.IsNullOrEmpty () ? hash : "compiled";
var stampFile = !stamp.IsNullOrEmpty () ? stamp : $"{filename}.stamp";
Log.LogDebugMessage ($"{filename} {stampFile}");
output.Add (file, new TaskItem (Path.GetFullPath (file), new Dictionary<string, string> {
{ "StampFile" , stampFile },
{ "Hash" , filename },
{ "Resource", resdir?.ItemSpec ?? file },
}));
}
Processed = output.Values.ToArray ();
return !Log.HasLoggedErrors;
}
static readonly XNamespace res_auto = "http://schemas.android.com/apk/res-auto";
static readonly XNamespace android = "http://schemas.android.com/apk/res/android";
bool TryFixCustomClassAttribute (XAttribute attr, Dictionary<string, string> acwMap)
{
/* Some attributes reference a Java class name.
* try to convert those like for TryFixCustomView
*/
if (attr.Name != (res_auto + "layout_behavior") && // For custom CoordinatorLayout behavior
(attr.Parent.Name != "transition" || attr.Name.LocalName != "class")) // For custom transitions
return false;
if (!acwMap.TryGetValue (attr.Value, out string mappedValue))
return false;
attr.Value = mappedValue;
return true;
}
bool TryFixFragment (XAttribute attr, Dictionary<string, string> acwMap)
{
// Looks for any:
// <fragment class="My.DotNet.Class"
// <fragment android:name="My.DotNet.Class" ...
// and tries to change it to the ACW name
if (attr.Parent.Name != "fragment")
return false;
if (attr.Name == "class" || attr.Name == android + "name") {
if (acwMap.TryGetValue (attr.Value, out string mappedValue)) {
attr.Value = mappedValue;
return true;
} else if (attr.Value?.Contains (',') ?? false) {
// attr.Value could be an assembly-qualified name that isn't in acw-map.txt;
// see e5b1c92c, https://github.com/xamarin/xamarin-android/issues/1296#issuecomment-365091948
var n = attr.Value.Substring (0, attr.Value.IndexOf (','));
if (acwMap.TryGetValue (n, out mappedValue)) {
attr.Value = mappedValue;
return true;
}
}
}
return false;
}
bool TryFixCustomView (XElement elem, Dictionary<string, string> acwMap, Action<TraceLevel, string>? logMessage = null)
{
// Looks for any <My.DotNet.Class ...
// and tries to change it to the ACW name
string name = elem.Name.ToString ();
if (acwMap.TryGetValue (name, out string mappedValue)) {
elem.Name = mappedValue;
return true;
}
if (logMessage == null)
return false;
var matchingKey = acwMap.FirstOrDefault (x => String.Equals (x.Key, name, StringComparison.OrdinalIgnoreCase));
if (matchingKey.Key != null) {
// we have elements with slightly different casing.
// lets issue a error.
logMessage (TraceLevel.Error, string.Format (Properties.Resources.XA1002, name, matchingKey.Key));
}
return false;
}
}
}