diff options
Diffstat (limited to 'src/WixToolset.BuildTasks/WixCommandLineBuilder.cs')
-rw-r--r-- | src/WixToolset.BuildTasks/WixCommandLineBuilder.cs | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/src/WixToolset.BuildTasks/WixCommandLineBuilder.cs b/src/WixToolset.BuildTasks/WixCommandLineBuilder.cs new file mode 100644 index 00000000..9a6a005d --- /dev/null +++ b/src/WixToolset.BuildTasks/WixCommandLineBuilder.cs | |||
@@ -0,0 +1,180 @@ | |||
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.BuildTasks | ||
4 | { | ||
5 | using System; | ||
6 | using System.Diagnostics; | ||
7 | using System.Globalization; | ||
8 | using System.IO; | ||
9 | using System.Text; | ||
10 | |||
11 | using Microsoft.Build.Framework; | ||
12 | using Microsoft.Build.Utilities; | ||
13 | |||
14 | /// <summary> | ||
15 | /// Helper class for appending the command line arguments. | ||
16 | /// </summary> | ||
17 | public class WixCommandLineBuilder : CommandLineBuilder | ||
18 | { | ||
19 | internal const int Unspecified = -1; | ||
20 | |||
21 | /// <summary> | ||
22 | /// Append a switch to the command line if the value has been specified. | ||
23 | /// </summary> | ||
24 | /// <param name="switchName">Switch to append.</param> | ||
25 | /// <param name="value">Value specified by the user.</param> | ||
26 | public void AppendIfSpecified(string switchName, int value) | ||
27 | { | ||
28 | if (value != Unspecified) | ||
29 | { | ||
30 | this.AppendSwitchIfNotNull(switchName, value.ToString(CultureInfo.InvariantCulture)); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | /// <summary> | ||
35 | /// Append a switch to the command line if the condition is true. | ||
36 | /// </summary> | ||
37 | /// <param name="switchName">Switch to append.</param> | ||
38 | /// <param name="condition">Condition specified by the user.</param> | ||
39 | public void AppendIfTrue(string switchName, bool condition) | ||
40 | { | ||
41 | if (condition) | ||
42 | { | ||
43 | this.AppendSwitch(switchName); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | /// <summary> | ||
48 | /// Append a switch to the command line if any values in the array have been specified. | ||
49 | /// </summary> | ||
50 | /// <param name="switchName">Switch to append.</param> | ||
51 | /// <param name="values">Values specified by the user.</param> | ||
52 | public void AppendArrayIfNotNull(string switchName, ITaskItem[] values) | ||
53 | { | ||
54 | if (values != null) | ||
55 | { | ||
56 | foreach (ITaskItem value in values) | ||
57 | { | ||
58 | this.AppendSwitchIfNotNull(switchName, value); | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// Append a switch to the command line if any values in the array have been specified. | ||
65 | /// </summary> | ||
66 | /// <param name="switchName">Switch to append.</param> | ||
67 | /// <param name="values">Values specified by the user.</param> | ||
68 | public void AppendArrayIfNotNull(string switchName, string[] values) | ||
69 | { | ||
70 | if (values != null) | ||
71 | { | ||
72 | foreach (string value in values) | ||
73 | { | ||
74 | this.AppendSwitchIfNotNull(switchName, value); | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | /// <summary> | ||
80 | /// Build the extensions argument. Each extension is searched in the current folder, user defined search | ||
81 | /// directories (ReferencePath), HintPath, and under Wix Extension Directory in that order. | ||
82 | /// The order of precednce is based off of that described in Microsoft.Common.Targets's SearchPaths | ||
83 | /// property for the ResolveAssemblyReferences task. | ||
84 | /// </summary> | ||
85 | /// <param name="extensions">The list of extensions to include.</param> | ||
86 | /// <param name="wixExtensionDirectory">Evaluated default folder for Wix Extensions</param> | ||
87 | /// <param name="referencePaths">User defined reference directories to search in</param> | ||
88 | public void AppendExtensions(ITaskItem[] extensions, string wixExtensionDirectory, string [] referencePaths) | ||
89 | { | ||
90 | if (extensions == null) | ||
91 | { | ||
92 | return; | ||
93 | } | ||
94 | |||
95 | string resolvedPath; | ||
96 | |||
97 | foreach (ITaskItem extension in extensions) | ||
98 | { | ||
99 | string className = extension.GetMetadata("Class"); | ||
100 | |||
101 | string fileName = Path.GetFileName(extension.ItemSpec); | ||
102 | |||
103 | if (Path.GetExtension(fileName).Length == 0) | ||
104 | { | ||
105 | fileName += ".dll"; | ||
106 | } | ||
107 | |||
108 | // First try reference paths | ||
109 | resolvedPath = FileSearchHelperMethods.SearchFilePaths(referencePaths, fileName); | ||
110 | |||
111 | if (String.IsNullOrEmpty(resolvedPath)) | ||
112 | { | ||
113 | // Now try HintPath | ||
114 | resolvedPath = extension.GetMetadata("HintPath"); | ||
115 | |||
116 | if (!File.Exists(resolvedPath)) | ||
117 | { | ||
118 | // Now try the item itself | ||
119 | resolvedPath = extension.ItemSpec; | ||
120 | |||
121 | if (Path.GetExtension(resolvedPath).Length == 0) | ||
122 | { | ||
123 | resolvedPath += ".dll"; | ||
124 | } | ||
125 | |||
126 | if (!File.Exists(resolvedPath)) | ||
127 | { | ||
128 | if (!String.IsNullOrEmpty(wixExtensionDirectory)) | ||
129 | { | ||
130 | // Now try the extension directory | ||
131 | resolvedPath = Path.Combine(wixExtensionDirectory, Path.GetFileName(resolvedPath)); | ||
132 | } | ||
133 | |||
134 | if (!File.Exists(resolvedPath)) | ||
135 | { | ||
136 | // Extesnion wasn't found, just set it to the extension name passed in | ||
137 | resolvedPath = extension.ItemSpec; | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | |||
143 | if (String.IsNullOrEmpty(className)) | ||
144 | { | ||
145 | this.AppendSwitchIfNotNull("-ext ", resolvedPath); | ||
146 | } | ||
147 | else | ||
148 | { | ||
149 | this.AppendSwitchIfNotNull("-ext ", className + ", " + resolvedPath); | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | |||
154 | /// <summary> | ||
155 | /// Append arbitrary text to the command-line if specified. | ||
156 | /// </summary> | ||
157 | /// <param name="textToAppend">Text to append.</param> | ||
158 | public void AppendTextIfNotNull(string textToAppend) | ||
159 | { | ||
160 | if (!String.IsNullOrEmpty(textToAppend)) | ||
161 | { | ||
162 | this.AppendSpaceIfNotEmpty(); | ||
163 | this.AppendTextUnquoted(textToAppend); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// <summary> | ||
168 | /// Append arbitrary text to the command-line if specified. | ||
169 | /// </summary> | ||
170 | /// <param name="textToAppend">Text to append.</param> | ||
171 | public void AppendTextIfNotWhitespace(string textToAppend) | ||
172 | { | ||
173 | if (!String.IsNullOrWhiteSpace(textToAppend)) | ||
174 | { | ||
175 | this.AppendSpaceIfNotEmpty(); | ||
176 | this.AppendTextUnquoted(textToAppend); | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | } | ||