2018-12-12 17:55:57 +03:00
|
|
|
using System;
|
2022-02-24 20:55:04 +03:00
|
|
|
using System.Collections;
|
2018-12-12 17:55:57 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
using Microsoft.Build.Framework;
|
|
|
|
using Microsoft.Build.Utilities;
|
2021-10-01 09:40:01 +03:00
|
|
|
|
2020-03-14 00:46:28 +03:00
|
|
|
using Xamarin.Localization.MSBuild;
|
2021-10-01 09:40:01 +03:00
|
|
|
using Xamarin.Utils;
|
2018-12-12 17:55:57 +03:00
|
|
|
|
|
|
|
namespace Xamarin.MacDev.Tasks {
|
2020-05-13 16:44:59 +03:00
|
|
|
public abstract class CreateBindingResourcePackageBase : XamarinTask {
|
2018-12-12 17:55:57 +03:00
|
|
|
[Required]
|
2021-10-01 09:40:01 +03:00
|
|
|
public string Compress { get; set; }
|
|
|
|
|
2018-12-12 17:55:57 +03:00
|
|
|
[Required]
|
2021-10-01 09:40:01 +03:00
|
|
|
public string BindingResourcePath { get; set; }
|
|
|
|
|
2018-12-12 17:55:57 +03:00
|
|
|
[Required]
|
2021-10-01 09:40:01 +03:00
|
|
|
public string IntermediateOutputPath { get; set; }
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
public ITaskItem[] NativeReferences { get; set; }
|
2018-12-12 17:55:57 +03:00
|
|
|
|
|
|
|
public override bool Execute ()
|
|
|
|
{
|
|
|
|
// LinkWith must be migrated for NoBindingEmbedding styled binding projects
|
|
|
|
if (NativeReferences.Length == 0) {
|
2020-03-14 00:46:28 +03:00
|
|
|
Log.LogError (7068, null, MSBStrings.E7068);
|
2018-12-12 17:55:57 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-01 09:40:01 +03:00
|
|
|
var compress = false;
|
|
|
|
if (string.Equals (Compress, "true", StringComparison.OrdinalIgnoreCase)) {
|
|
|
|
compress = true;
|
|
|
|
} else if (string.Equals (Compress, "false", StringComparison.OrdinalIgnoreCase)) {
|
|
|
|
compress = false;
|
|
|
|
} else if (string.Equals (Compress, "auto", StringComparison.OrdinalIgnoreCase)) {
|
|
|
|
compress = ContainsSymlinks (NativeReferences);
|
|
|
|
if (compress)
|
|
|
|
Log.LogMessage (MessageImportance.Low, MSBStrings.W7085 /* "Creating a compressed binding resource package because there are symlinks in the input." */);
|
|
|
|
} else {
|
|
|
|
Log.LogError (MSBStrings.E7086 /* "The value '{0}' is invalid for the Compress property. Valid values: 'true', 'false' or 'auto'." */, Compress);
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory.CreateDirectory (compress ? IntermediateOutputPath : BindingResourcePath);
|
2018-12-12 17:55:57 +03:00
|
|
|
|
2021-10-01 09:40:01 +03:00
|
|
|
var manifestDirectory = compress ? IntermediateOutputPath : BindingResourcePath;
|
|
|
|
var manifestPath = CreateManifest (manifestDirectory);
|
2018-12-12 17:55:57 +03:00
|
|
|
|
2021-10-01 09:40:01 +03:00
|
|
|
if (compress) {
|
|
|
|
var zipFile = Path.GetFullPath (BindingResourcePath + ".zip");
|
|
|
|
Log.LogMessage (MSBStrings.M0121, zipFile);
|
|
|
|
if (File.Exists (zipFile))
|
|
|
|
File.Delete (zipFile);
|
|
|
|
Directory.CreateDirectory (Path.GetDirectoryName (zipFile));
|
2018-12-12 17:55:57 +03:00
|
|
|
|
2021-10-01 09:40:01 +03:00
|
|
|
var filesToZip = NativeReferences.Select (v => v.ItemSpec).ToList ();
|
|
|
|
filesToZip.Add (manifestPath);
|
|
|
|
|
|
|
|
foreach (var nativeRef in filesToZip) {
|
|
|
|
var zipArguments = new List<string> ();
|
|
|
|
zipArguments.Add ("-9");
|
|
|
|
zipArguments.Add ("-r");
|
|
|
|
zipArguments.Add ("-y");
|
|
|
|
zipArguments.Add (zipFile);
|
|
|
|
|
|
|
|
var fullPath = Path.GetFullPath (nativeRef);
|
|
|
|
var workingDirectory = Path.GetDirectoryName (fullPath);
|
|
|
|
zipArguments.Add (Path.GetFileName (fullPath));
|
|
|
|
ExecuteAsync ("zip", zipArguments, workingDirectory: workingDirectory).Wait ();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var bindingResourcePath = BindingResourcePath;
|
|
|
|
Log.LogMessage (MSBStrings.M0121, bindingResourcePath);
|
|
|
|
Directory.CreateDirectory (bindingResourcePath);
|
|
|
|
foreach (var nativeRef in NativeReferences)
|
2022-02-24 18:51:49 +03:00
|
|
|
Xamarin.Bundler.FileCopier.UpdateDirectory (nativeRef.ItemSpec, bindingResourcePath, FileCopierReportErrorCallback, FileCopierLogCallback);
|
2021-10-01 09:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return !Log.HasLoggedErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ContainsSymlinks (ITaskItem[] items)
|
|
|
|
{
|
|
|
|
foreach (var item in items) {
|
|
|
|
if (PathUtils.IsSymlinkOrContainsSymlinks (item.ItemSpec))
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-12 17:55:57 +03:00
|
|
|
|
2021-10-01 09:40:01 +03:00
|
|
|
return false;
|
2018-12-12 17:55:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
string [] NativeReferenceAttributeNames = new string [] { "Kind", "ForceLoad", "SmartLink", "Frameworks", "WeakFrameworks", "LinkerFlags", "NeedsGccExceptionHandling", "IsCxx"};
|
|
|
|
|
|
|
|
string CreateManifest (string resourcePath)
|
|
|
|
{
|
|
|
|
XmlWriterSettings settings = new XmlWriterSettings() {
|
|
|
|
OmitXmlDeclaration = true,
|
|
|
|
Indent = true,
|
|
|
|
IndentChars = "\t",
|
|
|
|
};
|
|
|
|
|
|
|
|
string manifestPath = Path.Combine (resourcePath, "manifest");
|
|
|
|
using (var writer = XmlWriter.Create (manifestPath, settings)) {
|
|
|
|
writer.WriteStartElement ("BindingAssembly");
|
|
|
|
|
|
|
|
foreach (var nativeRef in NativeReferences) {
|
|
|
|
writer.WriteStartElement ("NativeReference");
|
|
|
|
writer.WriteAttributeString ("Name", Path.GetFileName (nativeRef.ItemSpec));
|
|
|
|
|
2022-02-24 20:55:04 +03:00
|
|
|
var customMetadata = nativeRef.CloneCustomMetadataToDictionary ();
|
|
|
|
var allKeys = customMetadata.Keys.Union (NativeReferenceAttributeNames, StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (var key in allKeys.OrderBy (v => v)) {
|
|
|
|
writer.WriteStartElement (key);
|
|
|
|
if (customMetadata.TryGetValue (key, out var value)) {
|
|
|
|
writer.WriteString (value);
|
|
|
|
} else {
|
|
|
|
writer.WriteString (string.Empty);
|
|
|
|
}
|
2018-12-12 17:55:57 +03:00
|
|
|
writer.WriteEndElement ();
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.WriteEndElement ();
|
|
|
|
}
|
|
|
|
writer.WriteEndElement ();
|
|
|
|
}
|
|
|
|
return manifestPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|