Beispiel #1
0
        /// <summary>
        /// 生成运行代码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string GenerateDto(GeneratePageInputDto input)
        {
            var targetBaseFolder   = Path.Combine($"GeneratedDtos/{DateTime.Now.ToString("yyyyMMdd")}");
            var targetSubFolder    = string.Empty;
            var targetFile         = string.Empty;
            var targetFilePath     = string.Empty;
            var renderedString     = string.Empty;
            var relativeFolderPath = string.Empty;

            if (!Directory.Exists(targetBaseFolder))
            {
                Directory.CreateDirectory(targetBaseFolder);
            }

            //清空目录
            foreach (var file in Directory.GetFiles(targetBaseFolder))
            {
                File.Delete(file);
            }

            #region Application/EntityDto
            relativeFolderPath = Path.Combine(targetBaseFolder);
            targetFile         = $"{input.TableName}Dto.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Application/Dtos/Template_EntityDto", new EntityDtoViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            #endregion

            return(renderedString);
        }
Beispiel #2
0
        /// <summary>
        /// 生成运行代码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string GeneratePage(GeneratePageInputDto input)
        {
            var targetBaseFolder   = Path.Combine($"GeneratedCodes/{DateTime.Now.ToString("yyyyMMdd")}");
            var targetSubFolder    = string.Empty;
            var targetFile         = string.Empty;
            var targetFilePath     = string.Empty;
            var renderedString     = string.Empty;
            var relativeFolderPath = string.Empty;

            if (!Directory.Exists(targetBaseFolder))
            {
                Directory.CreateDirectory(targetBaseFolder);
            }

            //清空目录
            foreach (var file in Directory.GetFiles(targetBaseFolder, "*", SearchOption.AllDirectories))
            {
                File.Delete(file);
            }

            #region Readme.txt
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"Readme.txt";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Readme", new ReadmeViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;

            #endregion

            #region Application/EntityDto
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Application/{input.ModuleName}/Dtos");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"{input.EntityName}Dto.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Application/Dtos/Template_EntityDto", new EntityDtoViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;

            #endregion

            #region Application/EntityInputDto
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Application/{input.ModuleName}/Dtos");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"{input.EntityName}InputDto.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Application/Dtos/Template_EntityInputDto", new EntityInputDtoViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;

            #endregion

            #region Application/IAppService
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Application/{input.ModuleName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"I{ input.EntityName}AppService.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Application/Template_IAppService", new IAppServiceViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Application/AppService
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Application/{input.ModuleName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"{input.EntityName}AppService.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Application/Template_AppService", new AppServiceViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/Controller
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Controllers/{input.ModuleName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"{input.EntityName}Controller.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/Controller/Template_Controller", new ControllerViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/ViewModel
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Models/{input.ModuleName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = $"{input.EntityName}ViewModel.cs";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/ViewModel/Template_ViewModel", new ViewModelViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/CreateOrUpdateModal.cshtml
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Views/{input.ModuleName}/{input.PageName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = "CreateOrUpdateModal.cshtml";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/Views/Template_CreateOrUpdateModalCshtml", new CreateOrUpdateModalCshtmlViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/CreateOrUpdateModal.js
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Views/{input.ModuleName}/{input.PageName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = "CreateOrUpdateModal.js";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/Views/Template_CreateOrUpdateModalJs", new CreateOrUpdateModalJsViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/Index.cshtml
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Views/{input.ModuleName}/{input.PageName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = "Index.cshtml";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/Views/Template_IndexCshtml", new IndexCshtmlViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region Web/Index.js
            targetSubFolder    = Path.Combine($"{input.ProjectName}.Web/Views/{input.ModuleName}/{input.PageName}");
            relativeFolderPath = Path.Combine(targetBaseFolder, targetSubFolder);
            targetFile         = "Index.js";

            if (!Directory.Exists(relativeFolderPath))
            {
                Directory.CreateDirectory(relativeFolderPath);
            }

            if (File.Exists(Path.Combine(relativeFolderPath, targetFile)))
            {
                File.Delete(Path.Combine(relativeFolderPath, targetFile));
            }

            using (File.Create(Path.Combine(relativeFolderPath, targetFile)))
            {
            }

            renderedString = this.RenderToStringAsync("CPS8x/Templates/Web/Views/Template_IndexJs", new IndexJsViewModel()
            {
                ProjectName          = input.ProjectName,
                ModuleName           = input.ModuleName,
                PageName             = input.PageName,
                EntityName           = input.EntityName,
                EntityNamespace      = input.EntityNamespace,
                EntityPrimaryKeyType = input.EntityPrimaryKeyType,
                TableName            = input.TableName,
                Sorting          = input.EntitySortingColumnName,
                ColumnConfigList = input.ColumnConfigList
            }).Result;

            targetFilePath = Path.Combine(relativeFolderPath, targetFile);

            File.WriteAllText(targetFilePath, renderedString, Encoding.UTF8);

            renderedString = string.Empty;
            #endregion

            #region 压缩生成的文件
            //清空原有文件
            var zipFilePath = Path.Combine(targetBaseFolder, $"{DateTime.Now.ToString("yyyyMMdd")}.zip");

            if (File.Exists(zipFilePath))
            {
                File.Delete(zipFilePath);
            }

            var zip = new ZipHelper();

            zip.ZipFileFromDirectory(targetBaseFolder, zipFilePath, 5);
            #endregion

            return(zipFilePath);
        }