diff options
Diffstat (limited to 'src/wix/WixToolset.Core/Compile/CompilerPayload.cs')
-rw-r--r-- | src/wix/WixToolset.Core/Compile/CompilerPayload.cs | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/src/wix/WixToolset.Core/Compile/CompilerPayload.cs b/src/wix/WixToolset.Core/Compile/CompilerPayload.cs new file mode 100644 index 00000000..3f423034 --- /dev/null +++ b/src/wix/WixToolset.Core/Compile/CompilerPayload.cs | |||
@@ -0,0 +1,291 @@ | |||
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.Core | ||
4 | { | ||
5 | using System; | ||
6 | using System.IO; | ||
7 | using System.Xml.Linq; | ||
8 | using WixToolset.Data; | ||
9 | using WixToolset.Data.Burn; | ||
10 | using WixToolset.Data.Symbols; | ||
11 | |||
12 | internal class CompilerPayload | ||
13 | { | ||
14 | public YesNoDefaultType Compressed { get; set; } = YesNoDefaultType.Default; | ||
15 | |||
16 | public string Description { get; set; } | ||
17 | |||
18 | public string DownloadUrl { get; set; } | ||
19 | |||
20 | public string Hash { get; set; } | ||
21 | |||
22 | public Identifier Id { get; set; } | ||
23 | |||
24 | public bool IsRemoteAllowed { get; set; } | ||
25 | |||
26 | public bool IsRequired { get; set; } = true; | ||
27 | |||
28 | public string Name { get; set; } | ||
29 | |||
30 | public string ProductName { get; set; } | ||
31 | |||
32 | public long? Size { get; set; } | ||
33 | |||
34 | public string SourceFile { get; set; } | ||
35 | |||
36 | public string Version { get; set; } | ||
37 | |||
38 | public CompilerPayload(CompilerCore core, SourceLineNumber sourceLineNumbers, XElement element) | ||
39 | { | ||
40 | this.Core = core; | ||
41 | this.Element = element; | ||
42 | this.SourceLineNumbers = sourceLineNumbers; | ||
43 | } | ||
44 | |||
45 | private CompilerCore Core { get; } | ||
46 | |||
47 | private XElement Element { get; } | ||
48 | |||
49 | private SourceLineNumber SourceLineNumbers { get; } | ||
50 | |||
51 | private void CalculateAndVerifyFields() | ||
52 | { | ||
53 | var isRemote = this.IsRemoteAllowed && !String.IsNullOrEmpty(this.Hash); | ||
54 | |||
55 | if (String.IsNullOrEmpty(this.SourceFile)) | ||
56 | { | ||
57 | if (!String.IsNullOrEmpty(this.Name) && !isRemote) | ||
58 | { | ||
59 | this.SourceFile = Path.Combine("SourceDir", this.Name); | ||
60 | } | ||
61 | } | ||
62 | else if (this.SourceFile.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) | ||
63 | { | ||
64 | if (String.IsNullOrEmpty(this.Name)) | ||
65 | { | ||
66 | this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", "SourceFile", this.SourceFile)); | ||
67 | } | ||
68 | else | ||
69 | { | ||
70 | this.SourceFile = Path.Combine(this.SourceFile, Path.GetFileName(this.Name)); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | if (String.IsNullOrEmpty(this.SourceFile) && !isRemote) | ||
75 | { | ||
76 | if (this.IsRequired) | ||
77 | { | ||
78 | if (!this.IsRemoteAllowed) | ||
79 | { | ||
80 | this.Core.Write(ErrorMessages.ExpectedAttributes(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", "SourceFile")); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | this.Core.Write(ErrorMessages.ExpectedAttributes(this.SourceLineNumbers, this.Element.Name.LocalName, "SourceFile", "Hash")); | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | else if (this.IsRemoteAllowed) | ||
89 | { | ||
90 | var isLocal = !String.IsNullOrEmpty(this.SourceFile); | ||
91 | |||
92 | if (isLocal) | ||
93 | { | ||
94 | if (isRemote) | ||
95 | { | ||
96 | this.Core.Write(ErrorMessages.IllegalAttributeWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Hash", "SourceFile")); | ||
97 | } | ||
98 | |||
99 | if (!String.IsNullOrEmpty(this.Description)) | ||
100 | { | ||
101 | this.Core.Write(ErrorMessages.IllegalAttributeWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Description", "SourceFile")); | ||
102 | } | ||
103 | |||
104 | if (!String.IsNullOrEmpty(this.ProductName)) | ||
105 | { | ||
106 | this.Core.Write(ErrorMessages.IllegalAttributeWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "ProductName", "SourceFile")); | ||
107 | } | ||
108 | |||
109 | if (this.Size.HasValue) | ||
110 | { | ||
111 | this.Core.Write(ErrorMessages.IllegalAttributeWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Size", "SourceFile")); | ||
112 | } | ||
113 | |||
114 | if (!String.IsNullOrEmpty(this.Version)) | ||
115 | { | ||
116 | this.Core.Write(ErrorMessages.IllegalAttributeWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Version", "SourceFile")); | ||
117 | } | ||
118 | } | ||
119 | else | ||
120 | { | ||
121 | if (String.IsNullOrEmpty(this.DownloadUrl)) | ||
122 | { | ||
123 | this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "DownloadUrl", "Hash")); | ||
124 | } | ||
125 | |||
126 | if (String.IsNullOrEmpty(this.Name)) | ||
127 | { | ||
128 | this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", "Hash")); | ||
129 | } | ||
130 | |||
131 | if (!this.Size.HasValue) | ||
132 | { | ||
133 | this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Size", "Hash")); | ||
134 | } | ||
135 | |||
136 | if (YesNoDefaultType.Yes == this.Compressed) | ||
137 | { | ||
138 | this.Core.Write(WarningMessages.RemotePayloadsMustNotAlsoBeCompressed(this.SourceLineNumbers, this.Element.Name.LocalName)); | ||
139 | } | ||
140 | |||
141 | this.Compressed = YesNoDefaultType.No; | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | |||
146 | public WixBundlePayloadSymbol CreatePayloadSymbol(ComplexReferenceParentType parentType, string parentId, ComplexReferenceChildType previousType = ComplexReferenceChildType.Unknown, string previousId = null) | ||
147 | { | ||
148 | WixBundlePayloadSymbol symbol = null; | ||
149 | |||
150 | if (parentType == ComplexReferenceParentType.Container && parentId == BurnConstants.BurnUXContainerName) | ||
151 | { | ||
152 | if (this.Compressed == YesNoDefaultType.No) | ||
153 | { | ||
154 | this.Core.Write(WarningMessages.UxPayloadsOnlySupportEmbedding(this.SourceLineNumbers, this.SourceFile)); | ||
155 | } | ||
156 | |||
157 | if (!String.IsNullOrEmpty(this.DownloadUrl)) | ||
158 | { | ||
159 | this.Core.Write(WarningMessages.DownloadUrlNotSupportedForBAPayloads(this.SourceLineNumbers, this.Id.Id)); | ||
160 | } | ||
161 | |||
162 | this.Compressed = YesNoDefaultType.Yes; | ||
163 | this.DownloadUrl = null; | ||
164 | } | ||
165 | |||
166 | if (!this.Core.EncounteredError) | ||
167 | { | ||
168 | symbol = this.Core.AddSymbol(new WixBundlePayloadSymbol(this.SourceLineNumbers, this.Id) | ||
169 | { | ||
170 | Name = String.IsNullOrEmpty(this.Name) ? Path.GetFileName(this.SourceFile) : this.Name, | ||
171 | SourceFile = new IntermediateFieldPathValue { Path = this.SourceFile }, | ||
172 | DownloadUrl = this.DownloadUrl, | ||
173 | Compressed = (this.Compressed == YesNoDefaultType.Yes) ? true : (this.Compressed == YesNoDefaultType.No) ? (bool?)false : null, | ||
174 | UnresolvedSourceFile = this.SourceFile, // duplicate of sourceFile but in a string column so it won't get resolved to a full path during binding. | ||
175 | DisplayName = this.ProductName, | ||
176 | Description = this.Description, | ||
177 | Hash = this.Hash, | ||
178 | FileSize = this.Size, | ||
179 | Version = this.Version, | ||
180 | }); | ||
181 | |||
182 | this.Core.CreateGroupAndOrderingRows(this.SourceLineNumbers, parentType, parentId, ComplexReferenceChildType.Payload, symbol.Id.Id, previousType, previousId); | ||
183 | } | ||
184 | |||
185 | return symbol; | ||
186 | } | ||
187 | |||
188 | public void FinishCompilingPackage() | ||
189 | { | ||
190 | this.CalculateAndVerifyFields(); | ||
191 | this.GenerateIdFromFilename(); | ||
192 | |||
193 | if (this.Id == null) | ||
194 | { | ||
195 | this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Id")); | ||
196 | this.Id = Identifier.Invalid; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | public void FinishCompilingPackagePayload() | ||
201 | { | ||
202 | this.CalculateAndVerifyFields(); | ||
203 | this.GenerateIdFromFilename(); | ||
204 | this.GenerateIdFromPrefix("ppy"); | ||
205 | } | ||
206 | |||
207 | public void FinishCompilingPayload() | ||
208 | { | ||
209 | this.CalculateAndVerifyFields(); | ||
210 | this.GenerateIdFromPrefix("pay"); | ||
211 | } | ||
212 | |||
213 | private void GenerateIdFromFilename() | ||
214 | { | ||
215 | if (this.Id == null) | ||
216 | { | ||
217 | if (!String.IsNullOrEmpty(this.Name)) | ||
218 | { | ||
219 | this.Id = this.Core.CreateIdentifierFromFilename(Path.GetFileName(this.Name)); | ||
220 | } | ||
221 | else if (!String.IsNullOrEmpty(this.SourceFile)) | ||
222 | { | ||
223 | this.Id = this.Core.CreateIdentifierFromFilename(Path.GetFileName(this.SourceFile)); | ||
224 | } | ||
225 | } | ||
226 | } | ||
227 | |||
228 | private void GenerateIdFromPrefix(string prefix) | ||
229 | { | ||
230 | if (this.Id == null) | ||
231 | { | ||
232 | this.Id = this.Core.CreateIdentifier(prefix, this.SourceFile?.ToUpperInvariant() ?? String.Empty); | ||
233 | } | ||
234 | } | ||
235 | |||
236 | public void ParseCompressed(XAttribute attrib) | ||
237 | { | ||
238 | this.Compressed = this.Core.GetAttributeYesNoDefaultValue(this.SourceLineNumbers, attrib); | ||
239 | } | ||
240 | |||
241 | public void ParseDescription(XAttribute attrib) | ||
242 | { | ||
243 | this.Description = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
244 | } | ||
245 | |||
246 | public void ParseDownloadUrl(XAttribute attrib) | ||
247 | { | ||
248 | this.DownloadUrl = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
249 | } | ||
250 | |||
251 | public void ParseHash(XAttribute attrib) | ||
252 | { | ||
253 | this.Hash = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
254 | } | ||
255 | |||
256 | public void ParseId(XAttribute attrib) | ||
257 | { | ||
258 | this.Id = this.Core.GetAttributeIdentifier(this.SourceLineNumbers, attrib); | ||
259 | } | ||
260 | |||
261 | public void ParseName(XAttribute attrib) | ||
262 | { | ||
263 | this.Name = this.Core.GetAttributeLongFilename(this.SourceLineNumbers, attrib, false, true); | ||
264 | if (!this.Core.IsValidLongFilename(this.Name, false, true)) | ||
265 | { | ||
266 | this.Core.Write(ErrorMessages.IllegalLongFilename(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", this.Name)); | ||
267 | } | ||
268 | } | ||
269 | |||
270 | public void ParseProductName(XAttribute attrib) | ||
271 | { | ||
272 | this.ProductName = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
273 | } | ||
274 | |||
275 | public void ParseSize(XAttribute attrib) | ||
276 | { | ||
277 | this.Size = this.Core.GetAttributeLongValue(this.SourceLineNumbers, attrib, 1, Int64.MaxValue); | ||
278 | } | ||
279 | |||
280 | public void ParseSourceFile(XAttribute attrib) | ||
281 | { | ||
282 | this.SourceFile = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
283 | } | ||
284 | |||
285 | public void ParseVersion(XAttribute attrib) | ||
286 | { | ||
287 | this.Version = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib); | ||
288 | } | ||
289 | |||
290 | } | ||
291 | } | ||