From 306f1d0c528cb6c151594ff96a41b5c01a5c4d9b Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 21 Jul 2018 07:36:34 -0700 Subject: Integrate tools from Core project --- src/WixToolset.BuildTasks/RefreshGeneratedFile.cs | 118 ++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/WixToolset.BuildTasks/RefreshGeneratedFile.cs (limited to 'src/WixToolset.BuildTasks/RefreshGeneratedFile.cs') diff --git a/src/WixToolset.BuildTasks/RefreshGeneratedFile.cs b/src/WixToolset.BuildTasks/RefreshGeneratedFile.cs new file mode 100644 index 00000000..fdfc4774 --- /dev/null +++ b/src/WixToolset.BuildTasks/RefreshGeneratedFile.cs @@ -0,0 +1,118 @@ +// 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. + +namespace WixToolset.BuildTasks +{ + using System; + using System.Collections; + using System.Globalization; + using System.IO; + using System.Text.RegularExpressions; + using System.Xml; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + + /// + /// This task refreshes the generated file that contains ComponentGroupRefs + /// to harvested output. + /// + public class RefreshGeneratedFile : Task + { + private static readonly Regex AddPrefix = new Regex(@"^[^a-zA-Z_]", RegexOptions.Compiled); + private static readonly Regex IllegalIdentifierCharacters = new Regex(@"[^A-Za-z0-9_\.]|\.{2,}", RegexOptions.Compiled); // non 'words' and assorted valid characters + + private ITaskItem[] generatedFiles; + private ITaskItem[] projectReferencePaths; + + /// + /// The list of files to generate. + /// + [Required] + public ITaskItem[] GeneratedFiles + { + get { return this.generatedFiles; } + set { this.generatedFiles = value; } + } + + /// + /// All the project references in the project. + /// + [Required] + public ITaskItem[] ProjectReferencePaths + { + get { return this.projectReferencePaths; } + set { this.projectReferencePaths = value; } + } + + /// + /// Gets a complete list of external cabs referenced by the given installer database file. + /// + /// True upon completion of the task execution. + public override bool Execute() + { + ArrayList componentGroupRefs = new ArrayList(); + for (int i = 0; i < this.ProjectReferencePaths.Length; i++) + { + ITaskItem item = this.ProjectReferencePaths[i]; + + if (!String.IsNullOrEmpty(item.GetMetadata(Common.DoNotHarvest))) + { + continue; + } + + string projectPath = CreateProjectReferenceDefineConstants.GetProjectPath(this.ProjectReferencePaths, i); + string projectName = Path.GetFileNameWithoutExtension(projectPath); + string referenceName = Common.GetIdentifierFromName(CreateProjectReferenceDefineConstants.GetReferenceName(item, projectName)); + + string[] pogs = item.GetMetadata("RefProjectOutputGroups").Split(';'); + foreach (string pog in pogs) + { + if (!String.IsNullOrEmpty(pog)) + { + componentGroupRefs.Add(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", referenceName, pog)); + } + } + } + + XmlDocument doc = new XmlDocument(); + + XmlProcessingInstruction head = doc.CreateProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + doc.AppendChild(head); + + XmlElement rootElement = doc.CreateElement("Wix"); + rootElement.SetAttribute("xmlns", "http://wixtoolset.org/schemas/v4/wxs"); + doc.AppendChild(rootElement); + + XmlElement fragment = doc.CreateElement("Fragment"); + rootElement.AppendChild(fragment); + + XmlElement componentGroup = doc.CreateElement("ComponentGroup"); + componentGroup.SetAttribute("Id", "Product.Generated"); + fragment.AppendChild(componentGroup); + + foreach (string componentGroupRef in componentGroupRefs) + { + XmlElement componentGroupRefElement = doc.CreateElement("ComponentGroupRef"); + componentGroupRefElement.SetAttribute("Id", componentGroupRef); + componentGroup.AppendChild(componentGroupRefElement); + } + + foreach (ITaskItem item in this.GeneratedFiles) + { + string fullPath = item.GetMetadata("FullPath"); + + componentGroup.SetAttribute("Id", Path.GetFileNameWithoutExtension(fullPath)); + try + { + doc.Save(fullPath); + } + catch (Exception e) + { + // e.Message will be something like: "Access to the path 'fullPath' is denied." + this.Log.LogMessage(MessageImportance.High, "Unable to save generated file to '{0}'. {1}", fullPath, e.Message); + } + } + + return true; + } + } +} -- cgit v1.2.3-55-g6feb