/// <summary> /// 创建“编辑”文件与“生成”文件 /// </summary> /// <typeparam name="TGenerate"></typeparam> /// <typeparam name="TEditable"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="entityContext"></param> /// <param name="codeTemplate"></param> /// <param name="getGeneratedNamegFun"></param> /// <param name="getEditableNameFun"></param> /// <param name="rootDirectory"></param> public static void CreateMultipleEntityClass <TGenerate, TEditable, TEntity, TProperty>(EntityContext <TEntity, TProperty> entityContext, CodeTemplate codeTemplate, Func <TEntity, string> getGeneratedNamegFun, Func <TEntity, string> getEditableNameFun, string rootDirectory) where TGenerate : CodeTemplate, new() where TEditable : CodeTemplate, new() where TProperty : Property where TEntity : Entity <TProperty> { TGenerate generatedClass = codeTemplate.Create <TGenerate>(); codeTemplate.CopyPropertiesTo(generatedClass); TEditable editableClass = codeTemplate.Create <TEditable>(); codeTemplate.CopyPropertiesTo(editableClass); //创建根目录 rootDirectory = Path.GetFullPath(rootDirectory); if (!Directory.Exists(rootDirectory)) { Directory.CreateDirectory(rootDirectory); } foreach (TEntity entity in entityContext.Entities) { string editableFile = Path.Combine(rootDirectory, getEditableNameFun(entity)); if (!File.Exists(editableFile)) { editableClass.SetProperty("Entity", entity); codeTemplate.Response.WriteLine(editableFile); editableClass.RenderToFile(editableFile, true); } string generatedFile = Path.Combine(rootDirectory, getGeneratedNamegFun(entity)); generatedClass.SetProperty("Entity", entity); codeTemplate.Response.WriteLine(generatedFile); generatedClass.RenderToFile(generatedFile, editableFile, true); } }
public static void CreateEntityClass <T, TEntity, TProperty>(EntityContext <TEntity, TProperty> entityContext, CodeTemplate codeTemplate, Func <TEntity, string> getNamegFun, string rootDirectory, bool overwrite = true) where T : CodeTemplate, new() where TEntity : Entity <TProperty> where TProperty : Property { T classTemplate = codeTemplate.Create <T>(); codeTemplate.CopyPropertiesTo(classTemplate); //创建根目录 rootDirectory = Path.GetFullPath(rootDirectory); if (!Directory.Exists(rootDirectory)) { Directory.CreateDirectory(rootDirectory); } foreach (TEntity entity in entityContext.Entities) { string editableFile = Path.Combine(rootDirectory, getNamegFun(entity)); classTemplate.SetProperty("Entity", entity); if (overwrite) { codeTemplate.Response.WriteLine(editableFile); classTemplate.RenderToFile(editableFile, true); } else { if (File.Exists(editableFile)) { continue; } codeTemplate.Response.WriteLine(editableFile); classTemplate.RenderToFile(editableFile, true); } } }
public static void BuildSubTemplate(CodeTemplate parent, CodeTemplate template, IMergeStrategy strategy, string pathAndFile, string outputDir) { // instantiate the sub-template parent.Response.WriteLine(string.Format("Begin Build SubTemplate for file {0}...",Path.GetFileName(pathAndFile))); // Set up the DL project parent.CopyPropertiesTo(template); //Render the file if(strategy == null) template.RenderToFile(Path.Combine(outputDir,pathAndFile), true); else template.RenderToFile(Path.Combine(outputDir,pathAndFile), strategy); parent.Response.WriteLine(string.Format("Build of {0} Complete.",Path.GetFileName(pathAndFile))); }