public async Task <object> Handle(DesignHub hub, InvokeArgs args) { var staged = await StagedService.LoadStagedAsync(onlyModelsAndFolders : false); //TODO:暂重新加载 hub.PendingChanges = staged.Items; if (hub.PendingChanges == null || hub.PendingChanges.Length == 0) { return(null); } var res = new List <ChangedInfo>(); for (int i = 0; i < hub.PendingChanges.Length; i++) { //TODO:其他类型处理 if (hub.PendingChanges[i] is ModelBase model) { res.Add(new ChangedInfo { ModelType = model.ModelType.ToString(), ModelID = model.Name }); } else if (hub.PendingChanges[i] is ModelFolder folder) { res.Add(new ChangedInfo { ModelType = ModelType.Folder.ToString(), ModelID = folder.TargetModelType.ToString() }); } } return(res); }
private static async Task DropModelNodeInside(ModelNode sourceNode, DesignNode targetNode) { //注意:目标节点可能是模型根目录 if (targetNode.NodeType == DesignNodeType.ModelRootNode) { var rootNode = (ModelRootNode)targetNode; if (rootNode.AppID != sourceNode.Model.AppId) { throw new InvalidOperationException("无法拖动模型节点至不同的Application内"); } sourceNode.Parent.Nodes.Remove(sourceNode); targetNode.Nodes.Add(sourceNode); sourceNode.Model.FolderId = null; await StagedService.SaveModelAsync(sourceNode.Model); //直接保存 } else if (targetNode.NodeType == DesignNodeType.FolderNode) { var targetFolder = ((FolderNode)targetNode).Folder; var rootFolder = targetFolder.GetRoot(); if (rootFolder.AppId != sourceNode.Model.AppId) { throw new InvalidOperationException("无法拖动模型节点至不同的Application内"); } sourceNode.Parent.Nodes.Remove(sourceNode); targetNode.Nodes.Add(sourceNode); sourceNode.Model.FolderId = targetFolder.Id; await StagedService.SaveModelAsync(sourceNode.Model); //直接保存 } else { throw new NotSupportedException(); } }
public async Task <object> Handle(DesignHub hub, InvokeArgs args) { var modelID = args.GetString(); // eg: sys.CustomerListView var sr = modelID.Split('.'); var app = hub.DesignTree.FindApplicationNodeByName(sr[0]); var node = hub.DesignTree.FindModelNodeByName(app.Model.Id, ModelType.View, sr[1]); if (node == null) { throw new Exception("Cannot found view node: " + modelID); } var modelNode = node as ModelNode; if (modelNode == null) { throw new Exception("Cannot found view model: " + modelID); } string runtimeCode = null; if (modelNode.IsCheckoutByMe) { runtimeCode = await StagedService.LoadViewRuntimeCode(modelNode.Model.Id); } if (string.IsNullOrEmpty(runtimeCode)) { runtimeCode = await Store.ModelStore.LoadViewAssemblyAsync(modelID); } return(runtimeCode); }
internal Task SaveAsync() { //查找文件夹直至根级文件夹,然后序列化保存根级文件夹 ModelFolder rootFolder = Folder.GetRoot(); //保存节点模型 return(StagedService.SaveFolderAsync(rootFolder)); }
internal async Task SaveAsync() { if (!IsCheckoutByMe) { throw new Exception("StoreNode has not checkout"); } //保存节点模型 await StagedService.SaveModelAsync(Model); }
public async Task <object> Handle(DesignHub hub, InvokeArgs args) { var modelID = args.GetString(); var modelNode = hub.DesignTree.FindModelNode(ModelType.Report, ulong.Parse(modelID)); if (modelNode == null) { throw new Exception($"Cannot find report model: {modelID}"); } if (modelNode.IsCheckoutByMe) { var code = await StagedService.LoadReportCodeAsync(modelNode.Model.Id); if (code != null) { return(code); } } return(await Store.ModelStore.LoadReportCodeAsync(modelNode.Model.Id)); }
public async Task <object> Handle(DesignHub hub, InvokeArgs args) { var modelID = args.GetString(); var modelNode = hub.DesignTree.FindModelNode(ModelType.View, ulong.Parse(modelID)); if (modelNode == null) { throw new Exception($"Cannot find view model: {modelID}"); } var res = new OpenViewModelResult(); res.Model = (ViewModel)modelNode.Model; bool hasLoadFromStaged = false; if (modelNode.IsCheckoutByMe) { var codes = await StagedService.LoadViewCodeAsync(modelNode.Model.Id); if (codes.Item1) { hasLoadFromStaged = true; res.Template = codes.Item2; res.Script = codes.Item3; res.Style = codes.Item4; } } if (!hasLoadFromStaged) { var codes = await Store.ModelStore.LoadViewCodeAsync(modelNode.Model.Id); res.Template = codes.Item1; res.Script = codes.Item2; res.Style = codes.Item3; } return(res); }
internal async Task LoadNodesAsync() { if (Interlocked.CompareExchange(ref _loadingFlag, 1, 0) != 0) { throw new Exception("DesignTree are loading."); } //先判断是否已经加载过,是则清空准备重新加载 if (Nodes.Count > 0) { DesignHub.ResetTypeSystem(); Nodes.Clear(); } //开始加载 StoreRootNode = new DataStoreRootNode(this); Nodes.Add(StoreRootNode); AppRootNode = new ApplicationRootNode(this); Nodes.Add(AppRootNode); //先加载签出信息及StagedModels _checkouts = await CheckoutService.LoadAllAsync(); Staged = await StagedService.LoadStagedAsync(onlyModelsAndFolders : true); var amodels = await Store.ModelStore.LoadAllApplicationAsync(); var applicationModels = new List <ApplicationModel>(amodels); applicationModels.Sort((a, b) => a.Name.CompareTo(b.Name)); var mfolders = await Store.ModelStore.LoadAllFolderAsync(); var folders = new List <ModelFolder>(mfolders); //从staged中添加新建的并更新修改的文件夹 Staged.UpdateFolders(folders); var mmodels = await Store.ModelStore.LoadAllModelAsync(); var models = new List <ModelBase>(mmodels); #if !FUTURE //加载默认存储模型 var defaultStoreType = Store.SqlStore.Default.GetType(); var defaultStoreModel = new DataStoreModel(DataStoreKind.Sql, $"{defaultStoreType.Assembly.GetName().Name};{defaultStoreType.Name}", "Default"); defaultStoreModel.NameRules = DataStoreNameRules.AppPrefixForTable; //defaultStoreModel.Settings = ""; //TODO:fix settings defaultStoreModel.AcceptChanges(); models.Add(defaultStoreModel); #endif //加载staged中新建的模型,可能包含DataStoreModel models.AddRange(Staged.FindNewModels()); //加入AppModels节点 foreach (var app in applicationModels) { AppRootNode.Nodes.Add(new ApplicationNode(this, app)); } //加入Folders foreach (var f in folders) { FindModelRootNode(f.AppId, f.TargetModelType).AddFolder(f); } //加入Models Staged.RemoveDeletedModels(models); //先移除已删除的 var allModelNodes = new List <ModelNode>(models.Count); foreach (var m in models) { if (m.ModelType == ModelType.DataStore) { var dsModel = (DataStoreModel)m; var dsNode = StoreRootNode.AddModel(dsModel, DesignHub); DesignHub.TypeSystem.CreateStoreDocument(dsNode); } else { allModelNodes.Add(FindModelRootNode(m.AppId, m.ModelType).AddModel(m)); } } //在所有节点加载完后创建模型对应的RoslynDocument foreach (var n in allModelNodes) { await DesignHub.TypeSystem.CreateModelDocumentAsync(n); } Interlocked.Exchange(ref _loadingFlag, 0); //清空Staged Staged = null; #if DEBUG ThreadPool.QueueUserWorkItem(s => { DesignHub.TypeSystem.DumpProjectErrors(DesignHub.TypeSystem.ModelProjectId); //DesignHub.TypeSystem.DumpProjectErrors(DesignHub.TypeSystem.SyncSysServiceProjectId); DesignHub.TypeSystem.DumpProjectErrors(DesignHub.TypeSystem.ServiceBaseProjectId); }); #endif }
/// <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}"); } } }
/// <summary> /// 保存模型节点 /// </summary> internal async Task SaveAsync(object[] modelInfos) { if (!IsCheckoutByMe) { throw new Exception("ModelNode has not checkout"); } //TODO: 更新相关模型的内容,另考虑事务保存模型及相关代码 if (Model.PersistentState != PersistentState.Deleted) { switch (Model.ModelType) { case ModelType.Service: { //TODO:*****更新服务模型代理类 // if (!this._document.ParsedDocument.HasErrors) // { // TypeSystemService.UpdateServiceProxyDocument(this.TargetNode.RoslynDocumentId, // this.TargetNode.SyncProxyDocumentId, // this.TargetNode.AsyncProxyDocumentId, // this.TargetModel); // } //保存初始化或更改过的代码 string sourceCode; if (modelInfos != null && modelInfos.Length == 1) { sourceCode = (string)modelInfos[0]; } else { var doc = DesignTree.DesignHub.TypeSystem.Workspace.CurrentSolution.GetDocument(RoslynDocumentId); var sourceText = await doc.GetTextAsync(); sourceCode = sourceText.ToString(); } await StagedService.SaveServiceCodeAsync(Model.Id, sourceCode); } break; case ModelType.View: { if (modelInfos != null) { await StagedService.SaveViewCodeAsync(Model.Id, (string)modelInfos[0], (string)modelInfos[1], (string)modelInfos[2]); await StagedService.SaveViewRuntimeCodeAsync(Model.Id, (string)modelInfos[3]); } } break; case ModelType.Report: await StagedService.SaveReportCodeAsync(Model.Id, (string)modelInfos[0]); break; } //注意:不在此更新RoslynDocument, 实体模型通过设计命令更新,服务模型通过前端代码编辑器实时更新 } //保存节点模型 await StagedService.SaveModelAsync(Model); }
private static async Task <DesignNode> DeleteModelNode(DesignHub hub, ModelNode node) { // 查找ModelRootNode var rootNode = hub.DesignTree.FindModelRootNode(node.Model.AppId, node.Model.ModelType); bool rootNodeHasCheckout = rootNode.IsCheckoutByMe; // 尝试签出模型节点及根节点 bool nodeCheckout = await node.Checkout(); bool rootCheckout = await rootNode.Checkout(); if (!nodeCheckout || !rootCheckout) { throw new Exception("Can't checkout nodes."); } // 注意:如果自动签出了模型根节点,当前选择的节点需要重新指向,因为Node.Checkout()时已重新加载 if (!rootNodeHasCheckout) { node = rootNode.FindModelNode(node.Model.Id); } if (node == null) //可能已不存在 { throw new Exception("Delete target not exists, please refresh."); } // 判断当前节点所属层是否是系统层 if (node.Model.ModleLayer == ModelLayer.SYS) { throw new Exception("Can't delete system model."); } var model = node.Model; // 查找引用项 var usages = await RefactoringService.FindModelReferencesAsync(hub, model.ModelType, node.AppNode.Model.Name, model.Name); if (usages != null && usages.Count > 0) { //注意排除自身引用 usages = usages.Where(u => !(u.ModelNode.Model.Id == model.Id)).ToArray(); if (usages.Count > 0) { #if DEBUG foreach (var item in usages) { Log.Warn(item.ToString()); } #endif throw new Exception("Has usages, Can't delete it."); } } // 判断当前模型是否已持久化到数据库中 if (model.PersistentState == PersistentState.Detached) { await StagedService.DeleteModelAsync(model.Id); } else { model.MarkDeleted(); await node.SaveAsync(null); } // 移除对应节点 rootNode.RemoveModel(node); // 删除Roslyn相关 RemoveRoslynFromModelNode(hub, node); return(rootNodeHasCheckout ? null : rootNode); }