/// <summary> /// Outputs a file if does not already exist. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="fileContents">The file contents.</param> private bool OutputFileIfDoesNotExist(string filePath, StringWriter fileContent) { //Only create the file if it does not already exist if (File.Exists(filePath)) { GenerationMessage += filePath + " already exists\n\n"; return(false); } using (StreamWriter sw = new StreamWriter(filePath)) { try { sw.Write(fileContent.GetStringBuilder().ToString()); } catch (Exception e) { GenerationMessage += filePath + " writing failed : " + e.Message + "\n\n"; return(false); } } GeneratedFilePaths.Add(filePath); GenerationMessage += filePath + " successfully written\n\n"; return(true); }
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); }