diff options
| author | Bob Arnson <bob@firegiant.com> | 2020-03-13 20:30:06 -0400 |
|---|---|---|
| committer | Bob Arnson <bob@firegiant.com> | 2020-03-13 20:33:50 -0400 |
| commit | d097c7deb98803f6e9e46fe20261dd761efeb993 (patch) | |
| tree | 8875d6318bf08e73b2bbd2c19ccd99af00699f28 /src | |
| parent | 2cae81fe9d9457c395996e6f27db0d81e250e4f2 (diff) | |
| download | wix-d097c7deb98803f6e9e46fe20261dd761efeb993.tar.gz wix-d097c7deb98803f6e9e46fe20261dd761efeb993.tar.bz2 wix-d097c7deb98803f6e9e46fe20261dd761efeb993.zip | |
Clean up unused IVariableResolver functionality.
Handle escaped bind-time variable references.
Diffstat (limited to 'src')
| -rw-r--r-- | src/WixToolset.Core/Bind/ResolveFieldsCommand.cs | 2 | ||||
| -rw-r--r-- | src/WixToolset.Core/Compiler.cs | 2 | ||||
| -rw-r--r-- | src/WixToolset.Core/Librarian.cs | 2 | ||||
| -rw-r--r-- | src/WixToolset.Core/VariableResolver.cs | 44 | ||||
| -rw-r--r-- | src/test/WixToolsetTest.CoreIntegration/VariableResolverFixture.cs | 40 |
5 files changed, 72 insertions, 18 deletions
diff --git a/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs b/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs index 5cb2524d..5db878a1 100644 --- a/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs +++ b/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs | |||
| @@ -62,7 +62,7 @@ namespace WixToolset.Core.Bind | |||
| 62 | var original = field.AsString(); | 62 | var original = field.AsString(); |
| 63 | if (!String.IsNullOrEmpty(original)) | 63 | if (!String.IsNullOrEmpty(original)) |
| 64 | { | 64 | { |
| 65 | var resolution = this.VariableResolver.ResolveVariables(tuple.SourceLineNumbers, original, false); | 65 | var resolution = this.VariableResolver.ResolveVariables(tuple.SourceLineNumbers, original); |
| 66 | if (resolution.UpdatedValue) | 66 | if (resolution.UpdatedValue) |
| 67 | { | 67 | { |
| 68 | field.Set(resolution.Value); | 68 | field.Set(resolution.Value); |
diff --git a/src/WixToolset.Core/Compiler.cs b/src/WixToolset.Core/Compiler.cs index 6fd0f30b..de718c84 100644 --- a/src/WixToolset.Core/Compiler.cs +++ b/src/WixToolset.Core/Compiler.cs | |||
| @@ -251,7 +251,7 @@ namespace WixToolset.Core | |||
| 251 | var data = field.AsString(); | 251 | var data = field.AsString(); |
| 252 | if (!String.IsNullOrEmpty(data)) | 252 | if (!String.IsNullOrEmpty(data)) |
| 253 | { | 253 | { |
| 254 | var resolved = this.componentIdPlaceholdersResolver.ResolveVariables(tuple.SourceLineNumbers, data, false, false); | 254 | var resolved = this.componentIdPlaceholdersResolver.ResolveVariables(tuple.SourceLineNumbers, data, errorOnUnknown: false); |
| 255 | if (resolved.UpdatedValue) | 255 | if (resolved.UpdatedValue) |
| 256 | { | 256 | { |
| 257 | field.Set(resolved.Value); | 257 | field.Set(resolved.Value); |
diff --git a/src/WixToolset.Core/Librarian.cs b/src/WixToolset.Core/Librarian.cs index 5c0fb302..b6be73e9 100644 --- a/src/WixToolset.Core/Librarian.cs +++ b/src/WixToolset.Core/Librarian.cs | |||
| @@ -95,7 +95,7 @@ namespace WixToolset.Core | |||
| 95 | 95 | ||
| 96 | if (pathField != null && !String.IsNullOrEmpty(pathField.Path)) | 96 | if (pathField != null && !String.IsNullOrEmpty(pathField.Path)) |
| 97 | { | 97 | { |
| 98 | var resolution = variableResolver.ResolveVariables(tuple.SourceLineNumbers, pathField.Path, false); | 98 | var resolution = variableResolver.ResolveVariables(tuple.SourceLineNumbers, pathField.Path); |
| 99 | 99 | ||
| 100 | var file = fileResolver.Resolve(tuple.SourceLineNumbers, tuple.Definition, resolution.Value); | 100 | var file = fileResolver.Resolve(tuple.SourceLineNumbers, tuple.Definition, resolution.Value); |
| 101 | 101 | ||
diff --git a/src/WixToolset.Core/VariableResolver.cs b/src/WixToolset.Core/VariableResolver.cs index 4a65bec2..b1a5defc 100644 --- a/src/WixToolset.Core/VariableResolver.cs +++ b/src/WixToolset.Core/VariableResolver.cs | |||
| @@ -66,9 +66,9 @@ namespace WixToolset.Core | |||
| 66 | } | 66 | } |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | public IVariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value, bool localizationOnly) | 69 | public IVariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value) |
| 70 | { | 70 | { |
| 71 | return this.ResolveVariables(sourceLineNumbers, value, localizationOnly, true); | 71 | return this.ResolveVariables(sourceLineNumbers, value, errorOnUnknown: true); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | public bool TryGetLocalizedControl(string dialog, string control, out LocalizedControl localizedControl) | 74 | public bool TryGetLocalizedControl(string dialog, string control, out LocalizedControl localizedControl) |
| @@ -82,25 +82,27 @@ namespace WixToolset.Core | |||
| 82 | /// </summary> | 82 | /// </summary> |
| 83 | /// <param name="sourceLineNumbers">The source line information for the value.</param> | 83 | /// <param name="sourceLineNumbers">The source line information for the value.</param> |
| 84 | /// <param name="value">The value to resolve.</param> | 84 | /// <param name="value">The value to resolve.</param> |
| 85 | /// <param name="localizationOnly">true to only resolve localization variables; false otherwise.</param> | ||
| 86 | /// <param name="errorOnUnknown">true if unknown variables should throw errors.</param> | 85 | /// <param name="errorOnUnknown">true if unknown variables should throw errors.</param> |
| 87 | /// <returns>The resolved value.</returns> | 86 | /// <returns>The resolved value.</returns> |
| 88 | internal IVariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value, bool localizationOnly, bool errorOnUnknown) | 87 | internal IVariableResolution ResolveVariables(SourceLineNumber sourceLineNumbers, string value, bool errorOnUnknown) |
| 89 | { | 88 | { |
| 90 | var matches = Common.WixVariableRegex.Matches(value); | 89 | var matches = Common.WixVariableRegex.Matches(value); |
| 91 | 90 | ||
| 92 | // the value is the default unless its substituted further down | 91 | // the value is the default unless it's substituted further down |
| 93 | var result = this.ServiceProvider.GetService<IVariableResolution>(); | 92 | var result = this.ServiceProvider.GetService<IVariableResolution>(); |
| 94 | result.IsDefault = true; | 93 | result.IsDefault = true; |
| 95 | result.Value = value; | 94 | result.Value = value; |
| 96 | 95 | ||
| 97 | while (!this.Messaging.EncounteredError && !result.DelayedResolve && matches.Count > 0) | 96 | var finalizeEscapes = false; |
| 97 | |||
| 98 | while (matches.Count > 0) | ||
| 98 | { | 99 | { |
| 100 | var updatedResultThisPass = false; | ||
| 99 | var sb = new StringBuilder(value); | 101 | var sb = new StringBuilder(value); |
| 100 | 102 | ||
| 101 | // notice how this code walks backward through the list | 103 | // notice how this code walks backward through the list |
| 102 | // because it modifies the string as we move through it | 104 | // because it modifies the string as we move through it |
| 103 | for (int i = matches.Count - 1; 0 <= i; i--) | 105 | for (var i = matches.Count - 1; 0 <= i; i--) |
| 104 | { | 106 | { |
| 105 | var variableNamespace = matches[i].Groups["namespace"].Value; | 107 | var variableNamespace = matches[i].Groups["namespace"].Value; |
| 106 | var variableId = matches[i].Groups["fullname"].Value; | 108 | var variableId = matches[i].Groups["fullname"].Value; |
| @@ -130,12 +132,16 @@ namespace WixToolset.Core | |||
| 130 | // check for an escape sequence of !! indicating the match is not a variable expression | 132 | // check for an escape sequence of !! indicating the match is not a variable expression |
| 131 | if (0 < matches[i].Index && '!' == sb[matches[i].Index - 1]) | 133 | if (0 < matches[i].Index && '!' == sb[matches[i].Index - 1]) |
| 132 | { | 134 | { |
| 133 | if (!localizationOnly) | 135 | if (finalizeEscapes) |
| 134 | { | 136 | { |
| 135 | sb.Remove(matches[i].Index - 1, 1); | 137 | sb.Remove(matches[i].Index - 1, 1); |
| 136 | 138 | ||
| 137 | result.UpdatedValue = true; | 139 | result.UpdatedValue = true; |
| 138 | } | 140 | } |
| 141 | else | ||
| 142 | { | ||
| 143 | continue; | ||
| 144 | } | ||
| 139 | } | 145 | } |
| 140 | else | 146 | else |
| 141 | { | 147 | { |
| @@ -154,7 +160,7 @@ namespace WixToolset.Core | |||
| 154 | resolvedValue = bindVariable.Value; | 160 | resolvedValue = bindVariable.Value; |
| 155 | } | 161 | } |
| 156 | } | 162 | } |
| 157 | else if (!localizationOnly && "wix" == variableNamespace) | 163 | else if ("wix" == variableNamespace) |
| 158 | { | 164 | { |
| 159 | // illegal syntax of $(wix.var) | 165 | // illegal syntax of $(wix.var) |
| 160 | if ('$' == sb[matches[i].Index]) | 166 | if ('$' == sb[matches[i].Index]) |
| @@ -189,12 +195,13 @@ namespace WixToolset.Core | |||
| 189 | sb.Insert(matches[i].Index, resolvedValue); | 195 | sb.Insert(matches[i].Index, resolvedValue); |
| 190 | 196 | ||
| 191 | result.UpdatedValue = true; | 197 | result.UpdatedValue = true; |
| 198 | updatedResultThisPass = true; | ||
| 192 | } | 199 | } |
| 193 | else if ("loc" == variableNamespace && errorOnUnknown) // unresolved loc variable | 200 | else if ("loc" == variableNamespace && errorOnUnknown) // unresolved loc variable |
| 194 | { | 201 | { |
| 195 | this.Messaging.Write(ErrorMessages.LocalizationVariableUnknown(sourceLineNumbers, variableId)); | 202 | this.Messaging.Write(ErrorMessages.LocalizationVariableUnknown(sourceLineNumbers, variableId)); |
| 196 | } | 203 | } |
| 197 | else if (!localizationOnly && "wix" == variableNamespace && errorOnUnknown) // unresolved wix variable | 204 | else if ("wix" == variableNamespace && errorOnUnknown) // unresolved wix variable |
| 198 | { | 205 | { |
| 199 | this.Messaging.Write(ErrorMessages.WixVariableUnknown(sourceLineNumbers, variableId)); | 206 | this.Messaging.Write(ErrorMessages.WixVariableUnknown(sourceLineNumbers, variableId)); |
| 200 | } | 207 | } |
| @@ -204,7 +211,22 @@ namespace WixToolset.Core | |||
| 204 | 211 | ||
| 205 | result.Value = sb.ToString(); | 212 | result.Value = sb.ToString(); |
| 206 | value = result.Value; | 213 | value = result.Value; |
| 207 | matches = Common.WixVariableRegex.Matches(value); | 214 | |
| 215 | if (finalizeEscapes) | ||
| 216 | { | ||
| 217 | // escaped references have been un-escaped, so we're done | ||
| 218 | break; | ||
| 219 | } | ||
| 220 | else if (updatedResultThisPass) | ||
| 221 | { | ||
| 222 | // we substituted loc strings, so make another pass to see if that brought in more loc strings | ||
| 223 | matches = Common.WixVariableRegex.Matches(value); | ||
| 224 | } | ||
| 225 | else | ||
| 226 | { | ||
| 227 | // make one final pass to un-escape any escaped references | ||
| 228 | finalizeEscapes = true; | ||
| 229 | } | ||
| 208 | } | 230 | } |
| 209 | 231 | ||
| 210 | return result; | 232 | return result; |
diff --git a/src/test/WixToolsetTest.CoreIntegration/VariableResolverFixture.cs b/src/test/WixToolsetTest.CoreIntegration/VariableResolverFixture.cs index eaeb4724..abf1bd43 100644 --- a/src/test/WixToolsetTest.CoreIntegration/VariableResolverFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/VariableResolverFixture.cs | |||
| @@ -29,10 +29,42 @@ namespace WixToolsetTest.CoreIntegration | |||
| 29 | 29 | ||
| 30 | variableResolver.AddLocalization(localization); | 30 | variableResolver.AddLocalization(localization); |
| 31 | 31 | ||
| 32 | Assert.Equal("Welcome to Localized Product Name", variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductName)", false).Value); | 32 | var result = variableResolver.ResolveVariables(null, "These are not the loc strings you're looking for."); |
| 33 | Assert.Equal("Welcome to Localized Product Name Enterprise Edition", variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEdition)", false).Value); | 33 | Assert.Equal("These are not the loc strings you're looking for.", result.Value); |
| 34 | Assert.Equal("Welcome to Localized Product Name Enterprise Edition v1.2.3", variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEditionVersion)", false).Value); | 34 | Assert.False(result.UpdatedValue); |
| 35 | Assert.Throws<WixException>(() => variableResolver.ResolveVariables(null, "Welcome to !(loc.UnknownLocalizationVariable)", false)); | 35 | |
| 36 | result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductName)"); | ||
| 37 | Assert.Equal("Welcome to Localized Product Name", result.Value); | ||
| 38 | Assert.True(result.UpdatedValue); | ||
| 39 | |||
| 40 | result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEdition)"); | ||
| 41 | Assert.Equal("Welcome to Localized Product Name Enterprise Edition", result.Value); | ||
| 42 | Assert.True(result.UpdatedValue); | ||
| 43 | |||
| 44 | result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEditionVersion)"); | ||
| 45 | Assert.Equal("Welcome to Localized Product Name Enterprise Edition v1.2.3", result.Value); | ||
| 46 | Assert.True(result.UpdatedValue); | ||
| 47 | |||
| 48 | result = variableResolver.ResolveVariables(null, "Welcome to !(bind.property.ProductVersion)"); | ||
| 49 | Assert.Equal("Welcome to !(bind.property.ProductVersion)", result.Value); | ||
| 50 | Assert.False(result.UpdatedValue); | ||
| 51 | Assert.True(result.DelayedResolve); | ||
| 52 | |||
| 53 | Assert.Throws<WixException>(() => variableResolver.ResolveVariables(null, "Welcome to !(loc.UnknownLocalizationVariable)")); | ||
| 54 | |||
| 55 | result = variableResolver.ResolveVariables(null, "Welcome to !!(loc.UnknownLocalizationVariable)"); | ||
| 56 | Assert.Equal("Welcome to !(loc.UnknownLocalizationVariable)", result.Value); | ||
| 57 | Assert.True(result.UpdatedValue); | ||
| 58 | |||
| 59 | result = variableResolver.ResolveVariables(null, "Welcome to !!(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)"); | ||
| 60 | Assert.Equal("Welcome to !(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)", result.Value); | ||
| 61 | Assert.True(result.UpdatedValue); | ||
| 62 | Assert.True(result.DelayedResolve); | ||
| 63 | |||
| 64 | result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEditionVersion) !!(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)"); | ||
| 65 | Assert.Equal("Welcome to Localized Product Name Enterprise Edition v1.2.3 !(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)", result.Value); | ||
| 66 | Assert.True(result.UpdatedValue); | ||
| 67 | Assert.True(result.DelayedResolve); | ||
| 36 | } | 68 | } |
| 37 | } | 69 | } |
| 38 | } | 70 | } |
