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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// 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.Extensions
{
#if TODO_BRINGBACK_FOR_BUNDLES
using System;
using System.Collections.Generic;
using System.Globalization;
using WixToolset.Data;
using WixToolset.Extensibility;
/// <summary>
/// The binder for the WiX Toolset Utility Extension.
/// </summary>
public sealed class UtilBinder : BinderExtension
{
// TODO: When WixSearch is supported in Product, etc, we may need to call
// ReorderWixSearch() from each of those initializers.
// TODO: A general-purpose "reorder this table given these constraints"
// mechanism may end up being helpful. This could be declaratively stated
// in the table definitions, or exposed from the core Wix.dll and called
// as-needed by any extensions.
/// <summary>
/// Called before bundle binding occurs.
/// </summary>
public override void Initialize(Output bundle)
{
if (OutputType.Bundle == bundle.Type)
{
this.ReorderWixSearch(bundle);
}
}
/// <summary>
/// Reorders Any WixSearch items.
/// </summary>
/// <param name="output">Output containing the tables to process.</param>
private void ReorderWixSearch(Output output)
{
Table wixSearchTable = output.Tables["WixSearch"];
if (null == wixSearchTable || wixSearchTable.Rows.Count == 0)
{
// nothing to do!
return;
}
RowDictionary rowDictionary = new RowDictionary();
foreach (Row row in wixSearchTable.Rows)
{
rowDictionary.AddRow(row);
}
Constraints constraints = new Constraints();
Table wixSearchRelationTable = output.Tables["WixSearchRelation"];
if (null != wixSearchRelationTable && wixSearchRelationTable.Rows.Count > 0)
{
// add relational info to our data...
foreach (Row row in wixSearchRelationTable.Rows)
{
constraints.AddConstraint((string)row[0], (string)row[1]);
}
}
this.FindCircularReference(constraints);
if (this.Core.EncounteredError)
{
return;
}
this.FlattenDependentReferences(constraints);
// Reorder by topographical sort (http://en.wikipedia.org/wiki/Topological_sorting)
// We use a variation of Kahn (1962) algorithm as described in
// Wikipedia, with the additional criteria that start nodes are sorted
// lexicographically at each step to ensure a deterministic ordering
// based on 'after' dependencies and ID.
TopologicalSort sorter = new TopologicalSort();
List <string> sortedIds = sorter.Sort(rowDictionary.Keys, constraints);
// Now, re-write the table with the searches in order...
wixSearchTable.Rows.Clear();
foreach (string id in sortedIds)
{
wixSearchTable.Rows.Add(rowDictionary[id]);
}
}
/// <summary>
/// A dictionary of Row items, indexed by their first column.
/// </summary>
private class RowDictionary : Dictionary<string, Row>
{
public void AddRow(Row row)
{
this.Add((string)row[0], row);
}
// TODO: Hide other Add methods?
}
/// <summary>
/// A dictionary of constraints, mapping an id to a list of ids.
/// </summary>
private class Constraints : Dictionary<string, List<string>>
{
public void AddConstraint(string id, string afterId)
{
if (!this.ContainsKey(id))
{
this.Add(id, new List<string>());
}
// TODO: Show warning if a constraint is seen twice?
if (!this[id].Contains(afterId))
{
this[id].Add(afterId);
}
}
// TODO: Hide other Add methods?
}
/// <summary>
/// Finds circular references in the constraints.
/// </summary>
/// <param name="constraints">Constraints to check.</param>
/// <remarks>This is not particularly performant, but it works.</remarks>
private void FindCircularReference(Constraints constraints)
{
foreach (string id in constraints.Keys)
{
List<string> seenIds = new List<string>();
string chain = null;
if (FindCircularReference(constraints, id, id, seenIds, out chain))
{
// We will show a separate message for every ID that's in
// the loop. We could bail after the first one, but then
// we wouldn't catch disjoint loops in a single run.
this.Core.OnMessage(UtilErrors.CircularSearchReference(chain));
}
}
}
/// <summary>
/// Recursive function that finds circular references in the constraints.
/// </summary>
/// <param name="constraints">Constraints to check.</param>
/// <param name="checkId">The identifier currently being looking for. (Fixed across a given run.)</param>
/// <param name="currentId">The idenifier curently being tested.</param>
/// <param name="seenIds">A list of identifiers seen, to ensure each identifier is only expanded once.</param>
/// <param name="chain">If a circular reference is found, will contain the chain of references.</param>
/// <returns>True if a circular reference is found, false otherwise.</returns>
private bool FindCircularReference(Constraints constraints, string checkId, string currentId, List<string> seenIds, out string chain)
{
chain = null;
List<string> afterList = null;
if (constraints.TryGetValue(currentId, out afterList))
{
foreach (string afterId in afterList)
{
if (afterId == checkId)
{
chain = String.Format(CultureInfo.InvariantCulture, "{0} -> {1}", currentId, afterId);
return true;
}
if (!seenIds.Contains(afterId))
{
seenIds.Add(afterId);
if (FindCircularReference(constraints, checkId, afterId, seenIds, out chain))
{
chain = String.Format(CultureInfo.InvariantCulture, "{0} -> {1}", currentId, chain);
return true;
}
}
}
}
return false;
}
/// <summary>
/// Flattens any dependency chains to simplify reordering.
/// </summary>
/// <param name="constraints"></param>
private void FlattenDependentReferences(Constraints constraints)
{
foreach (string id in constraints.Keys)
{
List<string> flattenedIds = new List<string>();
AddDependentReferences(constraints, id, flattenedIds);
List<string> constraintList = constraints[id];
foreach (string flattenedId in flattenedIds)
{
if (!constraintList.Contains(flattenedId))
{
constraintList.Add(flattenedId);
}
}
}
}
/// <summary>
/// Adds dependent references to a list.
/// </summary>
/// <param name="constraints"></param>
/// <param name="currentId"></param>
/// <param name="seenIds"></param>
private void AddDependentReferences(Constraints constraints, string currentId, List<string> seenIds)
{
List<string> afterList = null;
if (constraints.TryGetValue(currentId, out afterList))
{
foreach (string afterId in afterList)
{
if (!seenIds.Contains(afterId))
{
seenIds.Add(afterId);
AddDependentReferences(constraints, afterId, seenIds);
}
}
}
}
/// <summary>
/// Reorder by topological sort
/// </summary>
/// <remarks>
/// We use a variation of Kahn (1962) algorithm as described in
/// Wikipedia (http://en.wikipedia.org/wiki/Topological_sorting), with
/// the additional criteria that start nodes are sorted lexicographically
/// at each step to ensure a deterministic ordering based on 'after'
/// dependencies and ID.
/// </remarks>
private class TopologicalSort
{
private List<string> startIds = new List<string>();
private Constraints constraints;
/// <summary>
/// Reorder by topological sort
/// </summary>
/// <param name="allIds">The complete list of IDs.</param>
/// <param name="constraints">Constraints to use.</param>
/// <returns>The topologically sorted list of IDs.</returns>
internal List<string> Sort(IEnumerable<string> allIds, Constraints constraints)
{
this.startIds.Clear();
this.CopyConstraints(constraints);
this.FindInitialStartIds(allIds);
// We always create a new sortedId list, because we return it
// to the caller and don't know what its lifetime may be.
List<string> sortedIds = new List<string>();
while (this.startIds.Count > 0)
{
this.SortStartIds();
string currentId = this.startIds[0];
sortedIds.Add(currentId);
this.startIds.RemoveAt(0);
this.ResolveConstraint(currentId);
}
return sortedIds;
}
/// <summary>
/// Copies a Constraints set (to prevent modifying the incoming data).
/// </summary>
/// <param name="constraints">Constraints to copy.</param>
private void CopyConstraints(Constraints constraints)
{
this.constraints = new Constraints();
foreach (string id in constraints.Keys)
{
foreach (string afterId in constraints[id])
{
this.constraints.AddConstraint(id, afterId);
}
}
}
/// <summary>
/// Finds initial start IDs. (Those with no constraints.)
/// </summary>
/// <param name="allIds">The complete list of IDs.</param>
private void FindInitialStartIds(IEnumerable<string> allIds)
{
foreach (string id in allIds)
{
if (!this.constraints.ContainsKey(id))
{
this.startIds.Add(id);
}
}
}
/// <summary>
/// Sorts start IDs.
/// </summary>
private void SortStartIds()
{
this.startIds.Sort();
}
/// <summary>
/// Removes the resolved constraint and updates the list of startIds
/// with any now-valid (all constraints resolved) IDs.
/// </summary>
/// <param name="resolvedId">The ID to resolve from the set of constraints.</param>
private void ResolveConstraint(string resolvedId)
{
List<string> newStartIds = new List<string>();
foreach (string id in constraints.Keys)
{
if (this.constraints[id].Contains(resolvedId))
{
this.constraints[id].Remove(resolvedId);
// If we just removed the last constraint for this
// ID, it is now a valid start ID.
if (0 == this.constraints[id].Count)
{
newStartIds.Add(id);
}
}
}
foreach (string id in newStartIds)
{
this.constraints.Remove(id);
}
this.startIds.AddRange(newStartIds);
}
}
}
#endif
}
|