From c455d2290ef903ff36d540903e27d76d473cb67c Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Fri, 27 Mar 2020 14:30:35 +1000 Subject: Add SetVariable. --- src/WixToolset.Core/Compiler.cs | 6 ++ src/WixToolset.Core/Compiler_Bundle.cs | 142 +++++++++++++++++++++++++-------- 2 files changed, 116 insertions(+), 32 deletions(-) (limited to 'src/WixToolset.Core') diff --git a/src/WixToolset.Core/Compiler.cs b/src/WixToolset.Core/Compiler.cs index 6f122f7b..7638c11e 100644 --- a/src/WixToolset.Core/Compiler.cs +++ b/src/WixToolset.Core/Compiler.cs @@ -6221,6 +6221,12 @@ namespace WixToolset.Core case "SetProperty": this.ParseSetPropertyElement(child); break; + case "SetVariable": + this.ParseSetVariableElement(child); + break; + case "SetVariableRef": + this.ParseSimpleRefElement(child, "WixSetVariable"); + break; case "SFPCatalog": string parentName = null; this.ParseSFPCatalogElement(child, ref parentName); diff --git a/src/WixToolset.Core/Compiler_Bundle.cs b/src/WixToolset.Core/Compiler_Bundle.cs index a840e448..5d7072d0 100644 --- a/src/WixToolset.Core/Compiler_Bundle.cs +++ b/src/WixToolset.Core/Compiler_Bundle.cs @@ -323,6 +323,12 @@ namespace WixToolset.Core case "RelatedBundle": this.ParseRelatedBundleElement(child); break; + case "SetVariable": + this.ParseSetVariableElement(child); + break; + case "SetVariableRef": + this.ParseSimpleRefElement(child, "WixSetVariable"); + break; case "Update": this.ParseUpdateElement(child); break; @@ -2704,6 +2710,78 @@ namespace WixToolset.Core } } + /// + /// Parse SetVariable element + /// + /// Element to parse + private void ParseSetVariableElement(XElement node) + { + var sourceLineNumbers = Preprocessor.GetSourceLineNumbers(node); + Identifier id = null; + string variable = null; + string condition = null; + string after = null; + string value = null; + string type = null; + + foreach (var attrib in node.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || CompilerCore.WixNamespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Id": + id = this.Core.GetAttributeIdentifier(sourceLineNumbers, attrib); + break; + case "Variable": + variable = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Condition": + condition = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "After": + after = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Value": + value = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Type": + type = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + + default: + this.Core.UnexpectedAttribute(node, attrib); + break; + } + } + else + { + this.Core.ParseExtensionAttribute(node, attrib, null); + } + } + + type = this.ValidateVariableTypeWithValue(sourceLineNumbers, type, value); + + this.Core.ParseForExtensionElements(node); + + if (id == null) + { + id = this.Core.CreateIdentifier("sbv", variable, condition, after, value, type); + } + + this.Core.CreateWixSearchTuple(sourceLineNumbers, node.Name.LocalName, id, variable, condition, after); + + if (!this.Messaging.EncounteredError) + { + var tuple = new WixSetVariableTuple(sourceLineNumbers, id) + { + Value = value, + Type = type, + }; + this.Core.AddTuple(tuple); + } + } + /// /// Parse Variable element /// @@ -2764,64 +2842,64 @@ namespace WixToolset.Core this.Core.Write(ErrorMessages.ReservedNamespaceViolation(sourceLineNumbers, node.Name.LocalName, "Name", "Wix")); } - if (null == type && null != value) + type = this.ValidateVariableTypeWithValue(sourceLineNumbers, type, value); + + this.Core.ParseForExtensionElements(node); + + if (!this.Core.EncounteredError) + { + var tuple = new WixBundleVariableTuple(sourceLineNumbers, new Identifier(AccessModifier.Private, name)) + { + Value = value, + Type = type, + Hidden = hidden, + Persisted = persisted + }; + + this.Core.AddTuple(tuple); + } + } + + private string ValidateVariableTypeWithValue(SourceLineNumber sourceLineNumbers, string type, string value) + { + var newType = type; + if (newType == null && value != null) { // Infer the type from the current value... if (value.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { // Version constructor does not support simple "v#" syntax so check to see if the value is // non-negative real quick. - if (Int32.TryParse(value.Substring(1), NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out var number)) + if (Int32.TryParse(value.Substring(1), NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out var _)) { - type = "version"; + newType = "version"; } - else + else if (Version.TryParse(value.Substring(1), out var _)) { - // Sadly, Version doesn't have a TryParse() method until .NET 4, so we have to try/catch to see if it parses. - try - { - var version = new Version(value.Substring(1)); - type = "version"; - } - catch (Exception) - { - } + newType = "version"; } } // Not a version, check for numeric. - if (null == type) + if (newType == null) { - if (Int64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out var number)) + if (Int64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out var _)) { - type = "numeric"; + newType = "numeric"; } else { - type = "string"; + newType = "string"; } } } - if (null == value && null != type) + if (value == null && newType != null) { this.Core.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, "Variable", "Value", "Type")); } - this.Core.ParseForExtensionElements(node); - - if (!this.Core.EncounteredError) - { - var tuple = new WixBundleVariableTuple(sourceLineNumbers, new Identifier(AccessModifier.Private, name)) - { - Value = value, - Type = type, - Hidden = hidden, - Persisted = persisted - }; - - this.Core.AddTuple(tuple); - } + return newType; } private class RemotePayload -- cgit v1.2.3-55-g6feb