-
Notifications
You must be signed in to change notification settings - Fork 555
[Xamarin.Android.Build.Tasks] Add ConvertCustomView Task #2129
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
src/Xamarin.Android.Build.Tasks/Tasks/ConvertCustomView.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright (C) 2018 Microsoft, Inc. All rights reserved. | ||
|
||
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; | ||
|
||
namespace Xamarin.Android.Tasks { | ||
public class ConvertCustomView : Task { | ||
|
||
[Required] | ||
public string CustomViewMapFile { get; set; } | ||
|
||
[Required] | ||
public string AcwMapFile { get; set; } | ||
|
||
public string ResourceNameCaseMap { get; set; } | ||
|
||
public ITaskItem [] ResourceDirectories { get; set; } | ||
|
||
public override bool Execute () | ||
{ | ||
var resource_name_case_map = MonoAndroidHelper.LoadResourceCaseMap (ResourceNameCaseMap); | ||
var acw_map = MonoAndroidHelper.LoadAcwMapFile (AcwMapFile); | ||
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); | ||
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)) ?? null; | ||
switch (level) { | ||
case TraceLevel.Error: | ||
Log.FixupResourceFilenameAndLogCodedError ("XA1002", message, file, resdir.ItemSpec, resource_name_case_map); | ||
break; | ||
case TraceLevel.Warning: | ||
Log.FixupResourceFilenameAndLogCodedError ("XA1001", message, file, resdir.ItemSpec, resource_name_case_map); | ||
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) { | ||
document.Save (file); | ||
} | ||
processed.Add (file); | ||
} | ||
} | ||
} | ||
|
||
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, $"We found a matching key '{matchingKey.Key}' for '{name}'. But the casing was incorrect. Please correct the casing"); | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this file being used before it gets deleted