/// <summary> /// Executes a <see cref="CodeGenTemplate"/> replacing the placeholders with their respective values. /// </summary> public void Execute() { // Get the generated directory name. _eventArgs = new CodeGeneratorEventArgs { OutputGenDirName = CodeGenConfig.GetXmlVal <string>(_xmlTemplate, "OutputGenDirName", null !, false) }; // Invoke the XML. ExecuteXml(_xmlTemplate, _codeGenerator.Root !); }
/// <summary> /// Invoke an 'If' style condition. /// </summary> private bool ExecuteIfCondition(XElement xmlCon, CodeGenConfig config) { string condition = CodeGenConfig.GetXmlVal <string>(xmlCon, "Condition", null !, false); if (string.IsNullOrEmpty(condition)) { return(true); } bool notCondition = CodeGenConfig.GetXmlVal <bool>(xmlCon, "Not", false, false); string[] parts = condition.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); List <string> stmt = new List <string>(); foreach (string part in parts) { switch (part.ToUpperInvariant()) { case "AND": case "&&": if (!ExecuteIfConditionStmt(stmt, config, condition)) { return(ApplyNotCondition(false, notCondition)); } stmt.Clear(); break; case "OR": case "||": if (ExecuteIfConditionStmt(stmt, config, condition)) { return(ApplyNotCondition(true, notCondition)); } stmt.Clear(); break; default: stmt.Add(part); break; } } if (stmt.Count > 0) { return(ApplyNotCondition(ExecuteIfConditionStmt(stmt, config, condition), notCondition)); } throw new CodeGenException($"Condition is invalid: {condition}."); }
/// <summary> /// Gets the generated output file name where specified. /// </summary> private bool GetOutputFileName(XElement xml, CodeGenConfig config) { // Check if a file name has been specified. string fileName = CodeGenConfig.GetXmlVal <string>(xml, "OutputFileName", null !, false); if (fileName == null) { return(false); } // Record the specified file name. if (fileName.Length == 0) { throw new CodeGenException("'OutputFileName' attribute has no value; this must be specified."); } if (_eventArgs !.OutputFileName != null) { throw new CodeGenException("'OutputFileName' attribute unexpected; only one output file can be open at one time."); } string dirName = CodeGenConfig.GetXmlVal <string>(xml, "OutputDirName", null !, false); if (dirName != null && dirName.Length > 0) { _eventArgs !.OutputDirName = TemplateReplace(dirName, config); } _eventArgs !.OutputFileName = TemplateReplace(fileName, config); string isOutputNewOnly = CodeGenConfig.GetXmlVal <string>(xml, "IsOutputNewOnly", null !, false); if (!string.IsNullOrEmpty(isOutputNewOnly)) { var val = GetValue(isOutputNewOnly, config); if (val != null) { _eventArgs.IsOutputNewOnly = val is bool boolean ? boolean : throw new CodeGenException($"'IsOutputNewOnly' attribute value '{isOutputNewOnly}' does not result in a boolean value."); } } _eventArgs.IsOutputNewOnly = false; return(true); }
/// <summary> /// Executes a <see cref="CodeGenTemplate"/> replacing the placeholders with their respective values. /// </summary> public void Execute() { // Get the generated directory name. _eventArgs = new CodeGeneratorEventArgs { OutputGenDirName = CodeGenConfig.GetXmlVal <string>(_xmlTemplate, "OutputGenDirName", null, false) }; // Invoke the XML. try { ExecuteXml(_xmlTemplate, _codeGenerator.Root); } finally { if (_tw != null) { _tw.Dispose(); } } }