| 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.HeatTasks |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections; |
| 7 | using System.Globalization; |
| 8 | using System.IO; |
| 9 | using System.Xml; |
| 10 | using Microsoft.Build.Framework; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// This task refreshes the generated file for bundle projects. |
| 14 | /// </summary> |
| 15 | public class RefreshBundleGeneratedFile : RefreshTask |
| 16 | { |
| 17 | /// <summary> |
| 18 | /// Gets a complete list of external cabs referenced by the given installer database file. |
| 19 | /// </summary> |
| 20 | /// <returns>True upon completion of the task execution.</returns> |
| 21 | public override bool Execute() |
| 22 | { |
| 23 | var payloadGroupRefs = new ArrayList(); |
| 24 | var packageGroupRefs = new ArrayList(); |
| 25 | for (var i = 0; i < this.ProjectReferencePaths.Length; i++) |
| 26 | { |
| 27 | var item = this.ProjectReferencePaths[i]; |
| 28 | |
| 29 | if (!String.IsNullOrEmpty(item.GetMetadata(DoNotHarvest))) |
| 30 | { |
| 31 | continue; |
| 32 | } |
| 33 | |
| 34 | var projectPath = item.GetMetadata("MSBuildSourceProjectFile"); |
| 35 | var projectName = Path.GetFileNameWithoutExtension(projectPath); |
| 36 | var referenceName = GetIdentifierFromName(GetMetadataOrDefault(item, "Name", projectName)); |
| 37 | |
| 38 | var pogs = item.GetMetadata("RefProjectOutputGroups").Split(';'); |
| 39 | foreach (var pog in pogs) |
| 40 | { |
| 41 | if (!String.IsNullOrEmpty(pog)) |
| 42 | { |
| 43 | // TODO: Add payload group references and package group references once heat is generating them |
| 44 | ////payloadGroupRefs.Add(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", referenceName, pog)); |
| 45 | packageGroupRefs.Add(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", referenceName, pog)); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | var doc = new XmlDocument(); |
| 51 | |
| 52 | var head = doc.CreateProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); |
| 53 | doc.AppendChild(head); |
| 54 | |
| 55 | var rootElement = doc.CreateElement("Wix"); |
| 56 | rootElement.SetAttribute("xmlns", "http://wixtoolset.org/schemas/v4/wxs"); |
| 57 | doc.AppendChild(rootElement); |
| 58 | |
| 59 | var fragment = doc.CreateElement("Fragment"); |
| 60 | rootElement.AppendChild(fragment); |
| 61 | |
| 62 | var payloadGroup = doc.CreateElement("PayloadGroup"); |
| 63 | payloadGroup.SetAttribute("Id", "Bundle.Generated.Payloads"); |
| 64 | fragment.AppendChild(payloadGroup); |
| 65 | |
| 66 | var packageGroup = doc.CreateElement("PackageGroup"); |
| 67 | packageGroup.SetAttribute("Id", "Bundle.Generated.Packages"); |
| 68 | fragment.AppendChild(packageGroup); |
| 69 | |
| 70 | foreach (string payloadGroupRef in payloadGroupRefs) |
| 71 | { |
| 72 | var payloadGroupRefElement = doc.CreateElement("PayloadGroupRef"); |
| 73 | payloadGroupRefElement.SetAttribute("Id", payloadGroupRef); |
| 74 | payloadGroup.AppendChild(payloadGroupRefElement); |
| 75 | } |
| 76 | |
| 77 | foreach (string packageGroupRef in packageGroupRefs) |
| 78 | { |
| 79 | var packageGroupRefElement = doc.CreateElement("PackageGroupRef"); |
| 80 | packageGroupRefElement.SetAttribute("Id", packageGroupRef); |
| 81 | packageGroup.AppendChild(packageGroupRefElement); |
| 82 | } |
| 83 | |
| 84 | foreach (var item in this.GeneratedFiles) |
| 85 | { |
| 86 | var fullPath = item.GetMetadata("FullPath"); |
| 87 | |
| 88 | payloadGroup.SetAttribute("Id", Path.GetFileNameWithoutExtension(fullPath) + ".Payloads"); |
| 89 | packageGroup.SetAttribute("Id", Path.GetFileNameWithoutExtension(fullPath) + ".Packages"); |
| 90 | try |
| 91 | { |
| 92 | doc.Save(fullPath); |
| 93 | } |
| 94 | catch (Exception e) |
| 95 | { |
| 96 | // e.Message will be something like: "Access to the path 'fullPath' is denied." |
| 97 | this.Log.LogMessage(MessageImportance.High, "Unable to save generated file to '{0}'. {1}", fullPath, e.Message); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | } |