/// ------------------------------------------------------------------------------------ /// <summary> /// Initializes a new instance of the <see cref="FdoGenerate"/> class. /// </summary> /// <param name="doc">The XMI document.</param> /// <param name="outputDir">The output dir.</param> /// <param name="outputFile">The output file name.</param> /// ------------------------------------------------------------------------------------ public FdoGenerateImpl(XmlDocument doc, string outputDir, string outputFile) { Generator = this; m_Document = doc; m_OutputDir = outputDir; m_OutputFileName = outputFile; var entireModel = (XmlElement)doc.GetElementsByTagName("EntireModel")[0]; m_Model = new Model(entireModel); m_Engine = new VelocityEngine(); m_Engine.Init(); m_Context = new VelocityContext(); m_Context.Put("fdogenerate", this); m_Context.Put("model", m_Model); RuntimeSingleton.RuntimeServices.SetApplicationAttribute("FdoGenerate.Engine", m_Engine); RuntimeSingleton.RuntimeServices.SetApplicationAttribute("FdoGenerate.Context", m_Context); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Initializes a new instance of the <see cref="FdoGenerate"/> class. /// </summary> /// <param name="doc">The XMI document.</param> /// <param name="outputDir">The output dir.</param> /// <param name="outputFile">The output file name.</param> /// ------------------------------------------------------------------------------------ public FdoGenerateImpl(XmlDocument doc, string outputDir, string outputFile) { Generator = this; m_Document = doc; m_OutputDir = outputDir; m_OutputFileName = outputFile; var entireModel = (XmlElement)doc.GetElementsByTagName("EntireModel")[0]; m_Model = new Model(entireModel); m_Engine = new VelocityEngine(); m_Engine.Init(); m_Context = new VelocityContext(); m_Context.Put("fdogenerate", this); m_Context.Put("model", m_Model); RuntimeSingleton.RuntimeServices.SetApplicationAttribute("FdoGenerate.Engine", m_Engine); RuntimeSingleton.RuntimeServices.SetApplicationAttribute("FdoGenerate.Context", m_Context); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Executes the task. /// </summary> /// ------------------------------------------------------------------------------------ public override bool Execute() { string origDir = Directory.GetCurrentDirectory(); string oldDir; if (!String.IsNullOrEmpty(WorkingDirectory)) oldDir = WorkingDirectory; else oldDir = origDir; try { var doc = new XmlDocument(); string xmlPath = XmlFile; if (!Path.IsPathRooted(xmlPath)) xmlPath = Path.Combine(oldDir, XmlFile); try { Log.LogMessage(MessageImportance.Low, "Loading XML file {0}.", xmlPath); doc.Load(xmlPath); } catch (XmlException e) { Log.LogMessage(MessageImportance.High, "Error loading XML file " + xmlPath + " " + e.Message); return false; } var config = new XmlDocument(); var handGeneratedClasses = new Dictionary<string, List<string>>(); try { Log.LogMessage(MessageImportance.Low, "Loading hand generated classes from \"HandGenerated.xml\"."); config.Load(Path.Combine(oldDir, Path.Combine("FDOGenerate", "HandGenerated.xml"))); foreach (XmlElement node in config.GetElementsByTagName("Class")) { var props = new List<string>(); // ReSharper disable PossibleNullReferenceException foreach (XmlNode propertyNode in node.SelectNodes("property")) // ReSharper restore PossibleNullReferenceException { props.Add(propertyNode.Attributes["name"].Value); } if (props.Count > 0) { handGeneratedClasses.Add(node.Attributes["id"].Value, props); } } } catch (XmlException e) { Log.LogMessage(MessageImportance.High, "Error loading hand generated classes" + " " + e.Message); return false; } // Dictionary<ClassName, Property> var intPropTypeOverridesClasses = new Dictionary<string, Dictionary<string, string>>(); try { Log.LogMessage(MessageImportance.Low, "Loading hand generated classes from \"IntPropTypeOverrides.xml\"."); config.Load(Path.Combine(oldDir, Path.Combine("FDOGenerate", "IntPropTypeOverrides.xml"))); foreach (XmlElement node in config.GetElementsByTagName("Class")) { // Dictionary<PropertyName, PropertyType> var props = new Dictionary<string, string>(); // ReSharper disable PossibleNullReferenceException foreach (XmlNode propertyNode in node.SelectNodes("property")) // ReSharper restore PossibleNullReferenceException { props.Add(propertyNode.Attributes["name"].Value, propertyNode.Attributes["type"].Value); } if (props.Count > 0) { intPropTypeOverridesClasses.Add(node.Attributes["id"].Value, props); } } } catch (XmlException e) { Log.LogMessage(MessageImportance.High, "Error loading IntPropTypeOverrides classes" + " " + e.Message); return false; } try { // Remember current directory. var originalCurrentDirectory = Directory.GetCurrentDirectory(); Log.LogMessage(MessageImportance.Low, "Processing template {0}.", TemplateFile); string outputDirPath = OutputDir; if (!Path.IsPathRooted(OutputDir)) outputDirPath = Path.Combine(oldDir, OutputDir); var fdoGenerate = new FdoGenerateImpl(doc, outputDirPath) { Overrides = handGeneratedClasses, IntPropTypeOverrides = intPropTypeOverridesClasses }; string outputPath = OutputFile; if (!Path.IsPathRooted(outputPath)) outputPath = Path.Combine(outputDirPath, OutputFile); // Generate the main code. if (Path.GetDirectoryName(TemplateFile).Length > 0) Directory.SetCurrentDirectory(Path.GetDirectoryName(TemplateFile)); fdoGenerate.SetOutput(outputPath); fdoGenerate.Process(Path.GetFileName(TemplateFile)); // Generate flat DB SQL for SqlServer. // 'Flat' here means one table per concrete class in the model. //fdoGenerate.SetOutput("BootstrapFlatlandSqlServerDB.sql"); //fdoGenerate.Process("FlatlandSqlServer.vm.sql"); // Generate flat DB SQL for Firebird. // 'Flat' here means one table per concrete class in the model. //fdoGenerate.SetOutput("BootstrapFlatlandFirebirdDB.sql"); //fdoGenerate.Process("FlatlandFirebird.vm.sql"); // Generate the backend provider(s) code. if (!string.IsNullOrEmpty(BackendTemplateFiles)) { foreach (var backendDir in BackendTemplateFiles.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { var beDir = backendDir.Trim(); if (beDir == string.Empty) continue; var curDir = Path.Combine(Path.Combine(OutputDir, "FDOGenerate"), beDir); Directory.SetCurrentDirectory(curDir); fdoGenerate.SetOutput(Path.Combine(beDir, beDir + @"Generated.cs")); fdoGenerate.Process("Main" + beDir + ".vm.cs"); } } // Restore original directory. Directory.SetCurrentDirectory(originalCurrentDirectory); } catch (Exception e) { Log.LogMessage(MessageImportance.High, "Error processing template" + " " + e.Message); return false; } } finally { Directory.SetCurrentDirectory(origDir); } return true; }