/// <summary> /// 更新模型RoslynDocument,注意:服务模型也会更新,如不需要由调用者忽略 /// </summary> internal async ValueTask UpdateModelDocumentAsync(ModelNode node) { if (node.RoslynDocumentId == null) { return; } var appName = node.AppNode.Model.Name; var model = node.Model; var docId = node.RoslynDocumentId; Solution newSolution = null; //TODO: others switch (model.ModelType) { case ModelType.Entity: { var sourceCode = CodeGenService.GenEntityDummyCode((EntityModel)model, appName, node.DesignTree); newSolution = Workspace.CurrentSolution.WithDocumentText(docId, SourceText.From(sourceCode)); } break; case ModelType.Enum: newSolution = Workspace.CurrentSolution.WithDocumentText(docId, SourceText.From(CodeGenService.GenEnumDummyCode((EnumModel)model, appName))); break; case ModelType.Service: { var sourceCode = await Store.ModelStore.LoadServiceCodeAsync(model.Id); newSolution = Workspace.CurrentSolution.WithDocumentText(docId, SourceText.From(sourceCode)); // 服务模型还需要更新代理类 var srcdoc = newSolution.GetDocument(docId); var proxyCode = await CodeGenService.GenProxyCode(srcdoc, appName, (ServiceModel)model); newSolution = newSolution .WithDocumentText(node.AsyncProxyDocumentId, SourceText.From(proxyCode)); } break; } if (newSolution != null) { if (!Workspace.TryApplyChanges(newSolution)) { Log.Warn("Cannot update roslyn document for: " + model.Name); } } }
/// <summary> /// Creates the model's roslyn document /// </summary> internal async ValueTask CreateModelDocumentAsync(ModelNode node, string initServiceCode = null) { //TODO: fix others, 另考虑代码统一由调用者传入 Solution newSolution = null; var appName = node.AppNode.Model.Name; var model = node.Model; var docId = node.RoslynDocumentId; switch (model.ModelType) { case ModelType.Entity: { var docName = $"{appName}.Entities.{model.Name}.cs"; var dummyCode = CodeGenService.GenEntityDummyCode((EntityModel)model, appName, node.DesignTree); newSolution = Workspace.CurrentSolution.AddDocument(docId, docName, dummyCode); } break; case ModelType.Enum: { var docName = $"{appName}.Enums.{model.Name}.cs"; newSolution = Workspace.CurrentSolution.AddDocument(docId, docName, CodeGenService.GenEnumDummyCode((EnumModel)model, appName)); } break; case ModelType.Service: { //注意: 服务模型先创建虚拟项目 CreateServiceProject(node.ServiceProjectId, (ServiceModel)model, appName); var docName = $"{appName}.Services.{model.Name}.cs"; string sourceCode = initServiceCode; if (string.IsNullOrEmpty(sourceCode)) { if (node.IsCheckoutByMe) //已签出尝试从Staged中加载 { sourceCode = await StagedService.LoadServiceCode(model.Id); } if (string.IsNullOrEmpty(sourceCode)) //从ModelStore加载 { sourceCode = await Store.ModelStore.LoadServiceCodeAsync(model.Id); } } newSolution = Workspace.CurrentSolution.AddDocument(docId, docName, sourceCode); //服务模型创建代理 var proxyDocName = $"{appName}.{model.Name}.AsnycProxy.cs"; var srcdoc = newSolution.GetDocument(docId); var proxyCode = await CodeGenService.GenProxyCode(srcdoc, appName, (ServiceModel)model); newSolution = newSolution.AddDocument(node.AsyncProxyDocumentId, proxyDocName, proxyCode); } break; case ModelType.Permission: { var docName = $"{appName}.Permissions.{model.Name}.cs"; newSolution = Workspace.CurrentSolution.AddDocument(docId, docName, CodeGenService.GenPermissionDummyCode((PermissionModel)model, appName)); } break; //case ModelType.Workflow: //{ // var docName = string.Format("{0}.Workflows.{1}.cs", model.AppID, model.Name); // newSolution = Workspace.CurrentSolution.AddDocument(docId, docName, // CodeGenService.GenWorkflowDummyCode((WorkflowModel)model)); //} //break; } if (newSolution != null) { if (!Workspace.TryApplyChanges(newSolution)) { Log.Warn($"Cannot add roslyn document for: {model.Name}"); } } }