aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Compile/CompilerPayload.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Core/Compile/CompilerPayload.cs')
-rw-r--r--src/WixToolset.Core/Compile/CompilerPayload.cs247
1 files changed, 247 insertions, 0 deletions
diff --git a/src/WixToolset.Core/Compile/CompilerPayload.cs b/src/WixToolset.Core/Compile/CompilerPayload.cs
new file mode 100644
index 00000000..4eda56f8
--- /dev/null
+++ b/src/WixToolset.Core/Compile/CompilerPayload.cs
@@ -0,0 +1,247 @@
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
3namespace 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 DisplayName { get; set; }
19
20 public string DownloadUrl { get; set; }
21
22 public string Hash { get; set; }
23
24 public Identifier Id { 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(CompilerPayload remotePayload = null)
52 {
53 if (String.IsNullOrEmpty(this.SourceFile))
54 {
55 if (String.IsNullOrEmpty(this.Name))
56 {
57 if (this.IsRequired)
58 {
59 this.Core.Write(ErrorMessages.ExpectedAttributesWithOtherAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", "SourceFile"));
60 }
61 }
62 else if (remotePayload == null)
63 {
64 this.SourceFile = Path.Combine("SourceDir", this.Name);
65 }
66 }
67 else if (remotePayload != null)
68 {
69 this.Core.Write(ErrorMessages.UnexpectedElementWithAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "RemotePayload", "SourceFile"));
70 }
71 else if (this.SourceFile.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
72 {
73 if (String.IsNullOrEmpty(this.Name))
74 {
75 this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", "SourceFile", this.SourceFile));
76 }
77 else
78 {
79 this.SourceFile = Path.Combine(this.SourceFile, Path.GetFileName(this.Name));
80 }
81 }
82
83 if (remotePayload != null)
84 {
85 if (this.DownloadUrl == null)
86 {
87 this.Core.Write(ErrorMessages.ExpectedAttributeWithElement(this.SourceLineNumbers, this.Element.Name.LocalName, "DownloadUrl", "RemotePayload"));
88 }
89
90 if (YesNoDefaultType.No != this.Compressed)
91 {
92 this.Compressed = YesNoDefaultType.No;
93 this.Core.Write(WarningMessages.RemotePayloadsMustNotAlsoBeCompressed(this.SourceLineNumbers, this.Element.Name.LocalName));
94 }
95
96 this.Description = remotePayload.Description;
97 this.DisplayName = remotePayload.DisplayName;
98 this.Hash = remotePayload.Hash;
99 this.Size = remotePayload.Size;
100 this.Version = remotePayload.Version;
101 }
102 }
103
104 public WixBundlePayloadSymbol CreatePayloadSymbol(ComplexReferenceParentType parentType, string parentId, ComplexReferenceChildType previousType = ComplexReferenceChildType.Unknown, string previousId = null)
105 {
106 WixBundlePayloadSymbol symbol = null;
107
108 if (parentType == ComplexReferenceParentType.Container && parentId == BurnConstants.BurnUXContainerName)
109 {
110 if (this.Compressed == YesNoDefaultType.No)
111 {
112 this.Core.Write(WarningMessages.UxPayloadsOnlySupportEmbedding(this.SourceLineNumbers, this.SourceFile));
113 }
114
115 if (!String.IsNullOrEmpty(this.DownloadUrl))
116 {
117 this.Core.Write(WarningMessages.DownloadUrlNotSupportedForEmbeddedPayloads(this.SourceLineNumbers, this.Id.Id));
118 }
119
120 this.Compressed = YesNoDefaultType.Yes;
121 this.DownloadUrl = null;
122 }
123
124 if (!this.Core.EncounteredError)
125 {
126 symbol = this.Core.AddSymbol(new WixBundlePayloadSymbol(this.SourceLineNumbers, this.Id)
127 {
128 Name = String.IsNullOrEmpty(this.Name) ? Path.GetFileName(this.SourceFile) : this.Name,
129 SourceFile = new IntermediateFieldPathValue { Path = this.SourceFile },
130 DownloadUrl = this.DownloadUrl,
131 Compressed = (this.Compressed == YesNoDefaultType.Yes) ? true : (this.Compressed == YesNoDefaultType.No) ? (bool?)false : null,
132 UnresolvedSourceFile = this.SourceFile, // duplicate of sourceFile but in a string column so it won't get resolved to a full path during binding.
133 DisplayName = this.DisplayName ?? this.ProductName,
134 Description = this.Description,
135 Hash = this.Hash,
136 FileSize = this.Size,
137 Version = this.Version,
138 });
139
140 this.Core.CreateGroupAndOrderingRows(this.SourceLineNumbers, parentType, parentId, ComplexReferenceChildType.Payload, symbol.Id.Id, previousType, previousId);
141 }
142
143 return symbol;
144 }
145
146 public void FinishCompilingPackage(CompilerPayload remotePayload)
147 {
148 this.CalculateAndVerifyFields(remotePayload);
149 this.GenerateIdFromFilename();
150
151 if (this.Id == null)
152 {
153 this.Core.Write(ErrorMessages.ExpectedAttribute(this.SourceLineNumbers, this.Element.Name.LocalName, "Id"));
154 this.Id = Identifier.Invalid;
155 }
156 }
157
158 public void FinishCompilingPayload()
159 {
160 this.CalculateAndVerifyFields();
161 this.GenerateIdFromPrefix("pay");
162 }
163
164 private void GenerateIdFromFilename()
165 {
166 if (this.Id == null)
167 {
168 if (!String.IsNullOrEmpty(this.Name))
169 {
170 this.Id = this.Core.CreateIdentifierFromFilename(Path.GetFileName(this.Name));
171 }
172 else if (!String.IsNullOrEmpty(this.SourceFile))
173 {
174 this.Id = this.Core.CreateIdentifierFromFilename(Path.GetFileName(this.SourceFile));
175 }
176 }
177 }
178
179 private void GenerateIdFromPrefix(string prefix)
180 {
181 if (this.Id == null)
182 {
183 this.Id = this.Core.CreateIdentifier(prefix, this.SourceFile?.ToUpperInvariant() ?? String.Empty);
184 }
185 }
186
187 public void ParseCompressed(XAttribute attrib)
188 {
189 this.Compressed = this.Core.GetAttributeYesNoDefaultValue(this.SourceLineNumbers, attrib);
190 }
191
192 public void ParseDescription(XAttribute attrib)
193 {
194 this.Description = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
195 }
196
197 public void ParseDisplayName(XAttribute attrib)
198 {
199 this.DisplayName = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
200 }
201
202 public void ParseDownloadUrl(XAttribute attrib)
203 {
204 this.DownloadUrl = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
205 }
206
207 public void ParseHash(XAttribute attrib)
208 {
209 this.Hash = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
210 }
211
212 public void ParseId(XAttribute attrib)
213 {
214 this.Id = this.Core.GetAttributeIdentifier(this.SourceLineNumbers, attrib);
215 }
216
217 public void ParseName(XAttribute attrib)
218 {
219 this.Name = this.Core.GetAttributeLongFilename(this.SourceLineNumbers, attrib, false, true);
220 if (!this.Core.IsValidLongFilename(this.Name, false, true))
221 {
222 this.Core.Write(ErrorMessages.IllegalLongFilename(this.SourceLineNumbers, this.Element.Name.LocalName, "Name", this.Name));
223 }
224 }
225
226 public void ParseProductName(XAttribute attrib)
227 {
228 this.ProductName = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
229 }
230
231 public void ParseSize(XAttribute attrib)
232 {
233 this.Size = this.Core.GetAttributeLongValue(this.SourceLineNumbers, attrib, 1, Int64.MaxValue);
234 }
235
236 public void ParseSourceFile(XAttribute attrib)
237 {
238 this.SourceFile = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
239 }
240
241 public void ParseVersion(XAttribute attrib)
242 {
243 this.Version = this.Core.GetAttributeValue(this.SourceLineNumbers, attrib);
244 }
245
246 }
247}