diff options
Diffstat (limited to 'src/WixToolset.BuildTasks/GenerateCompileWithObjectPath.cs')
| -rw-r--r-- | src/WixToolset.BuildTasks/GenerateCompileWithObjectPath.cs | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/WixToolset.BuildTasks/GenerateCompileWithObjectPath.cs b/src/WixToolset.BuildTasks/GenerateCompileWithObjectPath.cs new file mode 100644 index 00000000..06c8b98a --- /dev/null +++ b/src/WixToolset.BuildTasks/GenerateCompileWithObjectPath.cs | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.BuildTasks | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Diagnostics.CodeAnalysis; | ||
| 8 | using System.Globalization; | ||
| 9 | using System.IO; | ||
| 10 | using System.Security.Cryptography; | ||
| 11 | using System.Text; | ||
| 12 | using Microsoft.Build.Framework; | ||
| 13 | using Microsoft.Build.Utilities; | ||
| 14 | |||
| 15 | /// <summary> | ||
| 16 | /// This task generates metadata on the for compile output objects. | ||
| 17 | /// </summary> | ||
| 18 | public class GenerateCompileWithObjectPath : Task | ||
| 19 | { | ||
| 20 | /// <summary> | ||
| 21 | /// The list of files to generate outputs for. | ||
| 22 | /// </summary> | ||
| 23 | [Required] | ||
| 24 | public ITaskItem[] Compile | ||
| 25 | { | ||
| 26 | get; | ||
| 27 | set; | ||
| 28 | } | ||
| 29 | |||
| 30 | /// <summary> | ||
| 31 | /// The list of files with ObjectPath metadata. | ||
| 32 | /// </summary> | ||
| 33 | [Output] | ||
| 34 | public ITaskItem[] CompileWithObjectPath | ||
| 35 | { | ||
| 36 | get; | ||
| 37 | private set; | ||
| 38 | } | ||
| 39 | |||
| 40 | /// <summary> | ||
| 41 | /// The folder under which all ObjectPaths should reside. | ||
| 42 | /// </summary> | ||
| 43 | [Required] | ||
| 44 | public string IntermediateOutputPath | ||
| 45 | { | ||
| 46 | get; | ||
| 47 | set; | ||
| 48 | } | ||
| 49 | |||
| 50 | /// <summary> | ||
| 51 | /// Generate an identifier by hashing data from the row. | ||
| 52 | /// </summary> | ||
| 53 | /// <param name="prefix">Three letter or less prefix for generated row identifier.</param> | ||
| 54 | /// <param name="args">Information to hash.</param> | ||
| 55 | /// <returns>The generated identifier.</returns> | ||
| 56 | [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.InvalidOperationException.#ctor(System.String)")] | ||
| 57 | public static string GenerateIdentifier(string prefix, params string[] args) | ||
| 58 | { | ||
| 59 | string stringData = String.Join("|", args); | ||
| 60 | byte[] data = Encoding.Unicode.GetBytes(stringData); | ||
| 61 | |||
| 62 | // hash the data | ||
| 63 | byte[] hash; | ||
| 64 | |||
| 65 | using (MD5 md5 = new MD5CryptoServiceProvider()) | ||
| 66 | { | ||
| 67 | hash = md5.ComputeHash(data); | ||
| 68 | } | ||
| 69 | |||
| 70 | // build up the identifier | ||
| 71 | StringBuilder identifier = new StringBuilder(35, 35); | ||
| 72 | identifier.Append(prefix); | ||
| 73 | |||
| 74 | // hard coded to 16 as that is the most bytes that can be used to meet the length requirements. SHA1 is 20 bytes. | ||
| 75 | for (int i = 0; i < 16; i++) | ||
| 76 | { | ||
| 77 | identifier.Append(hash[i].ToString("X2", CultureInfo.InvariantCulture.NumberFormat)); | ||
| 78 | } | ||
| 79 | |||
| 80 | return identifier.ToString(); | ||
| 81 | } | ||
| 82 | |||
| 83 | /// <summary> | ||
| 84 | /// Gets the full path of the directory in which the file is found. | ||
| 85 | /// </summary> | ||
| 86 | /// <param name='file'>The file from which to extract the directory.</param> | ||
| 87 | /// <returns>The generated identifier.</returns> | ||
| 88 | private static string GetDirectory(ITaskItem file) | ||
| 89 | { | ||
| 90 | return file.GetMetadata("RootDir") + file.GetMetadata("Directory"); | ||
| 91 | } | ||
| 92 | |||
| 93 | /// <summary> | ||
| 94 | /// Sets the object path to use for the file. | ||
| 95 | /// </summary> | ||
| 96 | /// <param name='file'>The file on which to set the ObjectPath metadata.</param> | ||
| 97 | /// <remarks> | ||
| 98 | /// For the same input path it will return the same ObjectPath. Case is not ignored, however that isn't a problem. | ||
| 99 | /// </remarks> | ||
| 100 | private void SetObjectPath(ITaskItem file) | ||
| 101 | { | ||
| 102 | // If the source file is in the project directory or in the intermediate directory, use the intermediate directory. | ||
| 103 | if (string.IsNullOrEmpty(file.GetMetadata("RelativeDir")) || string.Compare(file.GetMetadata("RelativeDir"), this.IntermediateOutputPath, StringComparison.OrdinalIgnoreCase) == 0) | ||
| 104 | { | ||
| 105 | file.SetMetadata("ObjectPath", this.IntermediateOutputPath); | ||
| 106 | } | ||
| 107 | // Otherwise use a subdirectory of the intermediate directory. The subfolder's name is based on the full path of the folder containing the source file. | ||
| 108 | else | ||
| 109 | { | ||
| 110 | file.SetMetadata("ObjectPath", Path.Combine(this.IntermediateOutputPath, GenerateIdentifier("pth", GetDirectory(file))) + Path.DirectorySeparatorChar); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | /// <summary> | ||
| 115 | /// Gets a complete list of external cabs referenced by the given installer database file. | ||
| 116 | /// </summary> | ||
| 117 | /// <returns>True upon completion of the task execution.</returns> | ||
| 118 | public override bool Execute() | ||
| 119 | { | ||
| 120 | if (string.IsNullOrEmpty(this.IntermediateOutputPath)) | ||
| 121 | { | ||
| 122 | this.Log.LogError("IntermediateOutputPath parameter is required and cannot be empty"); | ||
| 123 | return false; | ||
| 124 | } | ||
| 125 | |||
| 126 | if (this.Compile == null || this.Compile.Length == 0) | ||
| 127 | { | ||
| 128 | return true; | ||
| 129 | } | ||
| 130 | |||
| 131 | this.CompileWithObjectPath = new ITaskItem[this.Compile.Length]; | ||
| 132 | for (int i = 0; i < this.Compile.Length; ++i) | ||
| 133 | { | ||
| 134 | this.CompileWithObjectPath[i] = new TaskItem(this.Compile[i].ItemSpec, this.Compile[i].CloneCustomMetadata()); | ||
| 135 | |||
| 136 | // Do not overwrite the ObjectPath metadata if it already was set. | ||
| 137 | if (string.IsNullOrEmpty(this.CompileWithObjectPath[i].GetMetadata("ObjectPath"))) | ||
| 138 | { | ||
| 139 | SetObjectPath(this.CompileWithObjectPath[i]); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | return true; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
