| 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 that contains ComponentGroupRefs |
| 14 | /// to harvested output. |
| 15 | /// </summary> |
| 16 | public class RefreshGeneratedFile : RefreshTask |
| 17 | { |
| 18 | /// <summary> |
| 19 | /// Gets a complete list of external cabs referenced by the given installer database file. |
| 20 | /// </summary> |
| 21 | /// <returns>True upon completion of the task execution.</returns> |
| 22 | public override bool Execute() |
| 23 | { |
| 24 | var componentGroupRefs = new ArrayList(); |
| 25 | |
| 26 | for (var i = 0; i < this.ProjectReferencePaths.Length; i++) |
| 27 | { |
| 28 | var item = this.ProjectReferencePaths[i]; |
| 29 | |
| 30 | if (!String.IsNullOrEmpty(item.GetMetadata(DoNotHarvest))) |
| 31 | { |
| 32 | continue; |
| 33 | } |
| 34 | |
| 35 | var projectPath = item.GetMetadata("MSBuildSourceProjectFile"); |
| 36 | var projectName = Path.GetFileNameWithoutExtension(projectPath); |
| 37 | var referenceName = GetIdentifierFromName(GetMetadataOrDefault(item, "Name", projectName)); |
| 38 | |
| 39 | var pogs = item.GetMetadata("RefProjectOutputGroups").Split(';'); |
| 40 | foreach (var pog in pogs) |
| 41 | { |
| 42 | if (!String.IsNullOrEmpty(pog)) |
| 43 | { |
| 44 | componentGroupRefs.Add(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", referenceName, pog)); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | var doc = new XmlDocument(); |
| 50 | |
| 51 | var head = doc.CreateProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); |
| 52 | doc.AppendChild(head); |
| 53 | |
| 54 | var rootElement = doc.CreateElement("Wix"); |
| 55 | rootElement.SetAttribute("xmlns", "http://wixtoolset.org/schemas/v4/wxs"); |
| 56 | doc.AppendChild(rootElement); |
| 57 | |
| 58 | var fragment = doc.CreateElement("Fragment"); |
| 59 | rootElement.AppendChild(fragment); |
| 60 | |
| 61 | var componentGroup = doc.CreateElement("ComponentGroup"); |
| 62 | componentGroup.SetAttribute("Id", "Product.Generated"); |
| 63 | fragment.AppendChild(componentGroup); |
| 64 | |
| 65 | foreach (string componentGroupRef in componentGroupRefs) |
| 66 | { |
| 67 | var componentGroupRefElement = doc.CreateElement("ComponentGroupRef"); |
| 68 | componentGroupRefElement.SetAttribute("Id", componentGroupRef); |
| 69 | componentGroup.AppendChild(componentGroupRefElement); |
| 70 | } |
| 71 | |
| 72 | foreach (var item in this.GeneratedFiles) |
| 73 | { |
| 74 | var fullPath = item.GetMetadata("FullPath"); |
| 75 | |
| 76 | componentGroup.SetAttribute("Id", Path.GetFileNameWithoutExtension(fullPath)); |
| 77 | try |
| 78 | { |
| 79 | doc.Save(fullPath); |
| 80 | } |
| 81 | catch (Exception e) |
| 82 | { |
| 83 | // e.Message will be something like: "Access to the path 'fullPath' is denied." |
| 84 | this.Log.LogMessage(MessageImportance.High, "Unable to save generated file to '{0}'. {1}", fullPath, e.Message); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | } |