private static void GenTSCode(TableMeta pTableMeta, string pPath, ExportCfgType pCfgType) { var _Path = Path.Combine(pPath, pTableMeta.ClassName + "Ins" + ".ts"); using (var fs = new FileStream(_Path, FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine("/*"); sw.WriteLine("* 此类由ConfigTools自动生成. 不要手动修改!"); sw.WriteLine("*/"); sw.WriteLine("export class {0}", pTableMeta.ClassName + "Ins"); sw.WriteLine("{"); sw.WriteLine(" public static Ins:{0}", pTableMeta.ClassName + "Ins"); sw.WriteLine(" public static Load(json:any)"); sw.WriteLine(" {"); sw.WriteLine(" this.Ins = json as {0}", pTableMeta.ClassName + "Ins"); sw.WriteLine(" }"); sw.WriteLine(""); if (pTableMeta.CheckTypeIsMap()) { sw.WriteLine(" public mDataMap:{{ [key:string] :{0} }};", pTableMeta.DataName); } else { sw.WriteLine(" public mDataList:{0}[];", pTableMeta.DataName); } sw.WriteLine("}"); sw.WriteLine(); sw.WriteLine("export class {0}", pTableMeta.DataName); sw.WriteLine("{"); foreach (var field in pTableMeta.Fields) { if (!field.IsExportField(pCfgType)) { continue; } if (!string.IsNullOrWhiteSpace(field.mSpostil)) { sw.WriteLine(" /*"); sw.WriteLine(" " + field.mSpostil); sw.WriteLine(" */"); } sw.WriteLine(" " + field.mComment); sw.WriteLine(" public {0}:{1};", field.mFieldName, field.GetFieldTypeName(ExportCodeType.TypeScript)); } sw.WriteLine("}"); } } }
public static void GenCode(TableMeta pTableMeta, string pPath, ExportCodeType pType, ExportCfgType pCfgType) { if (pType == ExportCodeType.CSharp) { GenCSCode(pTableMeta, pPath, pCfgType); } else if (pType == ExportCodeType.TypeScript) { GenTSCode(pTableMeta, pPath, pCfgType); } }
private static void GenCSCode(TableMeta pTableMeta, string pPath, ExportCfgType pCfgType) { var _Path = Path.Combine(pPath, pTableMeta.ClassName + ".cs"); using (var fs = new FileStream(_Path, FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine("/*"); sw.WriteLine("* 此类由ConfigTools自动生成. 不要手动修改!"); sw.WriteLine("*/"); sw.WriteLine("using System.Collections.Generic;"); sw.WriteLine(""); sw.WriteLine("namespace Configs"); sw.WriteLine("{"); sw.WriteLine(" public class {0}", pTableMeta.ClassName); sw.WriteLine(" {"); if (pTableMeta.CheckTypeIsMap()) { sw.WriteLine(" public Dictionary<string, {0}> mDataMap;", pTableMeta.DataName); } else { sw.WriteLine(" public List<{0}> mDataList;", pTableMeta.DataName); } sw.WriteLine(" }"); sw.WriteLine(); sw.WriteLine(" public class {0}", pTableMeta.DataName); sw.WriteLine(" {"); foreach (var field in pTableMeta.Fields) { if (!field.IsExportField(pCfgType)) { continue; } if (!string.IsNullOrWhiteSpace(field.mSpostil)) { sw.WriteLine(" /*"); sw.WriteLine(" " + field.mSpostil); sw.WriteLine(" */"); } sw.WriteLine(" " + field.mComment); sw.WriteLine(" public {0} {1};", field.GetFieldTypeName(ExportCodeType.CSharp), field.mFieldName); sw.WriteLine(); } sw.WriteLine(" }"); sw.WriteLine("}"); } } }
private static void GenTSCode(TableMeta pTableMeta, string pPath, ExportCfgType pCfgType) { var _Path = Path.Combine(pPath, pTableMeta.ClassName + ".ts"); using (var fs = new FileStream(_Path, FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine("/*"); sw.WriteLine("* 此类由ConfigTools自动生成. https://github.com/huangkumao/ConfigTools"); sw.WriteLine("*/"); sw.WriteLine("namespace Configs"); sw.WriteLine("{"); sw.WriteLine(" export class {0}", pTableMeta.ClassName); sw.WriteLine(" {"); if (pTableMeta.CheckTypeIsMap()) { sw.WriteLine(" public mDataMap:{{ [key:string] :{0} }};", pTableMeta.DataName); } else { sw.WriteLine(" public mDataList:{0}[];", pTableMeta.DataName); } sw.WriteLine(" }"); sw.WriteLine(); sw.WriteLine(" export class {0}", pTableMeta.DataName); sw.WriteLine(" {"); foreach (var field in pTableMeta.Fields) { if (!field.IsExportField(pCfgType)) { continue; } sw.WriteLine(" " + field.mComment); sw.WriteLine(" public {0}:{1};", field.mFieldName, field.GetFieldTypeName(ExportCodeType.TypeScript)); } sw.WriteLine(" }"); sw.WriteLine("}"); } } }
public static TableMeta ParseTableMeta(string pFileName, DataTable pDT, ExportCfgType pExpType) { var meta = new TableMeta { TableName = pFileName }; for (var i = 0; i < pDT.Columns.Count; i++) { var field = new TableField { mSpostil = pDT.Rows[sPostilRow].ItemArray[i] + "", mComment = @"//" + pDT.Rows[sCommentRow].ItemArray[i], mFieldName = pDT.Rows[sFieldRow].ItemArray[i].ToString(), mTypeName = pDT.Rows[sTypeRow].ItemArray[i].ToString(), mExportType = pDT.Rows[sExportRow].ItemArray[i].ToString() }; meta.Fields.Add(field); } return(meta); }
public static string JsonDataName_2 = " \"mDataList\":["; //以List形式保存 public static void GenCfg(DataTable pDT, string pPath, TableMeta pTableMeta, ExportCfgType pCfgType) { var _Path = Path.Combine(pPath, pTableMeta.ClassName + ".json"); var isMap = pTableMeta.CheckTypeIsMap(); //只有第一个字段是"ID"才会以Map形式保存 using (var fs = new FileStream(_Path, FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine(JsonHead); sw.WriteLine(isMap ? JsonDataName_1 : JsonDataName_2); for (var iRow = 4; iRow < pDT.Rows.Count; iRow++) { if (isMap) { sw.Write(" \"{0}\":", pDT.Rows[iRow].ItemArray[0]); } sw.WriteLine(JsonDataBegin); for (var iCol = 0; iCol < pDT.Columns.Count; iCol++) { if (!pTableMeta.Fields[iCol].IsExportField(pCfgType)) { continue; } if (iCol != 0) { sw.WriteLine(","); } sw.Write(" \"{0}\":", pTableMeta.Fields[iCol].mFieldName); var _D = pDT.Rows[iRow].ItemArray[iCol].ToString(); if (pTableMeta.Fields[iCol].mTypeName == "int") { sw.Write(ParseInt(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "float") { sw.Write(ParseFloat(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "string") { sw.Write(ParseString(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "bool") { sw.Write(ParseBool(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "int+") { sw.Write(ParseIntList(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "float+") { sw.Write(ParseFloatList(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "string+") { sw.Write(ParseStringList(_D)); } } sw.WriteLine(); sw.Write(JsonDataEnd); if (iRow != pDT.Rows.Count - 1) { sw.WriteLine(","); } else { sw.WriteLine(); } } sw.WriteLine(isMap ? " }" : " ]"); sw.WriteLine(JsonTail); } } }
public static string JsonDataName_2 = " \"mDataList\":["; //以List形式保存 public static void GenCfg(DataTable pDT, string pPath, TableMeta pTableMeta, ExportCfgType pCfgType) { UTF8Encoding utf8 = new UTF8Encoding(false); var _Path = Path.Combine(pPath, pTableMeta.ClassName + ".json"); var isMap = pTableMeta.CheckTypeIsMap(); //只有第一个字段是"ID"才会以Map形式保存 using (var fs = new FileStream(_Path, FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(fs, utf8)) { sw.WriteLine(JsonHead); sw.WriteLine(isMap ? JsonDataName_1 : JsonDataName_2); for (var iRow = 5; iRow < pDT.Rows.Count; iRow++) { if (isMap) { sw.Write(" \"{0}\":", pDT.Rows[iRow].ItemArray[0]); } sw.WriteLine(JsonDataBegin); for (var iCol = 0; iCol < pDT.Columns.Count; iCol++) { if (!pTableMeta.Fields[iCol].IsExportField(pCfgType)) { continue; } if (iCol != 0) { sw.WriteLine(","); } sw.Write(" \"{0}\":", pTableMeta.Fields[iCol].mFieldName); var _D = pDT.Rows[iRow].ItemArray[iCol].ToString(); if (iCol == 0 && string.IsNullOrWhiteSpace(_D)) { throw new Exception("ID cant be null"); } if (pTableMeta.Fields[iCol].mTypeName == "int") { sw.Write(ParseInt(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "float") { sw.Write(ParseFloat(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "string") { sw.Write(ParseString(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "bool") { sw.Write(ParseBool(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "int+") { sw.Write(ParseIntList(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "float+") { sw.Write(ParseFloatList(_D)); } else if (pTableMeta.Fields[iCol].mTypeName == "string+") { sw.Write(ParseStringList(_D)); } } sw.WriteLine(); sw.Write(JsonDataEnd); if (iRow != pDT.Rows.Count - 1) { sw.WriteLine(","); } else { sw.WriteLine(); } } sw.WriteLine(isMap ? " }" : " ]"); sw.WriteLine(JsonTail); } } //是否压缩 if (Main.sNeedCom) { var _AllCon = File.ReadAllText(_Path); _AllCon = _AllCon.Trim().Replace(" ", "").Replace("\n", "").Replace("\r", ""); File.WriteAllText(_Path, _AllCon); } }