private void GenerateAccessoryFile(string fileName, string templateFile, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { // Create Inheritance file if it does not exist if (!File.Exists(fileName)) //&& objInfo.ObjectType != CslaObjectType.NameValueList) { // string tPath = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + "\\InheritFromBase.cst"; string tPath = _fullTemplatesPath + objInfo.OutputLanguage + templateFile; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("ActiveObjects", activeObjects); template.SetProperty("CurrentUnit", unit); FileStream fs = File.Open(fileName, FileMode.Create); OnGenerationFileName(fileName); StreamWriter sw = new StreamWriter(fs); try { template.Render(sw); } catch (Exception e) { ShowExceptionInformation(e); } finally { sw.Close(); } } } }
public static void Main() { string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\StoredProcedures.cst"); var engine = new TemplateEngine(new DefaultEngineHost(System.IO.Path.GetDirectoryName(path))); CompileTemplateResult result = engine.Compile(path); if (result.Errors.Count == 0) { var database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=.;Database=PetShop;Integrated Security=True;"); TableSchema table = database.Tables["Inventory"]; CodeTemplate template = result.CreateTemplateInstance(); template.SetProperty("SourceTable", table); template.SetProperty("IncludeDrop", false); template.SetProperty("InsertPrefix", "Insert"); template.Render(Console.Out); } else { foreach (var error in result.Errors) { Console.Error.WriteLine(error.ToString()); } } Console.WriteLine("\r\nPress any key to continue."); Console.ReadKey(); }
private string GenerateProcedure(CslaObjectInfo objInfo, Criteria crit, string templateName, string sprocName) { if (objInfo != null) { try { if (templateName != String.Empty) { string path = _templatesDirectory + @"sprocs\" + templateName; CodeTemplate template = GetTemplate(objInfo, path); if (crit != null) { template.SetProperty("Criteria", crit); } template.SetProperty("IncludeParentProperties", objInfo.DataSetLoadingScheme); if (template != null) { using (StringWriter sw = new StringWriter()) { template.Render(sw); sprocSuccess++; return(sw.ToString()); } } } } catch (Exception e) { sprocFailed++; throw (new Exception("Error generating " + GetFileNameWithoutExtension(templateName) + ": " + sprocName, e)); } } return(String.Empty); }
void GenerateInheritanceFile(string fileName, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { // Create Inheritance file if it does not exist if (!File.Exists(fileName)) //&& objInfo.ObjectType != CslaObjectType.NameValueList) { string tPath = this._fullTemplatesPath + unit.GenerationParams.OutputLanguage.ToString() + "\\InheritFromBase.cst"; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("ActiveObjects", activeObjects); template.SetProperty("CurrentUnit", unit); FileInfo fi = new FileInfo(fileName); using (FileStream fs = fi.Open(FileMode.Create)) { OnGenerationFileName(fi.FullName); using (StreamWriter sw = new StreamWriter(fs)) { try { template.Render(sw); } catch (Exception e) { ShowExceptionInformation(e); } } } } } }
/// <summary> /// 输出其它模块内容 /// </summary> /// <param name="isRender"></param> /// <param name="templatePath"></param> public void RenderOtherTemplate(bool isRender, string templatePath) { if (isRender) { CodeTemplate template = GetCodeTemplate(templatePath); template.Render(this.Response); } }
public void OutputTemplate(CodeTemplate template) { this.CopyPropertiesTo(template); template.Render(this.Response); }
private void GenerateUtilityFile(string utilityFilename, CslaObjectInfo objInfo, TargetFramework framework, CslaGeneratorUnit unit) { _fileSuccess.Add(utilityFilename, null); // only implement this for more recent frameworks); if (framework != TargetFramework.CSLA40) { return; } // base directory of project string fullFilename = _targetDirectory + @"\"; if (unit.GenerationParams.SeparateNamespaces) { // check whether to use namespace as folder fullFilename += unit.GenerationParams.UtilitiesNamespace.Replace(".", @"\") + @"\"; } else if (!unit.GenerationParams.UtilitiesFolder.Equals(String.Empty)) { // output folder inside directory fullFilename += unit.GenerationParams.UtilitiesFolder + @"\"; } CheckDirectory(fullFilename); // filename w/o extension fullFilename += utilityFilename; // extension if (objInfo.OutputLanguage == CodeLanguage.CSharp) { fullFilename += ".cs"; } else if (objInfo.OutputLanguage == CodeLanguage.VB) { fullFilename += ".vb"; } // Create utility class file if it does not exist if (!File.Exists(fullFilename)) { string tPath = _fullTemplatesPath + objInfo.OutputLanguage + "\\" + utilityFilename + ".cst"; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("FileName", utilityFilename); template.SetProperty("CurrentUnit", unit); FileStream fs = File.Open(fullFilename, FileMode.Create); OnGenerationInformation(utilityFilename + " file:"); OnGenerationFileName(fullFilename); StreamWriter sw = new StreamWriter(fs); try { template.Render(sw); _fileSuccess[utilityFilename] = true; } catch (Exception e) { ShowExceptionInformation(e); _fileSuccess[utilityFilename] = false; } finally { sw.Close(); } } } }
private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit) { Metadata.GenerationParameters generationParams = unit.GenerationParams; string fileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false); string baseFileName = GetBaseFileName(objInfo, true, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false); string classCommentFileName = string.Empty; if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix)) { classCommentFileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, true, generationParams.SeparateClassComment); } FileStream fsBase = null; StreamWriter swBase = null; StreamWriter sw = null; try { string tPath = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo); CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { StringBuilder errorsOutput = new StringBuilder(); StringBuilder warningsOutput = new StringBuilder(); template.SetProperty("ActiveObjects", generationParams.ActiveObjects); template.SetProperty("Errors", errorsOutput); template.SetProperty("Warnings", warningsOutput); template.SetProperty("CurrentUnit", unit); template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme); if (generationParams.BackupOldSource && File.Exists(baseFileName)) { FileInfo oldFile = new FileInfo(baseFileName); if (File.Exists(baseFileName + ".old")) { File.Delete(baseFileName + ".old"); } oldFile.MoveTo(baseFileName + ".old"); } fsBase = File.Open(baseFileName, FileMode.Create); OnGenerationFileName(baseFileName); swBase = new StreamWriter(fsBase); template.Render(swBase); errorsOutput = (StringBuilder)template.GetProperty("Errors"); warningsOutput = (StringBuilder)template.GetProperty("Warnings"); if (errorsOutput.Length > 0) { objFailed++; OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput); } else { if (warningsOutput != null) { if (warningsOutput.Length > 0) { objectWarnings++; OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput); } } objSuccess++; //OnGenerationInformation("Success"); } } GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit); if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix)) { GenerateClassCommentFile(classCommentFileName, objInfo, generationParams.ActiveObjects, unit); } } catch (Exception e) { objFailed++; ShowExceptionInformation(e); } finally { if (sw != null) { sw.Close(); } if (swBase != null) { swBase.Close(); } } }
private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit) { Metadata.GenerationParameters generationParams = unit.GenerationParams; string fileName = GetFileName(objInfo, generationParams.SeparateNamespaces, generationParams.OutputLanguage); string baseFileName = GetBaseFileName(objInfo, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.UseDotDesignerFileNameConvention, generationParams.OutputLanguage); try { GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit); string tPath = this._fullTemplatesPath + generationParams.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo); CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { StringBuilder errorsOutput = new StringBuilder(); StringBuilder warningsOutput = new StringBuilder(); template.SetProperty("ActiveObjects", generationParams.ActiveObjects); template.SetProperty("Errors", errorsOutput); template.SetProperty("Warnings", warningsOutput); template.SetProperty("CurrentUnit", unit); template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme); if (generationParams.BackupOldSource && File.Exists(baseFileName)) { FileInfo oldFile = new FileInfo(baseFileName); if (File.Exists(baseFileName + ".old")) { File.Delete(baseFileName + ".old"); } oldFile.MoveTo(baseFileName + ".old"); } FileInfo fi = new FileInfo(baseFileName); using (FileStream fs = fi.Open(FileMode.Create)) { OnGenerationFileName(fi.FullName); using (StreamWriter swBase = new StreamWriter(fs)) { template.Render(swBase); errorsOutput = (StringBuilder)template.GetProperty("Errors"); warningsOutput = (StringBuilder)template.GetProperty("Warnings"); if (errorsOutput.Length > 0) { objFailed++; OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput.ToString(), 2); } else { if (warningsOutput != null) { if (warningsOutput.Length > 0) { objectWarnings++; OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput, 2); } } objSuccess++; } } } } } catch (Exception e) { objFailed++; ShowExceptionInformation(e); } }