Пример #1
0
        public virtual async Task GenerateCodes(Workspace workspace)
        {
            if (workspace == null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            BitConfig bitConfig = _bitConfigProvider.GetConfiguration();

            foreach (BitCodeGeneratorMapping proxyGeneratorMapping in bitConfig.BitCodeGeneratorConfigs.BitCodeGeneratorMappings.Where(m => m.GenerationType == GenerationType.CSharp))
            {
                string generatedContextName = proxyGeneratorMapping.DestinationFileName;

                StringBuilder generatedCs = new StringBuilder();

                string generatedCSContextExtension = ".cs";

                Project destProject = workspace.CurrentSolution.Projects.Where(p => p.Language == LanguageNames.CSharp)
                                      .ExtendedSingle($"Trying to find project with name: {proxyGeneratorMapping.DestinationProject.Name}", p => proxyGeneratorMapping.DestinationProject.IsThisProject(p));

                IList <Project> involveableProjects = _bitCodeGeneratorOrderedProjectsProvider.GetInvolveableProjects(workspace, workspace.CurrentSolution.Projects.Where(p => p.Language == LanguageNames.CSharp).ToList(), proxyGeneratorMapping);

                List <DtoController> dtoControllers = new List <DtoController>();

                foreach (Project p in involveableProjects)
                {
                    dtoControllers.AddRange(await _dtoControllersProvider.GetProjectDtoControllersWithTheirOperations(p));
                }

                generatedCs.AppendLine(_contextGenerator.GenerateCSharpContext(dtoControllers, proxyGeneratorMapping));

                WriteFiles(generatedCs.ToString(), generatedContextName, generatedCSContextExtension, destProject);
            }
        }
        public virtual async Task GenerateCodes(Solution solution, IList <Project> projects)
        {
            if (solution == null)
            {
                throw new ArgumentNullException(nameof(solution));
            }

            if (projects == null)
            {
                throw new ArgumentNullException(nameof(projects));
            }

            foreach (BitCodeGeneratorMapping proxyGeneratorMapping in _bitCodeGeneratorMappingsProvider.GetBitCodeGeneratorMappings(solution, projects))
            {
                string generatedContextName = proxyGeneratorMapping.DestinationFileName;

                StringBuilder generatedJs = new StringBuilder();
                StringBuilder generatedTs = new StringBuilder();

                string generatedJsContextExtension = ".js";
                string generatedTsContextExtension = ".d.ts";

                Project destProject = solution.Projects
                                      .Last(p => p.Name == proxyGeneratorMapping.DestinationProject.Name);

                IList <Project> involveableProjects = _solutionProjectsSelector.GetInvolveableProjects(solution, solution.Projects.ToList(), proxyGeneratorMapping);

                List <Dto> dtos = new List <Dto>();

                foreach (Project p in involveableProjects)
                {
                    dtos.AddRange(await _dtosProvider.GetProjectDtos(p, involveableProjects));
                }

                List <EnumType> enumTypes = new List <EnumType>();

                foreach (Project p in involveableProjects)
                {
                    enumTypes.AddRange(await _projectEnumTypesProvider.GetProjectEnumTypes(p, involveableProjects));
                }

                List <DtoController> dtoControllers = new List <DtoController>();

                foreach (Project p in involveableProjects)
                {
                    dtoControllers.AddRange(await _dtoControllersProvider.GetProjectDtoControllersWithTheirOperations(p));
                }

                generatedJs.AppendLine(_dtoGenerator.GenerateJavaScriptDtos(dtos, enumTypes));
                generatedJs.AppendLine(_contextGenerator.GenerateJavaScriptContext(dtoControllers, proxyGeneratorMapping));
                generatedTs.AppendLine(_dtoGenerator.GenerateTypeScriptDtos(dtos, enumTypes, proxyGeneratorMapping.TypingsPath));
                generatedTs.AppendLine(_contextGenerator.GenerateTypeScriptContext(dtoControllers, proxyGeneratorMapping));

                WriteResults(solution, generatedJs.ToString(), generatedContextName, generatedJsContextExtension, destProject);

                WriteResults(solution, generatedTs.ToString(), generatedContextName, generatedTsContextExtension, destProject);
            }
        }
        public virtual async Task GenerateCodes(Workspace workspace, IList <string> projectNames)
        {
            if (workspace == null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            foreach (BitCodeGeneratorMapping proxyGeneratorMapping in _bitCodeGeneratorMappingsProvider.GetBitCodeGeneratorMappings(workspace, projectNames))
            {
                string generatedContextName = proxyGeneratorMapping.DestinationFileName;

                StringBuilder generatedJs = new StringBuilder();
                StringBuilder generatedTs = new StringBuilder();

                string generatedJsContextExtension = ".js";
                string generatedTsContextExtension = ".d.ts";

                Project destProject = workspace.CurrentSolution.Projects.Where(p => p.Language == LanguageNames.CSharp)
                                      .ExtendedSingle($"Trying to find project with name: {proxyGeneratorMapping.DestinationProject.Name}", p => p.Name == proxyGeneratorMapping.DestinationProject.Name);

                IList <Project> involveableProjects = _solutionProjectsSelector.GetInvolveableProjects(workspace, workspace.CurrentSolution.Projects.Where(p => p.Language == LanguageNames.CSharp).ToList(), proxyGeneratorMapping);

                List <Dto> dtos = new List <Dto>();

                foreach (Project p in involveableProjects)
                {
                    dtos.AddRange(await _dtosProvider.GetProjectDtos(p, involveableProjects));
                }

                List <EnumType> enumTypes = new List <EnumType>();

                foreach (Project p in involveableProjects)
                {
                    enumTypes.AddRange(await _projectEnumTypesProvider.GetProjectEnumTypes(p, involveableProjects));
                }

                List <DtoController> dtoControllers = new List <DtoController>();

                foreach (Project p in involveableProjects)
                {
                    dtoControllers.AddRange(await _dtoControllersProvider.GetProjectDtoControllersWithTheirOperations(p));
                }

                generatedJs.AppendLine(_dtoGenerator.GenerateJavaScriptDtos(dtos, enumTypes));
                generatedJs.AppendLine(_contextGenerator.GenerateJavaScriptContext(dtoControllers, proxyGeneratorMapping));
                generatedTs.AppendLine(_dtoGenerator.GenerateTypeScriptDtos(dtos, enumTypes, proxyGeneratorMapping.TypingsPath));
                generatedTs.AppendLine(_contextGenerator.GenerateTypeScriptContext(dtoControllers, proxyGeneratorMapping));

                WriteFiles(workspace.CurrentSolution, generatedJs.ToString(), generatedContextName, generatedJsContextExtension, destProject);

                WriteFiles(workspace.CurrentSolution, generatedTs.ToString(), generatedContextName, generatedTsContextExtension, destProject);
            }
        }
        public async Task <IList <EnumType> > GetProjectEnumTypes(Project project, IList <Project>?allSourceProjects = null)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (allSourceProjects == null)
            {
                allSourceProjects = new List <Project> {
                    project
                }
            }
            ;

            IList <EnumType> enumTypes = new List <EnumType>();

            List <DtoController> dtoControllers = (await _projectDtoControllersProvider
                                                   .GetProjectDtoControllersWithTheirOperations(project)).ToList();

            IList <Dto> dtos = await _dtosProvider.GetProjectDtos(project, allSourceProjects);

            List <Compilation> sourceProjectsCompilations = new List <Compilation> {
            };

            foreach (var p in allSourceProjects)
            {
                sourceProjectsCompilations.Add(await p.GetCompilationAsync());
            }

            dtos.SelectMany(d => d.Properties)
            .Where(p => p.Type.IsEnum())
            .Select(p => p.Type.GetUnderlyingTypeSymbol())
            .Union(dtoControllers.SelectMany(dtoController => dtoController.Operations.SelectMany(operation => operation.Parameters.Select(p => p.Type).Union(new[] { operation.ReturnType }))).Where(t => t.IsEnum()).Select(t => t.GetUnderlyingComplexType()))
            .Where(enumType => sourceProjectsCompilations.Any(c => c.Assembly.TypeNames.Any(tName => tName == enumType.Name)))
            .Distinct()
            .ToList()
            .ForEach(enumType =>
            {
                enumTypes.Add(new EnumType
                {
                    EnumTypeSymbol = enumType,
                    Members        = enumType.GetMembers().OfType <IFieldSymbol>().Select((m, i) => new EnumMember
                    {
                        Name   = m.Name,
                        Symbol = m,
                        Index  = i,
                        Value  = Convert.ToInt32(m.ConstantValue, CultureInfo.InvariantCulture)
                    }).ToList()
                });
            });

            return(enumTypes);
        }
    }
        public async Task <IList <EnumType> > GetProjectEnumTypes(Project project, IList <Project>?allSourceProjects = null)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (allSourceProjects == null)
            {
                allSourceProjects = new List <Project> {
                    project
                }
            }
            ;

            List <DtoController> dtoControllers = (await _projectDtoControllersProvider
                                                   .GetProjectDtoControllersWithTheirOperations(project).ConfigureAwait(false)).ToList();

            IList <Dto> dtos = await _dtosProvider.GetProjectDtos(project, allSourceProjects).ConfigureAwait(false);

            HashSet <EnumType> enums = new HashSet <EnumType>();

            foreach (var enumType in dtoControllers.SelectMany(dtoController => dtoController.Operations.SelectMany(operation => operation.Parameters.Select(p => p.Type).Union(new[] { operation.ReturnType })))
                     .Union(dtos.SelectMany(d => d.Properties.Select(p => p.Type)))
                     .Select(x => x.GetUnderlyingTypeSymbol())
                     .Select(x => x.IsCollectionType() || x.IsQueryableType() ? x.GetElementType() : x)
                     .Where(t => t.IsEnum()))
            {
                if (enums.Any(e => SymbolEqualityComparer.Default.Equals(e.EnumTypeSymbol, enumType)))
                {
                    continue;
                }

                enums.Add(new EnumType
                {
                    EnumTypeSymbol = enumType,
                    Members        = enumType.GetMembers().OfType <IFieldSymbol>().Select((m, i) => new EnumMember
                    {
                        Name   = m.Name,
                        Symbol = m,
                        Index  = i,
                        Value  = Convert.ToInt32(m.ConstantValue, CultureInfo.InvariantCulture)
                    }).ToList()
                });
            }

            return(enums
                   .ToList());
        }
    }
Пример #6
0
        public virtual async Task <IList <Dto> > GetProjectDtos(Project project, IList <Project>?allSourceProjects = null)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (allSourceProjects == null)
            {
                allSourceProjects = new List <Project> {
                    project
                }
            }
            ;

            List <DtoController> dtoControllers = (await _projectDtoControllersProvider
                                                   .GetProjectDtoControllersWithTheirOperations(project)).ToList();

            List <Dto> dtosAndComplexTypes = new List <Dto>();

            foreach (var dtoOrComplexType in dtoControllers.Select(c => c.ModelSymbol).Union(dtoControllers.SelectMany(dtoController => dtoController.Operations.SelectMany(operation => operation.Parameters.Select(p => p.Type).Union(new[] { operation.ReturnType }))))
                     .Select(x => x.GetUnderlyingTypeSymbol())
                     .Select(x => x.IsCollectionType() || x.IsQueryableType() ? x.GetElementType() : x)
                     .Where(t => t.IsComplexType() || t.IsDto())
                     .SelectMany(t => new[] { t }.Union(t.BaseDtoClasses())))
            {
                FindDtoAndComplexTypes(dtoOrComplexType, ref dtosAndComplexTypes);
            }

            foreach (var dtoOrComplexType in dtosAndComplexTypes)
            {
                if (dtoOrComplexType.DtoSymbol.Interfaces.Any(i => i.Name == "ISyncableDto") && !dtoOrComplexType.Properties.Any(p => p.Name == "IsSynced"))
                {
                    dtoOrComplexType.Properties.Add(await _isSyncedPropertyForISyncableDtos.Value);
                }
            }

            dtosAndComplexTypes.ToList()
            .ForEach(dOrC =>
            {
                if (dOrC.DtoSymbol.BaseType.IsDto())
                {
                    dOrC.BaseDtoSymbol = dOrC.DtoSymbol.BaseType;
                }
            });

            List <Dto> orderedFinalResult = new List <Dto>();

            dtosAndComplexTypes
            .ToList()
            .ForEach(dto =>
            {
                int insertIndex = 0;

                var orderedDtosWithIndex = orderedFinalResult.Select((d, i) => new { Dto = d, Index = i }).ToList();

                foreach (var dWithIndex in orderedDtosWithIndex)
                {
                    if (SymbolEqualityComparer.Default.Equals(dto.BaseDtoSymbol, dWithIndex.Dto.DtoSymbol) == true)
                    {
                        insertIndex = dWithIndex.Index + 1;
                    }
                }

                orderedFinalResult.Insert(insertIndex, dto);
            });

            return(orderedFinalResult);
        }

        void FindDtoAndComplexTypes(ITypeSymbol dtoOrComplexTypeSymbol, ref List <Dto> dtosAndComplexTypes)
        {
            if (dtosAndComplexTypes.Any(dOrC => SymbolEqualityComparer.Default.Equals(dOrC.DtoSymbol, dtoOrComplexTypeSymbol)))
            {
                return;
            }

            Dto dtoOrComplexType = new Dto
            {
                DtoSymbol  = (INamedTypeSymbol)dtoOrComplexTypeSymbol,
                Properties = dtoOrComplexTypeSymbol.GetMembers().OfType <IPropertySymbol>().ToList()
            };

            dtosAndComplexTypes.Add(dtoOrComplexType);

            foreach (var innerDtoOrComplexType in dtoOrComplexType.Properties
                     .Select(p => p.Type.GetUnderlyingTypeSymbol())
                     .Select(t => t.IsCollectionType() || t.IsQueryableType() ? t.GetElementType() : t)
                     .Where(pType => pType.IsComplexType() || pType.IsDto()))
            {
                FindDtoAndComplexTypes(innerDtoOrComplexType, ref dtosAndComplexTypes);
            }
        }
    }
        public virtual IList <Dto> GetProjectDtos(Project project, IList <Project> allSourceProjects = null)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (allSourceProjects == null)
            {
                allSourceProjects = new List <Project> {
                    project
                }
            }
            ;

            List <DtoController> dtoControllers = _projectDtoControllersProvider
                                                  .GetProjectDtoControllersWithTheirOperations(project).ToList();

            IList <Dto> dtos = dtoControllers
                               .Select(dtoController =>
            {
                Dto dto = new Dto
                {
                    DtoSymbol  = (INamedTypeSymbol)dtoController.ModelSymbol,
                    Properties = dtoController.ModelSymbol.GetMembers().OfType <IPropertySymbol>().ToList()
                };

                if (dto.DtoSymbol.Interfaces.Any(i => i.Name == "ISyncableDto") && !dto.Properties.Any(p => p.Name == "ISV"))
                {
                    dto.Properties.Add(_isvPropertyForISyncableDtos.Value);
                }

                return(dto);
            })
                               .ToList();

            List <Compilation> sourceProjectsCompilations = allSourceProjects.Select(p => p.GetCompilationAsync().Result).ToList();

            dtos.SelectMany(d => d.Properties)
            .Where(p => p.Type.IsComplexType())
            .Select(p => p.Type.GetUnderlyingComplexType())
            .Union(dtoControllers.SelectMany(dtoController => dtoController.Operations.SelectMany(operation => operation.Parameters.Select(p => p.Type).Union(new[] { operation.ReturnType }))).Where(t => t.IsComplexType()).Select(t => t.GetUnderlyingComplexType()))
            .Where(complexType => sourceProjectsCompilations.Any(c => c.Assembly.TypeNames.Any(tName => tName == complexType.Name)))
            .Distinct()
            .ToList()
            .ForEach(complexType =>
            {
                dtos.Add(new Dto
                {
                    DtoSymbol  = (INamedTypeSymbol)complexType,
                    Properties = complexType.GetMembers().OfType <IPropertySymbol>().ToList()
                });
            });

            dtos.ToList()
            .ForEach(dto =>
            {
                if (dto.DtoSymbol.BaseType.IsDto())
                {
                    dto.BaseDtoSymbol = dto.DtoSymbol.BaseType;
                }
            });

            List <Dto> orderedDtos = new List <Dto>();

            dtos.ToList()
            .ForEach(dto =>
            {
                int insertIndex = 0;

                var orderedDtosWithIndex = orderedDtos.Select((d, i) => new { Dto = d, Index = i }).ToList();

                foreach (var dWithIndex in orderedDtosWithIndex)
                {
                    if (dto.BaseDtoSymbol == dWithIndex.Dto.DtoSymbol)
                    {
                        insertIndex = dWithIndex.Index + 1;
                    }
                }

                orderedDtos.Insert(insertIndex, dto);
            });

            return(orderedDtos);
        }
    }