aboutsummaryrefslogtreecommitdiff
path: root/src/tools/heat/VSHeatExtension.cs
blob: d31cd25a51529cc23a0f2c44a385c215fa9c8708 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// 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.Harvesters
{
    using System;
    using System.Collections;
    using WixToolset.Data;
    using WixToolset.Harvesters.Data;
    using WixToolset.Harvesters.Extensibility;

    /// <summary>
    /// Defines generated element types.
    /// </summary>
    public enum GenerateType
    {
        /// <summary>Generate Components.</summary>
        Components,

        /// <summary>Generate a Container with Payloads.</summary>
        Container,

        /// <summary>Generate a Bundle PackageGroups.</summary>
        PackageGroup,

        /// <summary>Generate a PayloadGroup with Payloads.</summary>
        PayloadGroup,
    }

    /// <summary>
    /// VS-related extensions for the WiX Toolset Harvester application.
    /// </summary>
    public sealed class VSHeatExtension : BaseHeatExtension
    {
        /// <summary>
        /// Gets the supported command line types for this extension.
        /// </summary>
        /// <value>The supported command line types for this extension.</value>
        public override HeatCommandLineOption[] CommandLineTypes
        {
            get
            {
                return new HeatCommandLineOption[]
                {
                    new HeatCommandLineOption("project", "harvest outputs of a VS project"),
                    new HeatCommandLineOption("-configuration", "configuration to set when harvesting the project"),
                    new HeatCommandLineOption("-directoryid", "overridden directory id for generated directory elements"),
                    new HeatCommandLineOption("-generate", Environment.NewLine +
                        "            specify what elements to generate, one of:" + Environment.NewLine +
                        "                components, container, payloadgroup, packagegroup" + Environment.NewLine +
                        "                (default is components)"),
                    new HeatCommandLineOption("-msbuildbinpath", "msbuild bin directory path"),
                    new HeatCommandLineOption("-platform", "platform to set when harvesting the project"),
                    new HeatCommandLineOption("-pog", Environment.NewLine +
                        "            specify output group of VS project, one of:" + Environment.NewLine +
                        "                " + String.Join(",", VSProjectHarvester.GetOutputGroupNames()) + Environment.NewLine +
                        "              This option may be repeated for multiple output groups."),
                    new HeatCommandLineOption("-projectname", "overridden project name to use in variables"),
                    new HeatCommandLineOption("-usetoolsversion", "ignore msbuildbinpath if project specifies known msbuild version"),
                    new HeatCommandLineOption("-wixvar", "generate binder variables instead of preprocessor variables"),
                };
            }
        }

        /// <summary>
        /// Parse the command line options for this extension.
        /// </summary>
        /// <param name="type">The active harvester type.</param>
        /// <param name="args">The option arguments.</param>
        public override void ParseOptions(string type, string[] args)
        {
            if ("project" == type)
            {
                string[] allOutputGroups = VSProjectHarvester.GetOutputGroupNames();
                bool suppressUniqueId = false;
                bool generateWixVars = false;
                bool useToolsVersion = false;
                GenerateType generateType = GenerateType.Components;
                string directoryIds = null;
                string msbuildBinPath = null;
                string projectName = null;
                string configuration = null;
                string platform = null;
                ArrayList outputGroups = new ArrayList();

                for (int i = 0; i < args.Length; i++)
                {
                    if ("-configuration" == args[i])
                    {
                        configuration = args[++i];
                    }
                    else if ("-directoryid" == args[i])
                    {
                        if (!IsValidArg(args, ++i))
                        {
                            throw new WixException(HarvesterErrors.InvalidDirectoryId(args[i]));
                        }

                        directoryIds = args[i];
                    }
                    else if ("-generate" == args[i])
                    {
                        if (!IsValidArg(args, ++i))
                        {
                            throw new WixException(HarvesterErrors.InvalidProjectOutputType(args[i]));
                        }

                        string genType = args[i].ToUpperInvariant();
                        switch(genType)
                        {
                            case "CONTAINER":
                                generateType = GenerateType.Container;
                                break;
                            case "COMPONENTS":
                                generateType = GenerateType.Components;
                                break;
                            case "PACKAGEGROUP":
                                generateType = GenerateType.PackageGroup;
                                break;
                            case "PAYLOADGROUP":
                                generateType = GenerateType.PayloadGroup;
                                break;
                            default:
                                throw new WixException(HarvesterErrors.InvalidProjectOutputType(genType));
                        }
                    }
                    else if ("-msbuildbinpath" == args[i])
                    {
                        if (!IsValidArg(args, ++i))
                        {
                            throw new WixException(HarvesterErrors.ArgumentRequiresValue(args[i-1]));
                        }

                        msbuildBinPath = args[i];
                    }
                    else if ("-platform" == args[i])
                    {
                        platform = args[++i];
                    }
                    else if ("-pog" == args[i])
                    {
                        if (!IsValidArg(args, ++i))
                        {
                            throw new WixException(HarvesterErrors.InvalidOutputGroup(args[i]));
                        }

                        string pogName = args[i];
                        bool found = false;
                        foreach (string availableOutputGroup in allOutputGroups)
                        {
                            if (String.Equals(pogName, availableOutputGroup, StringComparison.Ordinal))
                            {
                                outputGroups.Add(availableOutputGroup);
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            throw new WixException(HarvesterErrors.InvalidOutputGroup(pogName));
                        }
                    }
                    else if (args[i].StartsWith("-pog:", StringComparison.Ordinal))
                    {
                        this.Core.Messaging.Write(WarningMessages.DeprecatedCommandLineSwitch("pog:", "pog"));

                        string pogName = args[i].Substring(5);
                        bool found = false;
                        foreach (string availableOutputGroup in allOutputGroups)
                        {
                            if (String.Equals(pogName, availableOutputGroup, StringComparison.Ordinal))
                            {
                                outputGroups.Add(availableOutputGroup);
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            throw new WixException(HarvesterErrors.InvalidOutputGroup(pogName));
                        }
                    }
                    else if ("-projectname" == args[i])
                    {
                        if (!IsValidArg(args, ++i))
                        {
                            throw new WixException(HarvesterErrors.InvalidProjectName(args[i]));
                        }

                        projectName = args[i];
                    }
                    else if ("-suid" == args[i])
                    {
                        suppressUniqueId = true;
                    }
                    else if ("-usetoolsversion" == args[i])
                    {
                        useToolsVersion = true;
                    }
                    else if ("-wixvar" == args[i])
                    {
                        generateWixVars = true;
                    }
                }

                if (outputGroups.Count == 0)
                {
                    throw new WixException(HarvesterErrors.NoOutputGroupSpecified());
                }

                VSProjectHarvester harvester = new VSProjectHarvester(
                    (string[]) outputGroups.ToArray(typeof(string)));

                harvester.SetUniqueIdentifiers = !suppressUniqueId;
                harvester.GenerateWixVars = generateWixVars;
                harvester.GenerateType = generateType;
                harvester.DirectoryIds = directoryIds;
                harvester.MsbuildBinPath = msbuildBinPath;
                harvester.ProjectName = projectName;
                harvester.Configuration = configuration;
                harvester.Platform = platform;
                harvester.UseToolsVersion = String.IsNullOrEmpty(msbuildBinPath) || useToolsVersion;

                this.Core.Harvester.Extension = harvester;
            }
        }
    }
}