aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/RefreshGeneratedFile.cs
blob: 101b5363415d806423d97fefffc788ec67b27350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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.Xml;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    using WixToolset.Tools.Core;

    /// <summary>
    /// This task refreshes the generated file that contains ComponentGroupRefs
    /// to harvested output.
    /// </summary>
    public class RefreshGeneratedFile : Task
    {
        private ITaskItem[] generatedFiles;
        private ITaskItem[] projectReferencePaths;

        /// <summary>
        /// The list of files to generate.
        /// </summary>
        [Required]
        public ITaskItem[] GeneratedFiles
        {
            get { return this.generatedFiles; }
            set { this.generatedFiles = value; }
        }

        /// <summary>
        /// All the project references in the project.
        /// </summary>
        [Required]
        public ITaskItem[] ProjectReferencePaths
        {
            get { return this.projectReferencePaths; }
            set { this.projectReferencePaths = value; }
        }

        /// <summary>
        /// Gets a complete list of external cabs referenced by the given installer database file.
        /// </summary>
        /// <returns>True upon completion of the task execution.</returns>
        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(ToolsCommon.DoNotHarvest)))
                {
                    continue;
                }

                string projectPath = CreateProjectReferenceDefineConstants.GetProjectPath(this.ProjectReferencePaths, i);
                string projectName = Path.GetFileNameWithoutExtension(projectPath);
                string referenceName = ToolsCommon.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;
        }
    }
}