예제 #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="LcmGenerateImpl"/> 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 LcmGenerateImpl(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("lcmgenerate", this);
            m_Context.Put("model", m_Model);

            RuntimeSingleton.RuntimeServices.SetApplicationAttribute("LcmGenerate.Engine", m_Engine);
            RuntimeSingleton.RuntimeServices.SetApplicationAttribute("LcmGenerate.Context", m_Context);
        }
예제 #2
0
        /// ------------------------------------------------------------------------------------
        /// <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("LcmGenerate", "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("LcmGenerate", "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 lcmGenerate = new LcmGenerateImpl(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));
                    }
                    lcmGenerate.SetOutput(outputPath);
                    lcmGenerate.Process(Path.GetFileName(TemplateFile));

                    // 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, "LcmGenerate"), beDir);
                            Directory.SetCurrentDirectory(curDir);
                            lcmGenerate.SetOutput(Path.Combine(beDir, beDir + @"Generated.cs"));
                            lcmGenerate.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);
        }