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()); }