public static void Gen() { try { ViewModel viewModel = new ViewModel { MainWindowsOptionCfg = Global.Option }; GeneratorCodeReplaceInfo replaceInfo = GeneratorCodeService.Create(Global.SolutionInfo, Global.Option, Global.Entity); ServerModel serverModel = new ServerModel { ReplaceInfo = replaceInfo, Entity = Global.Entity }; StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder2 = new StringBuilder(); foreach (EntityFieldModel entityFieldModel in serverModel.Entity.Properties) { if (entityFieldModel.EditChecked) { entityFieldModel.AppendFieldCode(stringBuilder); } if (entityFieldModel.ListChecked) { entityFieldModel.AppendFieldCode(stringBuilder2); } } serverModel.EditDtoFieldCode = stringBuilder.ToString(); serverModel.ListDtoFieldCode = stringBuilder2.ToString(); viewModel.Server = serverModel; SPAModel spaclient = new SPAModel { Entity = Global.Entity, UsePermission = Global.Option.UseDomainAuthorizeCode }; viewModel.SPAClient = spaclient; List <CodeTemplateInfo> list = CodeGen.CreateCodeTemplates(); foreach (CodeTemplateInfo codeTemplateInfo in list) { if (!File.Exists(codeTemplateInfo.BuildPath) || Global.Option.IsOverrideFile) { codeTemplateInfo.BuildCode = codeTemplateInfo.Path.GeneratorCode(viewModel, typeof(ViewModel), codeTemplateInfo.OldCustomCode); } } foreach (CodeTemplateInfo codeTemplateInfo2 in list) { if (!File.Exists(codeTemplateInfo2.BuildPath) || Global.Option.IsOverrideFile) { codeTemplateInfo2.BuildPath.CreateFile(codeTemplateInfo2.BuildCode); } } "所有代码已经生成完毕,第一次使用可阅读生成的Readme.md文件!".InfoMsg(); } catch (Exception ex) { ("代码生成出错! \r\n" + ex.ToString()).ErrorMsg(); } }
private static void Gen() { try { ViewModel viewModel = new ViewModel { MainWindowsOptionCfg = Global.Option }; GeneratorCodeReplaceInfo replaceInfo = GeneratorCodeServiceExtension.Create(Global.SolutionInfo, Global.Option, Global.Entity); ServerModel serverModel = new ServerModel { ReplaceInfo = replaceInfo, Entity = Global.Entity }; StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder2 = new StringBuilder(); foreach (EntityFieldModel entityFieldModel in serverModel.Entity.Properties) { if (entityFieldModel.EditChecked) { entityFieldModel.AppendFieldCode(stringBuilder, true); } if (entityFieldModel.ListChecked) { entityFieldModel.AppendFieldCode(stringBuilder2, false); } } serverModel.EditDtoFieldCode = stringBuilder.ToString().Trim(); serverModel.ListDtoFieldCode = stringBuilder2.ToString().Trim(); viewModel.Server = serverModel; SPAModel spaclient = new SPAModel { Entity = Global.Entity, UsePermission = Global.Option.UseDomainAuthorizeCode }; viewModel.SPAClient = spaclient; List <CodeTemplateInfo> list = CreateCodeTemplates(); foreach (CodeTemplateInfo codeTemplateInfo in list) { if (!File.Exists(codeTemplateInfo.BuildPath) || Global.Option.IsOverrideFile) { //王杰 格式化代码 //codeTemplateInfo.BuildCode = codeTemplateInfo.Path.GeneratorCode(viewModel, typeof(ViewModel), codeTemplateInfo.OldCustomCode); codeTemplateInfo.BuildCode = codeTemplateInfo.Path.GeneratorCode(viewModel, typeof(ViewModel), codeTemplateInfo.OldCustomCode?.Trim())?.Trim(); } } foreach (CodeTemplateInfo codeTemplateInfo2 in list) { if (!File.Exists(codeTemplateInfo2.BuildPath) || Global.Option.IsOverrideFile) { codeTemplateInfo2.BuildPath.CreateFile(codeTemplateInfo2.BuildCode); } } //多语言 if (Global.Option.UseDomainAuthorizeCode) { var sourceCode = string.Empty; var desFile = string.Empty; foreach (CodeTemplateInfo codeTemplateInfo in list) { if (!File.Exists(codeTemplateInfo.BuildPath) || Global.Option.IsOverrideFile) { if (codeTemplateInfo.Path.EndsWith("Localization-zh.txt")) { sourceCode = codeTemplateInfo.BuildCode; desFile = Path.Combine(Global.SolutionInfo.Core.BasePath, "Localization", "SourceFiles", "json", "YoyoCmsTemplate-zh-Hans.json"); } else if (codeTemplateInfo.Path.EndsWith("Localization-en.txt")) { sourceCode = codeTemplateInfo.BuildCode; desFile = Path.Combine(Global.SolutionInfo.Core.BasePath, "Localization", "SourceFiles", "json", "YoyoCmsTemplate.json"); } if (!string.IsNullOrEmpty(sourceCode) && File.Exists(desFile)) { var dicSource = JsonConvert.DeserializeObject <Dictionary <string, string> >(sourceCode); //JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(File.ReadAllText(desFile, Encoding.UTF8))); //var json = new JsonSerializer().Deserialize(jsonTextReader); //var localization = JsonConvert.DeserializeObject<Localization>(json.ToString()); var localization = JsonConvert.DeserializeObject <Localization>(File.ReadAllText(desFile, Encoding.UTF8)); var dicDes = localization.Texts; foreach (var item in dicSource) { if (dicDes.ContainsKey(item.Key)) { dicDes[item.Key] = item.Value; } else { dicDes.Add(item.Key, item.Value); } } var content = JsonConvert.SerializeObject(localization, (Newtonsoft.Json.Formatting)Formatting.Indented); File.WriteAllText(desFile, content); //using (FileStream fileStream = new FileStream(desFile, FileMode.Open, FileAccess.Write)) //{ // byte[] bytes = Encoding.UTF8.GetBytes(content); // fileStream.Write(bytes, 0, bytes.Length); //} } } } } "所有代码已经生成完毕,第一次使用可阅读生成的Readme.md文件!".InfoMsg(); } catch (Exception ex) { ("代码生成出错! \r\n" + ex.ToString()).ErrorMsg(); } }