|
| 1 | +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Xml; |
| 9 | +using System.Xml.Linq; |
| 10 | +using Microsoft.Build.Utilities; |
| 11 | +using Microsoft.Build.Framework; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | +using System.Collections.Generic; |
| 14 | +using Xamarin.Android.Tools; |
| 15 | +using ThreadingTasks = System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace Xamarin.Android.Tasks { |
| 18 | + |
| 19 | + public class Aapt2 : AsyncTask { |
| 20 | + |
| 21 | + protected Dictionary<string, string> resource_name_case_map = new Dictionary<string, string> (); |
| 22 | + |
| 23 | + public ITaskItem [] ResourceDirectories { get; set; } |
| 24 | + |
| 25 | + public string ResourceNameCaseMap { get; set; } |
| 26 | + |
| 27 | + public string ResourceSymbolsTextFile { get; set; } |
| 28 | + |
| 29 | + protected string ToolName { get { return OS.IsWindows ? "aapt2.exe" : "aapt2"; } } |
| 30 | + |
| 31 | + public string ToolPath { get; set; } |
| 32 | + |
| 33 | + public string ToolExe { get; set; } |
| 34 | + |
| 35 | + protected string ResourceDirectoryFullPath (string resourceDirectory) |
| 36 | + { |
| 37 | + return (Path.IsPathRooted (resourceDirectory) ? resourceDirectory : Path.Combine (WorkingDirectory, resourceDirectory)).TrimEnd ('\\'); |
| 38 | + } |
| 39 | + |
| 40 | + protected string GenerateFullPathToTool () |
| 41 | + { |
| 42 | + return Path.Combine (ToolPath, string.IsNullOrEmpty (ToolExe) ? ToolName : ToolExe); |
| 43 | + } |
| 44 | + |
| 45 | + protected bool RunAapt (string commandLine, IList<OutputLine> output) |
| 46 | + { |
| 47 | + var stdout_completed = new ManualResetEvent (false); |
| 48 | + var stderr_completed = new ManualResetEvent (false); |
| 49 | + var psi = new ProcessStartInfo () { |
| 50 | + FileName = GenerateFullPathToTool (), |
| 51 | + Arguments = commandLine, |
| 52 | + UseShellExecute = false, |
| 53 | + RedirectStandardOutput = true, |
| 54 | + RedirectStandardError = true, |
| 55 | + CreateNoWindow = true, |
| 56 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 57 | + WorkingDirectory = WorkingDirectory, |
| 58 | + }; |
| 59 | + object lockObject = new object (); |
| 60 | + using (var proc = new Process ()) { |
| 61 | + proc.OutputDataReceived += (sender, e) => { |
| 62 | + if (e.Data != null) |
| 63 | + lock (lockObject) |
| 64 | + output.Add (new OutputLine (e.Data, stdError: false)); |
| 65 | + else |
| 66 | + stdout_completed.Set (); |
| 67 | + }; |
| 68 | + proc.ErrorDataReceived += (sender, e) => { |
| 69 | + if (e.Data != null) |
| 70 | + lock (lockObject) |
| 71 | + output.Add (new OutputLine (e.Data, stdError: true)); |
| 72 | + else |
| 73 | + stderr_completed.Set (); |
| 74 | + }; |
| 75 | + LogDebugMessage ("Executing {0}", commandLine); |
| 76 | + proc.StartInfo = psi; |
| 77 | + proc.Start (); |
| 78 | + proc.BeginOutputReadLine (); |
| 79 | + proc.BeginErrorReadLine (); |
| 80 | + Token.Register (() => { |
| 81 | + try { |
| 82 | + proc.Kill (); |
| 83 | + } catch (Exception) { |
| 84 | + } |
| 85 | + }); |
| 86 | + proc.WaitForExit (); |
| 87 | + if (psi.RedirectStandardError) |
| 88 | + stderr_completed.WaitOne (TimeSpan.FromSeconds (30)); |
| 89 | + if (psi.RedirectStandardOutput) |
| 90 | + stdout_completed.WaitOne (TimeSpan.FromSeconds (30)); |
| 91 | + return proc.ExitCode == 0 && !output.Any (x => x.StdError); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance, bool apptResult) |
| 96 | + { |
| 97 | + if (string.IsNullOrEmpty (singleLine)) |
| 98 | + return; |
| 99 | + |
| 100 | + var match = AndroidToolTask.AndroidErrorRegex.Match (singleLine.Trim ()); |
| 101 | + |
| 102 | + if (match.Success) { |
| 103 | + var file = match.Groups ["file"].Value; |
| 104 | + int line = 0; |
| 105 | + if (!string.IsNullOrEmpty (match.Groups ["line"]?.Value)) |
| 106 | + line = int.Parse (match.Groups ["line"].Value) + 1; |
| 107 | + var level = match.Groups ["level"].Value.ToLowerInvariant (); |
| 108 | + var message = match.Groups ["message"].Value; |
| 109 | + if (message.Contains ("fakeLogOpen")) { |
| 110 | + LogMessage (singleLine, messageImportance); |
| 111 | + return; |
| 112 | + } |
| 113 | + if (message.Contains ("note:")) { |
| 114 | + LogMessage (singleLine, messageImportance); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (message.Contains ("warn:")) { |
| 118 | + LogCodedWarning ("APT0000", singleLine); |
| 119 | + return; |
| 120 | + } |
| 121 | + if (level.Contains ("note")) { |
| 122 | + LogMessage (message, messageImportance); |
| 123 | + return; |
| 124 | + } |
| 125 | + if (level.Contains ("warning")) { |
| 126 | + LogCodedWarning ("APT0000", singleLine); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Try to map back to the original resource file, so when the user |
| 131 | + // double clicks the error, it won't take them to the obj/Debug copy |
| 132 | + if (ResourceDirectories != null) { |
| 133 | + foreach (var dir in ResourceDirectories) { |
| 134 | + var resourceDirectoryFullPath = ResourceDirectoryFullPath (dir.ItemSpec); |
| 135 | + if (file.StartsWith (resourceDirectoryFullPath, StringComparison.InvariantCultureIgnoreCase)) { |
| 136 | + var newfile = file.Substring (resourceDirectoryFullPath.Length).TrimStart (Path.DirectorySeparatorChar); |
| 137 | + newfile = resource_name_case_map.ContainsKey (newfile) ? resource_name_case_map [newfile] : newfile; |
| 138 | + newfile = Path.Combine ("Resources", newfile); |
| 139 | + file = newfile; |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Strip any "Error:" text from aapt's output |
| 146 | + if (message.StartsWith ("error: ", StringComparison.InvariantCultureIgnoreCase)) |
| 147 | + message = message.Substring ("error: ".Length); |
| 148 | + |
| 149 | + if (level.Contains ("error") || (line != 0 && !string.IsNullOrEmpty (file))) { |
| 150 | + LogCodedError ("APT0000", message, file, line); |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (!apptResult) { |
| 156 | + LogCodedError ("APT0000", string.Format ("{0} \"{1}\".", singleLine.Trim (), singleLine.Substring (singleLine.LastIndexOfAny (new char [] { '\\', '/' }) + 1)), ToolName); |
| 157 | + } else { |
| 158 | + LogCodedWarning ("APT0000", singleLine); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + protected void LoadResourceCaseMap () |
| 163 | + { |
| 164 | + if (ResourceNameCaseMap != null) |
| 165 | + foreach (var arr in ResourceNameCaseMap.Split (';').Select (l => l.Split ('|')).Where (a => a.Length == 2)) |
| 166 | + resource_name_case_map [arr [1]] = arr [0]; // lowercase -> original |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments