/// <summary> /// 单个表的模板生成 /// </summary> /// <param name="fname">模板文件</param> /// <param name="dir">生成目录</param> /// <param name="table">表</param> /// <returns>生成的文件路径</returns> public static string Render(string fname, string dir, Model.TableSchema table) { string fallName = string.Empty; if (fname.Contains(':')) { fallName = fname; } else { fallName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fname); } if (string.IsNullOrWhiteSpace(dir)) { dir = AppDomain.CurrentDomain.BaseDirectory + "\\MultipleFile"; } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string template = File.ReadAllText(fallName); string result = RenderTableLoop(template, table); string path = Path.Combine(dir, table.Name + ".cs"); File.WriteAllText(path, result); return(path); }
/// <summary> /// 单个表的模板生成 /// </summary> /// <param name="fname">模板文件</param> /// <param name="odir">生成目录</param> /// <param name="table">表</param> /// <returns>生成的文件路径</returns> public static void Render(string template_file, string output_file, Model.TableSchema table) { string template = File.ReadAllText(template_file); string result = RenderTableLoop(template, table); FileInfo finfo = new FileInfo(output_file); if (!Directory.Exists(finfo.DirectoryName)) { Directory.CreateDirectory(finfo.DirectoryName); } File.WriteAllText(output_file, result, new System.Text.UTF8Encoding(true)); }
private static string RenderTableLoop(string loop_table_template, Model.TableSchema table) { var match = regex_loop_field.Match(loop_table_template); string result = loop_table_template; while (match.Success) { result = result.Replace(match.Groups[0].Value, RenderFieldLoop(match.Groups[1].Value, table.Columns)); match = match.NextMatch(); } result = result.Replace("@{tablename}", table.Name); result = result.Replace("@{description}", table.Description); return(result); }