|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Xml.Linq; |
| 7 | +using Microsoft.Build.Framework; |
| 8 | +using Microsoft.Build.Utilities; |
| 9 | + |
| 10 | +namespace Xamarin.Android.Tasks |
| 11 | +{ |
| 12 | + public class ManifestMerger : JavaToolTask |
| 13 | + { |
| 14 | + public override string TaskPrefix => "AMM"; |
| 15 | + |
| 16 | + public override string DefaultErrorCode => $"{TaskPrefix}0000"; |
| 17 | + |
| 18 | + [Required] |
| 19 | + public string ManifestMergerJarPath { get; set; } |
| 20 | + |
| 21 | + [Required] |
| 22 | + public string AndroidManifest { get; set; } |
| 23 | + |
| 24 | + [Required] |
| 25 | + public string OutputManifestFile { get; set; } |
| 26 | + |
| 27 | + public string [] LibraryManifestFiles { get; set; } |
| 28 | + |
| 29 | + public string [] ManifestPlaceholders { get; set; } |
| 30 | + |
| 31 | + string tempFile; |
| 32 | + string responseFile; |
| 33 | + |
| 34 | + public override bool Execute () |
| 35 | + { |
| 36 | + tempFile = OutputManifestFile + ".tmp"; |
| 37 | + responseFile = Path.Combine (Path.GetDirectoryName (OutputManifestFile), "manifestmerger.rsp"); |
| 38 | + try { |
| 39 | + bool result = base.Execute (); |
| 40 | + if (!result) |
| 41 | + return result; |
| 42 | + var m = new ManifestDocument (tempFile, Log); |
| 43 | + using (var ms = new MemoryStream ()) { |
| 44 | + m.Save (ms); |
| 45 | + MonoAndroidHelper.CopyIfStreamChanged (ms, OutputManifestFile); |
| 46 | + return result; |
| 47 | + } |
| 48 | + } finally { |
| 49 | + if (File.Exists (tempFile)) |
| 50 | + File.Delete (tempFile); |
| 51 | + if (File.Exists (responseFile)) |
| 52 | + File.Delete (responseFile); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected override string GenerateCommandLineCommands () |
| 57 | + { |
| 58 | + string cmd = GetCommandLineBuilder ().ToString (); |
| 59 | + Log.LogDebugMessage (cmd); |
| 60 | + return cmd; |
| 61 | + } |
| 62 | + |
| 63 | + protected virtual CommandLineBuilder GetCommandLineBuilder () |
| 64 | + { |
| 65 | + var cmd = new CommandLineBuilder (); |
| 66 | + |
| 67 | + if (!string.IsNullOrEmpty (JavaOptions)) { |
| 68 | + cmd.AppendSwitch (JavaOptions); |
| 69 | + } |
| 70 | + cmd.AppendSwitchIfNotNull ("-Xmx", JavaMaximumHeapSize); |
| 71 | + cmd.AppendSwitchIfNotNull ("-cp ", $"{ManifestMergerJarPath}"); |
| 72 | + cmd.AppendSwitch ("com.xamarin.manifestmerger.Main"); |
| 73 | + StringBuilder sb = new StringBuilder (); |
| 74 | + sb.AppendLine ("--main"); |
| 75 | + sb.AppendLine (AndroidManifest); |
| 76 | + if (LibraryManifestFiles != null) { |
| 77 | + sb.AppendLine ("--libs"); |
| 78 | + sb.AppendLine ($"{string.Join ($"{Path.PathSeparator}", LibraryManifestFiles)}"); |
| 79 | + } |
| 80 | + if (ManifestPlaceholders != null) { |
| 81 | + foreach (var entry in ManifestPlaceholders.Select (e => e.Split (new char [] { '=' }, 2, StringSplitOptions.None))) { |
| 82 | + if (entry.Length == 2) { |
| 83 | + sb.AppendLine ("--placeholder"); |
| 84 | + sb.AppendLine ($"{entry [0]}={entry [1]}"); |
| 85 | + } else |
| 86 | + Log.LogWarning ("Invalid application placeholders (AndroidApplicationPlaceholders) value. Use 'key1=value1;key2=value2, ...' format. The specified value was: " + ManifestPlaceholders); |
| 87 | + } |
| 88 | + } |
| 89 | + sb.AppendLine ("--out"); |
| 90 | + sb.AppendLine (tempFile); |
| 91 | + File.WriteAllText (responseFile, sb.ToString ()); |
| 92 | + cmd.AppendFileNameIfNotNull (responseFile); |
| 93 | + return cmd; |
| 94 | + } |
| 95 | + |
| 96 | + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) |
| 97 | + { |
| 98 | + if (ExitCode != 0) |
| 99 | + Log.LogCodedError (DefaultErrorCode, singleLine); |
| 100 | + base.LogEventsFromTextOutput (singleLine, messageImportance); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments