Пример #1
0
 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();
     }
 }
        public static GeneratorCodeReplaceInfo Create(SolutionInfoModel solutionInfo, BasicOptionCfg option,
                                                      EntityModel entity)
        {
            GeneratorCodeReplaceInfo generatorCodeReplaceInfo = new GeneratorCodeReplaceInfo();

            generatorCodeReplaceInfo.Multi_Language          = new List <TranslatorTextList>();
            generatorCodeReplaceInfo.SolutionNamespace       = solutionInfo.SolutionNamespace;
            generatorCodeReplaceInfo.CompanyNamespace        = solutionInfo.CompanyNamespace;
            generatorCodeReplaceInfo.BaseClassDtoReplacement = ": " + entity.BaseClassDtoName;
            if (generatorCodeReplaceInfo.BaseClassDtoReplacement == ": ")
            {
                generatorCodeReplaceInfo.BaseClassDtoReplacement = string.Empty;
            }
            else if (!string.IsNullOrEmpty(entity.BaseClassNameList))
            {
                generatorCodeReplaceInfo.BaseClassDtoReplacement =
                    generatorCodeReplaceInfo.BaseClassDtoReplacement + "," + entity.BaseClassNameList;
            }

            if (option.UseDomainManagerCode || option.IsAllGeneratorCode)
            {
                generatorCodeReplaceInfo.DomainServicesNamespace   = "using " + entity.Namespace + ".DomainService;";
                generatorCodeReplaceInfo.PrivateIEntityNameManager = string.Concat(new string[]
                {
                    "private readonly I",
                    entity.Name,
                    "Manager _",
                    entity.LowerName,
                    "Manager;"
                });
                generatorCodeReplaceInfo.IEntityNameManager = string.Concat(new string[]
                {
                    ",I",
                    entity.Name,
                    "Manager ",
                    entity.LowerName,
                    "Manager"
                });
                generatorCodeReplaceInfo.EntityNameManager = string.Concat(new string[]
                {
                    "_",
                    entity.LowerName,
                    "Manager = ",
                    entity.LowerName,
                    "Manager;"
                });
            }

            if (option.UseDomainAuthorizeCode || option.IsAllGeneratorCode)
            {
                generatorCodeReplaceInfo.DomainAuthorizationNamespace = "using " + entity.Namespace + ".Authorization;";
                generatorCodeReplaceInfo.PermissionNode = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Node)]"
                });
                generatorCodeReplaceInfo.PermissionQuery = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Query)]"
                });
                generatorCodeReplaceInfo.PermissionCreate = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Create)]"
                });
                generatorCodeReplaceInfo.PermissionEdit = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Edit)]"
                });
                generatorCodeReplaceInfo.PermissionDelete = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Delete)]"
                });
                generatorCodeReplaceInfo.PermissionBatchDelete = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "BatchDelete)]"
                });
                generatorCodeReplaceInfo.PermissionExportExcel = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "ExportExcel)]"
                });
                generatorCodeReplaceInfo.EntityCreateOrEditPermission = string.Concat(new string[]
                {
                    "[AbpAuthorize(",
                    entity.Name,
                    "Permissions.",
                    "Create, ",
                    entity.Name,
                    "Permissions.",
                    "Edit)]"
                });
            }

            return(generatorCodeReplaceInfo);
        }
Пример #3
0
        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();
            }
        }