public async Task <IResultModel <ProjectBuildCodeResultModel> > BuildCode(Guid projectId, IList <ClassEntity> classList = null) { var result = new ResultModel <ProjectBuildCodeResultModel>(); var project = await _repository.GetAsync(projectId); if (project == null) { return(result.Failed("项目不存在")); } //创建项目生成对象 var projectBuildModel = _mapper.Map <ProjectBuildModel>(project); projectBuildModel.Prefix = _codeGeneratorOptions.Prefix; var id = Guid.NewGuid().ToString(); var rootPath = Path.Combine(_commonOptions.TempPath, _codeGeneratorOptions.BuildCodePath); var buildModel = new TemplateBuildModel { RootPath = Path.Combine(rootPath, id), }; if (classList == null) { classList = await _classRepository.QueryAllByProject(project.Id); } foreach (var classEntity in classList) { var classBuildModel = _mapper.Map <ClassBuildModel>(classEntity); var propertyList = await _propertyRepository.QueryByClass(classEntity.Id); if (propertyList.Any()) { //查询属性 foreach (var propertyEntity in propertyList) { var propertyBuildModel = _mapper.Map <PropertyBuildModel>(propertyEntity); //如果属性类型是枚举,查询枚举信息 if (propertyEntity.Type == PropertyType.Enum && propertyEntity.EnumId.NotEmpty()) { var enumEntity = await _enumRepository.GetAsync(propertyEntity.EnumId); propertyBuildModel.Enum = new EnumBuildModel { Name = enumEntity.Name, Remarks = enumEntity.Remarks }; var enumItemList = await _enumItemRepository.QueryByEnum(propertyEntity.EnumId); propertyBuildModel.Enum.ItemList = enumItemList.Select(m => new EnumItemBuildModel { Name = m.Name, Remarks = m.Remarks, Value = m.Value }).ToList(); } classBuildModel.PropertyList.Add(propertyBuildModel); } } var modelPropertyList = await _modelPropertyRepository.QueryByClass(classEntity.Id); if (modelPropertyList.Any()) { foreach (var propertyEntity in modelPropertyList) { var modelPropertyBuildModel = _mapper.Map <ModelPropertyBuildModel>(propertyEntity); //如果属性类型是枚举,查询枚举信息 if (propertyEntity.Type == PropertyType.Enum && propertyEntity.EnumId.NotEmpty()) { var enumEntity = await _enumRepository.GetAsync(propertyEntity.EnumId); modelPropertyBuildModel.Enum = new EnumBuildModel { Name = enumEntity.Name, Remarks = enumEntity.Remarks }; var enumItemList = await _enumItemRepository.QueryByEnum(propertyEntity.EnumId); modelPropertyBuildModel.Enum.ItemList = enumItemList.Select(m => new EnumItemBuildModel { Name = m.Name, Remarks = m.Remarks, Value = m.Value }).ToList(); } classBuildModel.ModelPropertyList.Add(modelPropertyBuildModel); } } classBuildModel.Method = await _classMethodRepository.GetByClass(classEntity.Id); projectBuildModel.ClassList.Add(classBuildModel); } buildModel.Project = projectBuildModel; var builder = new DefaultTemplateBuilder(); builder.Build(buildModel); ZipFile.CreateFromDirectory(buildModel.RootPath, Path.Combine(rootPath, id + ".zip")); var resultModel = new ProjectBuildCodeResultModel { Id = id, Name = projectBuildModel.Name + ".zip" }; return(result.Success(resultModel)); }
public async Task <IResultModel <ModuleBuildCodeResultModel> > BuildCode(Guid moduleId, IList <ClassEntity> classList = null) { var result = new ResultModel <ModuleBuildCodeResultModel>(); var module = await _repository.GetAsync(moduleId); if (module == null) { return(result.Failed("模块不存在")); } //创建模块生成对象 var moduleBuildModel = _mapper.Map <ModuleBuildModel>(module); var config = _configProvider.Get <CodeGeneratorConfig>(); moduleBuildModel.Prefix = config.Prefix; moduleBuildModel.UiPrefix = config.UiPrefix; var id = Guid.NewGuid().ToString(); var rootPath = config.BuildCodePath; if (rootPath.IsNull()) { var pathConfig = _configProvider.Get <PathConfig>(); rootPath = Path.Combine(pathConfig.TempPath, "CodeGenerator/BuildCode"); } var moduleFullName = $"{config.Prefix}.Module.{module.Code}"; var buildModel = new TemplateBuildModel { RootPath = Path.Combine(rootPath, id, moduleFullName), NuGetPackageVersions = _nugetHelper.GetVersions() }; if (classList == null) { //如果classList参数为null,表示生成整个解决方案 buildModel.GenerateSln = true; classList = await _classRepository.QueryAllByModule(module.Id); } foreach (var classEntity in classList) { var classBuildModel = _mapper.Map <ClassBuildModel>(classEntity); var propertyList = await _propertyRepository.QueryByClass(classEntity.Id); if (propertyList.Any()) { //查询属性 foreach (var propertyEntity in propertyList) { var propertyBuildModel = _mapper.Map <PropertyBuildModel>(propertyEntity); //如果属性类型是枚举,查询枚举信息 if (propertyEntity.Type == PropertyType.Enum && propertyEntity.EnumId.NotEmpty()) { var enumEntity = await _enumRepository.GetAsync(propertyEntity.EnumId); propertyBuildModel.Enum = new EnumBuildModel { Name = enumEntity.Name, Remarks = enumEntity.Remarks }; var enumItemList = await _enumItemRepository.QueryByEnum(propertyEntity.EnumId); propertyBuildModel.Enum.ItemList = enumItemList.Select(m => new EnumItemBuildModel { Name = m.Name, Remarks = m.Remarks, Value = m.Value }).ToList(); } classBuildModel.PropertyList.Add(propertyBuildModel); } } var modelPropertyList = await _modelPropertyRepository.QueryByClass(classEntity.Id); if (modelPropertyList.Any()) { foreach (var propertyEntity in modelPropertyList) { var modelPropertyBuildModel = _mapper.Map <ModelPropertyBuildModel>(propertyEntity); //如果属性类型是枚举,查询枚举信息 if (propertyEntity.Type == PropertyType.Enum && propertyEntity.EnumId.NotEmpty()) { var enumEntity = await _enumRepository.GetAsync(propertyEntity.EnumId); modelPropertyBuildModel.Enum = new EnumBuildModel { Name = enumEntity.Name, Remarks = enumEntity.Remarks }; var enumItemList = await _enumItemRepository.QueryByEnum(propertyEntity.EnumId); modelPropertyBuildModel.Enum.ItemList = enumItemList.Select(m => new EnumItemBuildModel { Name = m.Name, Remarks = m.Remarks, Value = m.Value }).ToList(); } classBuildModel.ModelPropertyList.Add(modelPropertyBuildModel); } } classBuildModel.Method = await _classMethodRepository.GetByClass(classEntity.Id); moduleBuildModel.ClassList.Add(classBuildModel); } buildModel.Module = moduleBuildModel; var builder = new DefaultTemplateBuilder(); builder.Build(buildModel); var sourceDir = Path.Combine(rootPath, id); var outputFile = Path.Combine(rootPath, id + ".zip"); ZipFile.CreateFromDirectory(sourceDir, outputFile); //删除临时文件 Directory.Delete(sourceDir, true); var resultModel = new ModuleBuildCodeResultModel { Id = id, Name = moduleBuildModel.Name + ".zip", ZipPath = outputFile }; return(result.Success(resultModel)); }