aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2021-03-01 23:13:11 -0800
committerRob Mensching <rob@firegiant.com>2021-03-02 01:54:57 -0800
commita96d1a916bfcf35f77d48f70215d62b298bfb933 (patch)
tree00c1625058424783a3ef4726c0d7b11bd46baba3 /src
parent26415c014397c463f2954e0bd9ff742110c5ea75 (diff)
downloadwix-a96d1a916bfcf35f77d48f70215d62b298bfb933.tar.gz
wix-a96d1a916bfcf35f77d48f70215d62b298bfb933.tar.bz2
wix-a96d1a916bfcf35f77d48f70215d62b298bfb933.zip
Small cleanup
Diffstat (limited to 'src')
-rw-r--r--src/WixToolset.Data/IntermediateFieldExtensions.cs48
-rw-r--r--src/WixToolset.Data/IntermediateSymbol.cs1
-rw-r--r--src/WixToolset.Data/Json/JsonObjectExtensions.cs6
-rw-r--r--src/WixToolset.Data/Library.cs243
-rw-r--r--src/WixToolset.Data/WarningMessages.cs2
5 files changed, 7 insertions, 293 deletions
diff --git a/src/WixToolset.Data/IntermediateFieldExtensions.cs b/src/WixToolset.Data/IntermediateFieldExtensions.cs
index 2c856e65..c70eb230 100644
--- a/src/WixToolset.Data/IntermediateFieldExtensions.cs
+++ b/src/WixToolset.Data/IntermediateFieldExtensions.cs
@@ -45,21 +45,7 @@ namespace WixToolset.Data
45 return null; 45 return null;
46 } 46 }
47 47
48 switch (field.Definition.Type) 48 return field.AsBool();
49 {
50 case IntermediateFieldType.Bool:
51 return field.Value.AsBool();
52
53 case IntermediateFieldType.LargeNumber:
54 case IntermediateFieldType.Number:
55 return field.Value.AsLargeNumber() != 0;
56
57 case IntermediateFieldType.String:
58 return !String.IsNullOrEmpty(field.Value.AsString());
59
60 default:
61 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to boolean");
62 }
63 } 49 }
64 50
65 public static long AsLargeNumber(this IntermediateField field) 51 public static long AsLargeNumber(this IntermediateField field)
@@ -93,21 +79,7 @@ namespace WixToolset.Data
93 return null; 79 return null;
94 } 80 }
95 81
96 switch (field.Definition.Type) 82 return field.AsLargeNumber();
97 {
98 case IntermediateFieldType.Bool:
99 return field.Value.AsBool() ? 1 : 0;
100
101 case IntermediateFieldType.LargeNumber:
102 case IntermediateFieldType.Number:
103 return field.Value.AsLargeNumber();
104
105 case IntermediateFieldType.String:
106 return Convert.ToInt64(field.Value.AsString());
107
108 default:
109 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to large number");
110 }
111 } 83 }
112 84
113 public static int AsNumber(this IntermediateField field) 85 public static int AsNumber(this IntermediateField field)
@@ -141,21 +113,7 @@ namespace WixToolset.Data
141 return null; 113 return null;
142 } 114 }
143 115
144 switch (field.Definition.Type) 116 return field.AsNumber();
145 {
146 case IntermediateFieldType.Bool:
147 return field.Value.AsBool() ? 1 : 0;
148
149 case IntermediateFieldType.LargeNumber:
150 case IntermediateFieldType.Number:
151 return field.Value.AsNumber();
152
153 case IntermediateFieldType.String:
154 return Convert.ToInt32(field.Value.AsString());
155
156 default:
157 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to number");
158 }
159 } 117 }
160 118
161 public static IntermediateFieldPathValue AsPath(this IntermediateField field) 119 public static IntermediateFieldPathValue AsPath(this IntermediateField field)
diff --git a/src/WixToolset.Data/IntermediateSymbol.cs b/src/WixToolset.Data/IntermediateSymbol.cs
index 4030df5d..408be37c 100644
--- a/src/WixToolset.Data/IntermediateSymbol.cs
+++ b/src/WixToolset.Data/IntermediateSymbol.cs
@@ -165,7 +165,6 @@ namespace WixToolset.Data
165 var id = (idJson == null) ? null : Identifier.Deserialize(idJson); 165 var id = (idJson == null) ? null : Identifier.Deserialize(idJson);
166 var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson); 166 var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson);
167 167
168 // TODO: this isn't sufficient.
169 if (!creator.TryGetSymbolDefinitionByName(definitionName, out var definition)) 168 if (!creator.TryGetSymbolDefinitionByName(definitionName, out var definition))
170 { 169 {
171 throw new WixException(ErrorMessages.UnknownSymbolType(definitionName)); 170 throw new WixException(ErrorMessages.UnknownSymbolType(definitionName));
diff --git a/src/WixToolset.Data/Json/JsonObjectExtensions.cs b/src/WixToolset.Data/Json/JsonObjectExtensions.cs
index e9e170ee..bc52333c 100644
--- a/src/WixToolset.Data/Json/JsonObjectExtensions.cs
+++ b/src/WixToolset.Data/Json/JsonObjectExtensions.cs
@@ -1,4 +1,4 @@
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. 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 2
3namespace WixToolset.Data 3namespace WixToolset.Data
4{ 4{
@@ -64,13 +64,13 @@ namespace WixToolset.Data
64 64
65 public static T GetValueOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue = default(T)) where T : class 65 public static T GetValueOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue = default(T)) where T : class
66 { 66 {
67 return jsonObject.TryGetValue(key, out var value) ? value as T: defaultValue; 67 return jsonObject.TryGetValue(key, out var value) ? value as T : defaultValue;
68 } 68 }
69 69
70 public static T GetEnumOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue) where T : struct 70 public static T GetEnumOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue) where T : struct
71 { 71 {
72#if DEBUG 72#if DEBUG
73 if (!typeof(T).IsEnum) throw new ArgumentException("This method is designed to only only support enums.", nameof(T)); 73 if (!typeof(T).IsEnum) { throw new ArgumentException("This method only supports enums.", nameof(T)); }
74#endif 74#endif
75 var value = jsonObject.GetValueOrDefault<string>(key); 75 var value = jsonObject.GetValueOrDefault<string>(key);
76 return Enum.TryParse(value, true, out T e) ? e : defaultValue; 76 return Enum.TryParse(value, true, out T e) ? e : defaultValue;
diff --git a/src/WixToolset.Data/Library.cs b/src/WixToolset.Data/Library.cs
deleted file mode 100644
index 13a670b0..00000000
--- a/src/WixToolset.Data/Library.cs
+++ /dev/null
@@ -1,243 +0,0 @@
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.Data
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9
10 /// <summary>
11 /// Object that represents a library file.
12 /// </summary>
13 public sealed class Library
14 {
15 public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wixlib";
16 private static readonly Version CurrentVersion = new Version("4.0.0.0");
17
18#if false
19 private string id;
20 private List<string> embedFilePaths;
21 private Dictionary<string, Localization> localizations;
22 private List<Section> sections;
23
24 /// <summary>
25 /// Instantiates a new empty library which is only useful from static creating methods.
26 /// </summary>
27 private Library()
28 {
29 this.embedFilePaths = new List<string>();
30 this.localizations = new Dictionary<string, Localization>();
31 this.sections = new List<Section>();
32 }
33
34 /// <summary>
35 /// Instantiate a new library populated with sections.
36 /// </summary>
37 /// <param name="sections">Sections to add to the library.</param>
38 /// <param name="localizationsByCulture"></param>
39 public Library(IEnumerable<Section> sections, IDictionary<string, Localization> localizationsByCulture, IEnumerable<string> embedFilePaths)
40 {
41 this.id = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).TrimEnd('=').Replace('+', '.').Replace('/', '_');
42 this.embedFilePaths = new List<string>(embedFilePaths);
43 this.localizations = new Dictionary<string, Localization>(localizationsByCulture);
44 this.sections = new List<Section>(sections);
45
46 foreach (Section section in this.sections)
47 {
48 section.LibraryId = this.id;
49 }
50 }
51
52 /// <summary>
53 /// Get the sections contained in this library.
54 /// </summary>
55 /// <value>Sections contained in this library.</value>
56 public IEnumerable<Section> Sections => this.sections;
57
58 /// <summary>
59 /// Gets localization files from this library that match the cultures passed in, in the order of the array of cultures.
60 /// </summary>
61 /// <param name="cultures">The list of cultures to get localizations for.</param>
62 /// <returns>All localizations contained in this library that match the set of cultures provided, in the same order.</returns>
63 public IEnumerable<Localization> GetLocalizations(string[] cultures)
64 {
65 foreach (string culture in cultures ?? new string[0])
66 {
67 if (this.localizations.TryGetValue(culture, out var localization))
68 {
69 yield return localization;
70 }
71 }
72 }
73
74 /// <summary>
75 /// Loads a library from a path on disk.
76 /// </summary>
77 /// <param name="path">Path to library file saved on disk.</param>
78 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
79 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
80 /// <returns>Returns the loaded library.</returns>
81 public static Library Load(string path, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
82 {
83 using (FileStream stream = File.OpenRead(path))
84 {
85 return Load(stream, new Uri(Path.GetFullPath(path)), tableDefinitions, suppressVersionCheck);
86 }
87 }
88
89 /// <summary>
90 /// Loads a library from a stream.
91 /// </summary>
92 /// <param name="stream">Stream containing the library file.</param>
93 /// <param name="uri">Uri for finding this stream.</param>
94 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
95 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
96 /// <returns>Returns the loaded library.</returns>
97 public static Library Load(Stream stream, Uri uri, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
98 {
99 using (FileStructure fs = FileStructure.Read(stream))
100 {
101 if (FileFormat.Wixlib != fs.FileFormat)
102 {
103 throw new WixUnexpectedFileFormatException(uri.LocalPath, FileFormat.Wixlib, fs.FileFormat);
104 }
105
106 using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri))
107 {
108 try
109 {
110 reader.MoveToContent();
111 return Library.Read(reader, tableDefinitions, suppressVersionCheck);
112 }
113 catch (XmlException xe)
114 {
115 throw new WixCorruptFileException(uri.LocalPath, fs.FileFormat, xe);
116 }
117 }
118 }
119 }
120
121 /// <summary>
122 /// Saves a library to a path on disk.
123 /// </summary>
124 /// <param name="path">Path to save library file to on disk.</param>
125 /// <param name="resolver">The WiX path resolver.</param>
126 public void Save(string path)
127 {
128 Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
129
130 using (FileStream stream = File.Create(path))
131 using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixlib, embedFilePaths))
132 using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream()))
133 {
134 writer.WriteStartDocument();
135
136 this.Write(writer);
137
138 writer.WriteEndDocument();
139 }
140 }
141
142 /// <summary>
143 /// Parse the root library element.
144 /// </summary>
145 /// <param name="reader">XmlReader with library persisted as Xml.</param>
146 /// <param name="tableDefinitions">Collection containing TableDefinitions to use when reconstituting the intermediates.</param>
147 /// <param name="suppressVersionCheck">Suppresses check for wix.dll version mismatch.</param>
148 /// <returns>The parsed Library.</returns>
149 private static Library Read(XmlReader reader, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
150 {
151 if (!reader.LocalName.Equals("wixLibrary"))
152 {
153 throw new XmlException();
154 }
155
156 bool empty = reader.IsEmptyElement;
157 Library library = new Library();
158 Version version = null;
159
160 while (reader.MoveToNextAttribute())
161 {
162 switch (reader.LocalName)
163 {
164 case "version":
165 version = new Version(reader.Value);
166 break;
167 case "id":
168 library.id = reader.Value;
169 break;
170 }
171 }
172
173 if (!suppressVersionCheck && null != version && !Library.CurrentVersion.Equals(version))
174 {
175 throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(reader.BaseURI), "library", version.ToString(), Library.CurrentVersion.ToString()));
176 }
177
178 if (!empty)
179 {
180 bool done = false;
181
182 while (!done && (XmlNodeType.Element == reader.NodeType || reader.Read()))
183 {
184 switch (reader.NodeType)
185 {
186 case XmlNodeType.Element:
187 switch (reader.LocalName)
188 {
189 case "localization":
190 Localization localization = Localization.Read(reader);
191 library.localizations.Add(localization.Culture, localization);
192 break;
193 case "section":
194 Section section = Section.Read(reader, tableDefinitions);
195 section.LibraryId = library.id;
196 library.sections.Add(section);
197 break;
198 default:
199 throw new XmlException();
200 }
201 break;
202 case XmlNodeType.EndElement:
203 done = true;
204 break;
205 }
206 }
207
208 if (!done)
209 {
210 throw new XmlException();
211 }
212 }
213
214 return library;
215 }
216
217 /// <summary>
218 /// Persists a library in an XML format.
219 /// </summary>
220 /// <param name="writer">XmlWriter where the library should persist itself as XML.</param>
221 private void Write(XmlWriter writer)
222 {
223 writer.WriteStartElement("wixLibrary", XmlNamespaceUri);
224
225 writer.WriteAttributeString("version", CurrentVersion.ToString());
226
227 writer.WriteAttributeString("id", this.id);
228
229 foreach (Localization localization in this.localizations.Values)
230 {
231 localization.Write(writer);
232 }
233
234 foreach (Section section in this.sections)
235 {
236 section.Write(writer);
237 }
238
239 writer.WriteEndElement();
240 }
241#endif
242 }
243}
diff --git a/src/WixToolset.Data/WarningMessages.cs b/src/WixToolset.Data/WarningMessages.cs
index a28b0fc3..edd06981 100644
--- a/src/WixToolset.Data/WarningMessages.cs
+++ b/src/WixToolset.Data/WarningMessages.cs
@@ -589,7 +589,7 @@ namespace WixToolset.Data
589 589
590 public static Message DefiningWellKnownDirectoryDeprecated(SourceLineNumber sourceLineNumbers, string directoryId) 590 public static Message DefiningWellKnownDirectoryDeprecated(SourceLineNumber sourceLineNumbers, string directoryId)
591 { 591 {
592 return Message(null, Ids.DefiningWellKnownDirectoryDeprecated, "It is no longer necessary to define Directory with Id '{0}'. One will be provided automatically. Remove the Directory element.", directoryId); 592 return Message(sourceLineNumbers, Ids.DefiningWellKnownDirectoryDeprecated, "It is no longer necessary to define Directory with Id '{0}'. One will be provided automatically. Remove the Directory element.", directoryId);
593 } 593 }
594 594
595 public static Message TooManyProgIds(SourceLineNumber sourceLineNumbers, string clsId, string progId, string otherClsId) 595 public static Message TooManyProgIds(SourceLineNumber sourceLineNumbers, string clsId, string progId, string otherClsId)