Beispiel #1
0
        public static TsFile[] Convert(IEnumerable <Type> types,
                                       IEnumerable <ICoder> coders,
                                       ICoder defaultCoder = null,
                                       IEnumerable <ICustomTypeConverter> customTypeConverters =
                                       null)
        {
            if (customTypeConverters != null)
            {
                TsType.RegisterCustomTypeConverters(customTypeConverters);
            }

            var csDeclarations = CsDeclarationFactory.Create(types).ToArray();

            var writtenTsFiles = coders.SelectMany(coder => coder.Rewrite(csDeclarations))
                                 .Where(x => x.Declarations.Any())
                                 .ToArray();

            return(FixBuild(writtenTsFiles,
                            defaultCoder ?? new DefaultCoder(),
                            csDeclarations));
        }
Beispiel #2
0
        private static TsFile[] FixBuild(TsFile[] tsFiles,
                                         ICoder defaultCoder,
                                         CsDeclaration[] csDeclarations)
        {
            var exportRegistry = new ExportRegistry();

            foreach (var tsFile in tsFiles)
            {
                exportRegistry.Register(tsFile);
            }

            var generatedFiles = new List <TsFile>();
            var filesToFix     = new Stack <TsFile>(tsFiles);

            while (filesToFix.Count > 0)
            {
                var fileToFix = filesToFix.Pop();

                // 1. Generating and auto importing external types
                var externals = FindExternals(fileToFix);
                foreach (var csType in externals)
                {
                    if (!exportRegistry.TryGetExport(csType, out var export))
                    {
                        Generate(csType);

                        if (!exportRegistry.TryGetExport(csType, out export))
                        {
                            throw new Exception($"Can not find Export for {csType}");
                        }
                    }

                    if (export.TsFile != fileToFix)
                    {
                        AutoImport(fileToFix, export);
                    }
                }

                // 2. Generating derived types
                var derivedTypes = FindDerivedTypes(externals, csDeclarations);
                foreach (var csType in derivedTypes)
                {
                    if (!exportRegistry.TryGetExport(csType, out _))
                    {
                        Generate(csType);
                    }
                }
            }

            void Generate(CsType csType)
            {
                var csDeclaration        = CsDeclarationFactory.Create(csType.OriginalType);
                var autoGeneratedTsFiles = defaultCoder.Rewrite(new[]
                {
                    csDeclaration
                });

                foreach (var autoGeneratedTsFile in autoGeneratedTsFiles)
                {
                    exportRegistry.Register(autoGeneratedTsFile);
                    filesToFix.Push(autoGeneratedTsFile);
                    generatedFiles.Add(autoGeneratedTsFile);
                }
            }

            return(tsFiles.Concat(generatedFiles).ToArray());
        }