Exemple #1
0
        public void GenerateCode()
        {
            VelocityEngine velocity = new VelocityEngine();

            TextReader reader   = new StreamReader(NvelocityUtil.GetTemplateFolderPath() + "\\CustomItem.base.vm");
            string     template = reader.ReadToEnd();

            //Setup the template with the needed code, and then do the merge
            VelocityContext baseContext = new VelocityContext();

            baseContext.Put("Usings", CustomItemInformation.Usings);
            baseContext.Put("BaseTemplates", CustomItemInformation.BaseTemplates);
            baseContext.Put("CustomItemFields", CustomItemInformation.Fields);
            baseContext.Put("CustomItemInformation", CustomItemInformation);

            //Get the full file path to the .base.cs file
            string filePath = FileUtil.GetClassFilePath(CustomItemInformation.ClassName,
                                                        CustomItemInformation.FolderPathProvider.GetFolderPath(
                                                            CustomItemInformation.Template, CustomItemInformation.BaseFileRoot));


            //Build the folder strucutre so that we have a place to put the .base.cs file
            BuildFolderStructure(CustomItemInformation);

            //Write the .base.cs file
            if (GenerateBaseFile)
            {
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    //TODO add error checking
                    Velocity.Init();
                    sw.Write(Sitecore.Text.NVelocity.VelocityHelper.Evaluate(baseContext, template, "base-custom-item"));
                    GenerationMessage += filePath + " successfully written\n\n";
                    GeneratedFilePaths.Add(filePath);
                }
            }

            //Write out the other partial files
            OuputPartialFiles(velocity);
        }
Exemple #2
0
        /// <summary>
        /// Ouputs the partial class files for a custom item.
        /// </summary>
        /// <param name="velocity">The velocity.</param>
        private void OuputPartialFiles(VelocityEngine velocity)
        {
            StringWriter writer;
            TextReader   reader     = new StreamReader(NvelocityUtil.GetTemplateFolderPath() + "\\CustomItem.partial.vm");
            string       template   = reader.ReadToEnd();
            string       folderPath = CustomItemInformation.FolderPathProvider.GetFolderPath(CustomItemInformation.Template,
                                                                                             CustomItemInformation.BaseFileRoot);

            VelocityContext partialContext = new VelocityContext();

            partialContext.Put("CustomItemInformation", CustomItemInformation);

            writer = new StringWriter();
            writer.Write(Sitecore.Text.NVelocity.VelocityHelper.Evaluate(partialContext, template, "base-custom-item"));

            //.instance.cs
            if (GenerateInstanceFile)
            {
                //Only create the file if it does not already exist
                string instanceFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".instance.cs";
                OutputFileIfDoesNotExist(instanceFilePath, writer);
            }

            //.interface.cs
            if (GenerateInterfaceFile)
            {
                //Only create the file if it does not already exist
                string interfaceFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".interface.cs";
                OutputFileIfDoesNotExist(interfaceFilePath, writer);
            }

            //.static.cs
            if (GenerateStaticFile)
            {
                //Only create the file if it does not already exist
                string staticFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".static.cs";
                OutputFileIfDoesNotExist(staticFilePath, writer);
            }
        }