/// <summary> /// Get the value of a variable. /// </summary> /// <param name="sourceLineNumbers">The source line information for the function.</param> /// <param name="prefix">The variable prefix.</param> /// <param name="name">The variable name.</param> /// <returns>The variable value or null if the variable is not set.</returns> public string GetVariableValue(SourceLineNumberCollection sourceLineNumbers, string prefix, string name) { if (null == prefix) { throw new ArgumentNullException("prefix"); } if (null == name) { throw new ArgumentNullException("name"); } if (0 == name.Length) { throw new ArgumentException("Empty variable name.", "name"); } switch (prefix) { case "env": return(Environment.GetEnvironmentVariable(name)); case "sys": switch (name) { case "CURRENTDIR": return(String.Concat(Directory.GetCurrentDirectory(), Path.DirectorySeparatorChar)); case "SOURCEFILEDIR": return(String.Concat(Path.GetDirectoryName(this.sourceFile), Path.DirectorySeparatorChar)); case "SOURCEFILEPATH": return(this.sourceFile); default: return(null); } case "var": return((string)this.variables[name]); default: PreprocessorExtension extension = (PreprocessorExtension)this.extensionsByPrefix[prefix]; if (null != extension) { try { return(extension.GetVariableValue(prefix, name)); } catch (Exception e) { throw new WixException(WixErrors.PreprocessorExtensionGetVariableValueFailed(sourceLineNumbers, prefix, name, e.Message)); } } else { return(null); } } }
/// <summary> /// Evaluate a function. /// </summary> /// <param name="sourceLineNumbers">The source line information for the function.</param> /// <param name="prefix">The function prefix.</param> /// <param name="function">The function name.</param> /// <param name="args">The arguments for the function.</param> /// <returns>The function value or null if the function is not defined.</returns> public string EvaluateFunction(SourceLineNumberCollection sourceLineNumbers, string prefix, string function, string[] args) { if (String.IsNullOrEmpty(prefix)) { throw new ArgumentNullException("prefix"); } if (String.IsNullOrEmpty(function)) { throw new ArgumentNullException("function"); } switch (prefix) { case "fun": switch (function) { case "AutoVersion": // Make sure the base version is specified if (args.Length == 0 || args[0].Length == 0) { throw new WixException(WixErrors.InvalidPreprocessorFunctionAutoVersion(sourceLineNumbers)); } // Build = days since 1/1/2000; Revision = seconds since midnight / 2 DateTime now = DateTime.Now.ToUniversalTime(); TimeSpan tsBuild = now - new DateTime(2000, 1, 1); TimeSpan tsRevision = now - new DateTime(now.Year, now.Month, now.Day); return(String.Format("{0}.{1}.{2}", args[0], (int)tsBuild.TotalDays, (int)(tsRevision.TotalSeconds / 2))); // Add any core defined functions here default: return(null); } default: PreprocessorExtension extension = (PreprocessorExtension)this.extensionsByPrefix[prefix]; if (null != extension) { try { return(extension.EvaluateFunction(prefix, function, args)); } catch (Exception e) { throw new WixException(WixErrors.PreprocessorExtensionEvaluateFunctionFailed(sourceLineNumbers, prefix, function, String.Join(",", args), e.Message)); } } else { return(null); } } }
/// <summary> /// Evaluate a function. /// </summary> /// <param name="sourceLineNumbers">The source line information for the function.</param> /// <param name="prefix">The function prefix.</param> /// <param name="function">The function name.</param> /// <param name="args">The arguments for the function.</param> /// <returns>The function value or null if the function is not defined.</returns> public string EvaluateFunction(SourceLineNumberCollection sourceLineNumbers, string prefix, string function, string[] args) { if (null == prefix) { throw new ArgumentNullException("prefix"); } if (null == function) { throw new ArgumentNullException("name"); } if (0 == function.Length) { throw new ArgumentException("Empty function call.", "function"); } switch (prefix) { case "fun": switch (function) { // Add any core defined functions here default: return(null); } default: PreprocessorExtension extension = (PreprocessorExtension)this.extensionsByPrefix[prefix]; if (null != extension) { try { return(extension.EvaluateFunction(prefix, function, args)); } catch (Exception e) { throw new WixException(WixErrors.PreprocessorExtensionEvaluateFunctionFailed(sourceLineNumbers, prefix, function, String.Join(",", args), e.Message)); } } else { return(null); } } }
/// <summary> /// Evaluate a Pragma. /// </summary> /// <param name="sourceLineNumbers">The source line information for the function.</param> /// <param name="pragmaName">The pragma's full name (<prefix>.<pragma>).</param> /// <param name="args">The arguments to the pragma.</param> /// <param name="writer">The xml writer.</param> public void PreprocessPragma(SourceLineNumberCollection sourceLineNumbers, string pragmaName, string args, XmlWriter writer) { string[] prefixParts = pragmaName.Split(variableSplitter, 2); // Check to make sure there are 2 parts and neither is an empty string. if (2 != prefixParts.Length) { throw new WixException(WixErrors.InvalidPreprocessorPragma(sourceLineNumbers, pragmaName)); } string prefix = prefixParts[0]; string pragma = prefixParts[1]; if (String.IsNullOrEmpty(prefix) || String.IsNullOrEmpty(pragma)) { throw new WixException(WixErrors.InvalidPreprocessorPragma(sourceLineNumbers, pragmaName)); } switch (prefix) { case "wix": switch (pragma) { // Add any core defined pragmas here default: this.OnMessage(WixWarnings.PreprocessorUnknownPragma(sourceLineNumbers, pragmaName)); break; } break; default: PreprocessorExtension extension = (PreprocessorExtension)this.extensionsByPrefix[prefix]; if (null == extension || !extension.ProcessPragma(sourceLineNumbers, prefix, pragma, args, writer)) { this.OnMessage(WixWarnings.PreprocessorUnknownPragma(sourceLineNumbers, pragmaName)); } break; } }
/// <summary> /// Get the value of a variable. /// </summary> /// <param name="sourceLineNumbers">The source line information for the function.</param> /// <param name="prefix">The variable prefix.</param> /// <param name="name">The variable name.</param> /// <returns>The variable value or null if the variable is not set.</returns> public string GetVariableValue(SourceLineNumberCollection sourceLineNumbers, string prefix, string name) { if (String.IsNullOrEmpty(prefix)) { throw new ArgumentNullException("prefix"); } if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } switch (prefix) { case "env": return(Environment.GetEnvironmentVariable(name)); case "sys": switch (name) { case "CURRENTDIR": return(String.Concat(Directory.GetCurrentDirectory(), Path.DirectorySeparatorChar)); case "SOURCEFILEDIR": return(String.Concat(Path.GetDirectoryName(sourceLineNumbers[0].FileName), Path.DirectorySeparatorChar)); case "SOURCEFILEPATH": return(sourceLineNumbers[0].FileName); case "PLATFORM": this.OnMessage(WixWarnings.DeprecatedPreProcVariable(sourceLineNumbers, "$(sys.PLATFORM)", "$(sys.BUILDARCH)")); goto case "BUILDARCH"; case "BUILDARCH": switch (this.currentPlatform) { case Platform.X86: return("x86"); case Platform.X64: return("x64"); case Platform.IA64: return("ia64"); default: throw new ArgumentException(WixStrings.EXP_UnknownPlatformEnum, this.currentPlatform.ToString()); } default: return(null); } case "var": return((string)this.variables[name]); default: PreprocessorExtension extension = (PreprocessorExtension)this.extensionsByPrefix[prefix]; if (null != extension) { try { return(extension.GetVariableValue(prefix, name)); } catch (Exception e) { throw new WixException(WixErrors.PreprocessorExtensionGetVariableValueFailed(sourceLineNumbers, prefix, name, e.Message)); } } else { return(null); } } }
/// <summary> /// Instantiate a new WixExtensionTypeConflictException. /// </summary> /// <param name="extension">Extension with the conflict.</param> /// <param name="existingExtension">Existing extension with the same type</param> public WixExtensionTypeConflictException(PreprocessorExtension extension, PreprocessorExtension existingExtension) : base(null, ExceptionType) { this.extension = extension; this.existingExtension = existingExtension; }
/// <summary> /// Instantiate a new WixExtensionTypeConflictException. /// </summary> /// <param name="extension">Extension with the conflict.</param> /// <param name="existingExtension">Existing extension with the same type</param> public WixExtensionTypeConflictException(PreprocessorExtension extension, PreprocessorExtension existingExtension) : base(null, ExceptionType) { this.extension = extension; this.existingExtension = existingExtension; }
/// <summary> /// Adds an extension to the preprocessor. /// </summary> /// <param name="extension">preprocessor extension to add to preprocessor.</param> public void AddExtension(PreprocessorExtension extension) { extension.Messages = this.extensionMessages; // check if this extension is adding an extension type that already exists if (this.extensionTypes.Contains(extension.Type)) { throw new WixExtensionTypeConflictException(extension, (PreprocessorExtension)this.extensionTypes[extension.Type]); } this.extensionTypes.Add(extension.Type, extension); }