diff options
Diffstat (limited to 'src/wix/WixToolset.BuildTasks/UpdateProjectReferenceMetadata.cs')
| -rw-r--r-- | src/wix/WixToolset.BuildTasks/UpdateProjectReferenceMetadata.cs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/wix/WixToolset.BuildTasks/UpdateProjectReferenceMetadata.cs b/src/wix/WixToolset.BuildTasks/UpdateProjectReferenceMetadata.cs new file mode 100644 index 00000000..4de948ba --- /dev/null +++ b/src/wix/WixToolset.BuildTasks/UpdateProjectReferenceMetadata.cs | |||
| @@ -0,0 +1,95 @@ | |||
| 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.IO; | ||
| 8 | using Microsoft.Build.Framework; | ||
| 9 | using Microsoft.Build.Utilities; | ||
| 10 | |||
| 11 | /// <summary> | ||
| 12 | /// This task adds publish metadata to the appropriate project references. | ||
| 13 | /// </summary> | ||
| 14 | public class UpdateProjectReferenceMetadata : Task | ||
| 15 | { | ||
| 16 | /// <summary> | ||
| 17 | /// The list of project references that exist. | ||
| 18 | /// </summary> | ||
| 19 | [Required] | ||
| 20 | public ITaskItem[] ProjectReferences { get; set; } | ||
| 21 | |||
| 22 | [Required] | ||
| 23 | public string IntermediateFolder { get; set; } | ||
| 24 | |||
| 25 | [Output] | ||
| 26 | public ITaskItem[] UpdatedProjectReferences { get; private set; } | ||
| 27 | |||
| 28 | /// <summary> | ||
| 29 | /// Finds all project references requesting publishing and updates them to publish instead of build. | ||
| 30 | /// </summary> | ||
| 31 | /// <returns>True upon completion of the task execution.</returns> | ||
| 32 | public override bool Execute() | ||
| 33 | { | ||
| 34 | var publishProjectReferences = new List<ITaskItem>(); | ||
| 35 | var intermediateFolder = Path.GetFullPath(this.IntermediateFolder); | ||
| 36 | |||
| 37 | foreach (var projectReference in this.ProjectReferences) | ||
| 38 | { | ||
| 39 | var publish = projectReference.GetMetadata("Publish"); | ||
| 40 | var publishDir = projectReference.GetMetadata("PublishDir"); | ||
| 41 | |||
| 42 | if (publish.Equals("true", StringComparison.OrdinalIgnoreCase) || | ||
| 43 | (String.IsNullOrWhiteSpace(publish) && !String.IsNullOrWhiteSpace(publishDir))) | ||
| 44 | { | ||
| 45 | publishDir = String.IsNullOrWhiteSpace(publishDir) ? this.CalculatePublishDirFromProjectReference(projectReference, intermediateFolder) : Path.GetFullPath(publishDir); | ||
| 46 | |||
| 47 | this.AddPublishPropertiesToProjectReference(projectReference, publishDir); | ||
| 48 | |||
| 49 | publishProjectReferences.Add(projectReference); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | this.UpdatedProjectReferences = publishProjectReferences.ToArray(); | ||
| 54 | |||
| 55 | return true; | ||
| 56 | } | ||
| 57 | |||
| 58 | private string CalculatePublishDirFromProjectReference(ITaskItem projectReference, string intermediateFolder) | ||
| 59 | { | ||
| 60 | var publishDir = Path.Combine("publish", Path.GetFileNameWithoutExtension(projectReference.ItemSpec)); | ||
| 61 | |||
| 62 | return Path.Combine(intermediateFolder, publishDir); | ||
| 63 | } | ||
| 64 | |||
| 65 | private void AddPublishPropertiesToProjectReference(ITaskItem projectReference, string publishDir) | ||
| 66 | { | ||
| 67 | var additionalProperties = projectReference.GetMetadata("AdditionalProperties"); | ||
| 68 | if (!String.IsNullOrWhiteSpace(additionalProperties)) | ||
| 69 | { | ||
| 70 | additionalProperties += ";"; | ||
| 71 | } | ||
| 72 | |||
| 73 | additionalProperties += "PublishDir=" + publishDir; | ||
| 74 | |||
| 75 | var bindPath = ToolsCommon.GetMetadataOrDefault(projectReference, "BindPath", publishDir); | ||
| 76 | |||
| 77 | var publishTargets = projectReference.GetMetadata("PublishTargets"); | ||
| 78 | if (String.IsNullOrWhiteSpace(publishTargets)) | ||
| 79 | { | ||
| 80 | publishTargets = "Publish;GetTargetPath"; | ||
| 81 | } | ||
| 82 | else if (!publishTargets.EndsWith(";GetTargetsPath", StringComparison.OrdinalIgnoreCase)) | ||
| 83 | { | ||
| 84 | publishTargets += ";GetTargetsPath"; | ||
| 85 | } | ||
| 86 | |||
| 87 | projectReference.SetMetadata("AdditionalProperties", additionalProperties); | ||
| 88 | projectReference.SetMetadata("BindPath", bindPath); | ||
| 89 | projectReference.SetMetadata("Targets", publishTargets); | ||
| 90 | |||
| 91 | this.Log.LogMessage(MessageImportance.Low, "Adding publish metadata to project reference {0} Targets {1}, BindPath {2}, AdditionalProperties: {3}", | ||
| 92 | projectReference.ItemSpec, projectReference.GetMetadata("Targets"), projectReference.GetMetadata("BindPath"), projectReference.GetMetadata("AdditionalProperties")); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
