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
|
// 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.Core
{
using System;
using System.Collections.Generic;
using System.Text;
using WixToolset.Data;
using WixToolset.Data.Bind;
using WixToolset.Extensibility.Services;
/// <summary>
/// WiX variable resolver.
/// </summary>
internal sealed class WixVariableResolver : IVariableResolver
{
private Dictionary<string, BindVariable> locVariables;
private Dictionary<string, BindVariable> wixVariables;
private Dictionary<string, LocalizedControl> localizedControls;
/// <summary>
/// Instantiate a new WixVariableResolver.
/// </summary>
public WixVariableResolver(IMessaging messaging)
{
this.locVariables = new Dictionary<string, BindVariable>();
this.wixVariables = new Dictionary<string, BindVariable>();
this.Codepage = -1;
this.Messaging = messaging;
}
private IMessaging Messaging { get; }
public int Codepage { get; private set; }
public int VariableCount => this.wixVariables.Count;
public void AddLocalization(Localization localization)
{
if (-1 == this.Codepage)
{
this.Codepage = localization.Codepage;
}
foreach (var variable in localization.Variables)
{
if (!TryAddWixVariable(this.locVariables, variable))
{
this.Messaging.Write(ErrorMessages.DuplicateLocalizationIdentifier(variable.SourceLineNumbers, variable.Id));
}
}
foreach (KeyValuePair<string, LocalizedControl> localizedControl in localization.LocalizedControls)
{
if (!this.localizedControls.ContainsKey(localizedControl.Key))
{
this.localizedControls.Add(localizedControl.Key, localizedControl.Value);
}
}
}
public void AddVariable(SourceLineNumber sourceLineNumber, string name, string value, bool overridable)
{
var bindVariable = new BindVariable { Id = name, Value = value, Overridable = overridable, SourceLineNumbers = sourceLineNumber };
if (!TryAddWixVariable(this.wixVariables, bindVariable))
{
this.Messaging.Write(ErrorMessages.WixVariableCollision(sourceLineNumber, name));
}
}
public VariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value, bool localizationOnly)
{
return this.ResolveVariables(sourceLineNumbers, value, localizationOnly, true);
}
public bool TryGetLocalizedControl(string dialog, string control, out LocalizedControl localizedControl)
{
var key = LocalizedControl.GetKey(dialog, control);
return this.localizedControls.TryGetValue(key, out localizedControl);
}
/// <summary>
/// Resolve the wix variables in a value.
/// </summary>
/// <param name="sourceLineNumbers">The source line information for the value.</param>
/// <param name="value">The value to resolve.</param>
/// <param name="localizationOnly">true to only resolve localization variables; false otherwise.</param>
/// <param name="errorOnUnknown">true if unknown variables should throw errors.</param>
/// <param name="isDefault">true if the resolved value was the default.</param>
/// <param name="delayedResolve">true if the value has variables that cannot yet be resolved.</param>
/// <returns>The resolved value.</returns>
internal VariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value, bool localizationOnly, bool errorOnUnknown)
{
var matches = Common.WixVariableRegex.Matches(value);
// the value is the default unless its substituted further down
var result = new VariableResolution { IsDefault = true, Value = value };
if (0 < matches.Count)
{
var sb = new StringBuilder(value);
// notice how this code walks backward through the list
// because it modifies the string as we through it
for (int i = matches.Count - 1; 0 <= i; i--)
{
var variableNamespace = matches[i].Groups["namespace"].Value;
var variableId = matches[i].Groups["fullname"].Value;
string variableDefaultValue = null;
// get the default value if one was specified
if (matches[i].Groups["value"].Success)
{
variableDefaultValue = matches[i].Groups["value"].Value;
// localization variables to not support inline default values
if ("loc" == variableNamespace)
{
this.Messaging.Write(ErrorMessages.IllegalInlineLocVariable(sourceLineNumbers, variableId, variableDefaultValue));
}
}
// get the scope if one was specified
if (matches[i].Groups["scope"].Success)
{
if ("bind" == variableNamespace)
{
variableId = matches[i].Groups["name"].Value;
}
}
// check for an escape sequence of !! indicating the match is not a variable expression
if (0 < matches[i].Index && '!' == sb[matches[i].Index - 1])
{
if (!localizationOnly)
{
sb.Remove(matches[i].Index - 1, 1);
result.UpdatedValue = true;
}
}
else
{
string resolvedValue = null;
if ("loc" == variableNamespace)
{
// warn about deprecated syntax of $(loc.var)
if ('$' == sb[matches[i].Index])
{
this.Messaging.Write(WarningMessages.DeprecatedLocalizationVariablePrefix(sourceLineNumbers, variableId));
}
if (this.locVariables.TryGetValue(variableId, out var bindVariable))
{
resolvedValue = bindVariable.Value;
}
}
else if (!localizationOnly && "wix" == variableNamespace)
{
// illegal syntax of $(wix.var)
if ('$' == sb[matches[i].Index])
{
this.Messaging.Write(ErrorMessages.IllegalWixVariablePrefix(sourceLineNumbers, variableId));
}
else
{
if (this.wixVariables.TryGetValue(variableId, out var bindVariable))
{
resolvedValue = bindVariable.Value ?? String.Empty;
result.IsDefault = false;
}
else if (null != variableDefaultValue) // default the resolved value to the inline value if one was specified
{
resolvedValue = variableDefaultValue;
}
}
}
if ("bind" == variableNamespace)
{
// can't resolve these yet, but keep track of where we find them so they can be resolved later with less effort
result.DelayedResolve = true;
}
else
{
// insert the resolved value if it was found or display an error
if (null != resolvedValue)
{
sb.Remove(matches[i].Index, matches[i].Length);
sb.Insert(matches[i].Index, resolvedValue);
result.UpdatedValue = true;
}
else if ("loc" == variableNamespace && errorOnUnknown) // unresolved loc variable
{
this.Messaging.Write(ErrorMessages.LocalizationVariableUnknown(sourceLineNumbers, variableId));
}
else if (!localizationOnly && "wix" == variableNamespace && errorOnUnknown) // unresolved wix variable
{
this.Messaging.Write(ErrorMessages.WixVariableUnknown(sourceLineNumbers, variableId));
}
}
}
}
result.Value = sb.ToString();
}
return result;
}
private static bool TryAddWixVariable(IDictionary<string, BindVariable> variables, BindVariable variable)
{
if (!variables.TryGetValue(variable.Id, out var existingWixVariableRow) || (existingWixVariableRow.Overridable && !variable.Overridable))
{
variables[variable.Id] = variable;
return true;
}
return variable.Overridable;
}
}
}
|