Beispiel #1
0
        /// <summary>
        /// Creates DataStore roslyn document
        /// </summary>
        internal void CreateStoreDocument(DataStoreNode node)
        {
            var docName     = $"sys.DataStore.{node.Model.Name}.cs";
            var newSolution = Workspace.CurrentSolution.AddDocument(node.RoslynDocumentId, docName,
                                                                    CodeGenService.GenDataStoreDummyCode(node.Model));

            if (!Workspace.TryApplyChanges(newSolution))
            {
                Log.Warn($"Cannot add roslyn document for: {node.Model.Name}");
            }
        }
Beispiel #2
0
        /// <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);
                }
            }
        }
Beispiel #3
0
        public TypeSystem()
        {
            //Create Workspace
            Workspace = new OmniSharpWorkspace(new HostServicesAggregator(/*null*/));

            //虚拟项目
            ModelProjectId             = ProjectId.CreateNewId();
            WorkflowModelProjectId     = ProjectId.CreateNewId();
            ServiceBaseProjectId       = ProjectId.CreateNewId();
            AsyncServiceProxyProjectId = ProjectId.CreateNewId();
            //ExpressionProjectId = ProjectId.CreateNewId();

            //各基本虚拟代码
            BaseDummyCodeDocumentId        = DocumentId.CreateNewId(ModelProjectId);
            ServiceBaseDummyCodeDocumentId = DocumentId.CreateNewId(ServiceBaseProjectId);
            BaseWFDummyCodeDocumentId      = DocumentId.CreateNewId(WorkflowModelProjectId);

            var dllCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            var modelProjectInfo = ProjectInfo.Create(ModelProjectId, VersionStamp.Create(),
                                                      "ModelProject", "ModelProject", LanguageNames.CSharp, null, null,
                                                      dllCompilationOptions);
            var workflowProjectInfo = ProjectInfo.Create(WorkflowModelProjectId, VersionStamp.Create(),
                                                         "WorkflowProject", "WorkflowProject", LanguageNames.CSharp, null, null,
                                                         dllCompilationOptions);
            var serviceBaseProjectInfo = ProjectInfo.Create(ServiceBaseProjectId, VersionStamp.Create(),
                                                            "ServiceBaseProject", "ServiceBaseProject", LanguageNames.CSharp, null, null,
                                                            dllCompilationOptions);
            var asyncServiceProjectInfo = ProjectInfo.Create(AsyncServiceProxyProjectId, VersionStamp.Create(),
                                                             "AsyncServiceProxyProject", "AsyncServiceProxyProject", LanguageNames.CSharp, null, null,
                                                             dllCompilationOptions);
            //var expressionProjectInfo = ProjectInfo.Create(ExpressionProjectId, VersionStamp.Create(),
            //                                          "ExpressionProject", "ExpressionProject", LanguageNames.CSharp, null, null,
            //                                          dllCompilationOptions);

            var newSolution = Workspace.CurrentSolution
                              .AddProject(modelProjectInfo)
                              .AddMetadataReference(ModelProjectId, MetadataReferences.CoreLib)
                              .AddMetadataReference(ModelProjectId, MetadataReferences.NetstandardLib)
                              .AddDocument(BaseDummyCodeDocumentId, "BaseDummyCode.cs", CodeGenService.GenBaseDummyCode())

                              .AddProject(asyncServiceProjectInfo)
                              .AddMetadataReference(AsyncServiceProxyProjectId, MetadataReferences.CoreLib)
                              .AddMetadataReference(AsyncServiceProxyProjectId, MetadataReferences.NetstandardLib)
                              .AddProjectReference(AsyncServiceProxyProjectId, new ProjectReference(ModelProjectId))

                              .AddProject(serviceBaseProjectInfo)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.CoreLib)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.NetstandardLib)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.SystemRuntimLib)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.TasksLib)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.TasksExtLib)
                              .AddMetadataReference(ServiceBaseProjectId, MetadataReferences.DataCommonLib)
                              .AddProjectReference(ServiceBaseProjectId, new ProjectReference(ModelProjectId))
                              .AddDocument(ServiceBaseDummyCodeDocumentId, "ServiceBaseDummyCode.cs", CodeGenService.GenServiceBaseDummyCode())

                              .AddProject(workflowProjectInfo)
                              .AddMetadataReference(WorkflowModelProjectId, MetadataReferences.CoreLib)
                              .AddMetadataReference(WorkflowModelProjectId, MetadataReferences.NetstandardLib)
                              .AddProjectReference(WorkflowModelProjectId, new ProjectReference(ModelProjectId))
                              .AddDocument(BaseWFDummyCodeDocumentId, "BaseWFDummyCode.cs", CodeGenService.GenBaseWFDummyCode())
            ;

            //.AddProject(expressionProjectInfo)
            //.AddMetadataReference(ExpressionProjectId, MetadataReferences.CoreLib)
            //.AddMetadataReference(ExpressionProjectId, MetadataReferences.NetstandardLib)
            ////.AddMetadataReference(ExpressionProjectId, GetMetadataReference("System.dll"))
            //.AddProjectReference(ExpressionProjectId, new ProjectReference(ModelProjectId))
            //.AddProjectReference(ExpressionProjectId, new ProjectReference(SyncServiceProxyProjectId));

            if (!Workspace.TryApplyChanges(newSolution))
            {
                Log.Warn("Cannot create default workspace.");
            }
        }
Beispiel #4
0
        /// <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}");
                }
            }
        }