|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {FileSystem} from '../../../../compiler-cli/src/ngtsc/file_system'; |
| 10 | +import {NgtscProgram} from '../../../../compiler-cli/src/ngtsc/program'; |
| 11 | +import assert from 'assert'; |
| 12 | +import path from 'path'; |
| 13 | +import ts from 'typescript'; |
| 14 | +import {isShim} from '../../../../compiler-cli/src/ngtsc/shims'; |
| 15 | +import {createNgtscProgram} from './helpers/ngtsc_program'; |
| 16 | +import {Serializable} from './helpers/serializable'; |
| 17 | +import {Replacement} from './replacement'; |
| 18 | +import {BaseProgramInfo, ProgramInfo} from './program_info'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class defining a `Tsurge` migration. |
| 22 | + * |
| 23 | + * A tsurge migration is split into three stages: |
| 24 | + * - analyze phase |
| 25 | + * - merge phase |
| 26 | + * - migrate phase |
| 27 | + * |
| 28 | + * The motivation for such split is that migrations may be executed |
| 29 | + * on individual workers, e.g. via go/tsunami or a Beam pipeline. The |
| 30 | + * individual workers are never seeing the full project, e.g. Google3. |
| 31 | + * |
| 32 | + * The analysis phases can operate on smaller TS project units, and later |
| 33 | + * the expect the isolated unit data to be merged into some sort of global |
| 34 | + * metadata via the `merge` phase. For example, every analyze worker may |
| 35 | + * contribute to a list of TS references that are later combined. |
| 36 | + * |
| 37 | + * The migrate phase can then compute actual file updates for all individual |
| 38 | + * compilation units, leveraging the global metadata to e.g. see if there are |
| 39 | + * any references from other compilation units that may be problematic and prevent |
| 40 | + * migration of a given file. |
| 41 | + * |
| 42 | + * More details can be found in the design doc for signal input migration, |
| 43 | + * or in the testing examples. |
| 44 | + * |
| 45 | + * TODO: Link design doc. |
| 46 | + */ |
| 47 | +export abstract class TsurgeMigration< |
| 48 | + UnitAnalysisMetadata, |
| 49 | + CombinedGlobalMetadata, |
| 50 | + TsProgramType extends ts.Program | NgtscProgram = NgtscProgram, |
| 51 | + FullProgramInfo extends ProgramInfo<TsProgramType> = ProgramInfo<TsProgramType>, |
| 52 | +> { |
| 53 | + // By default, ngtsc programs are being created. |
| 54 | + createProgram(tsconfigAbsPath: string, fs?: FileSystem): BaseProgramInfo<TsProgramType> { |
| 55 | + return createNgtscProgram(tsconfigAbsPath, fs) as BaseProgramInfo<TsProgramType>; |
| 56 | + } |
| 57 | + |
| 58 | + // Optional function to prepare the base `ProgramInfo` even further, |
| 59 | + // for the analyze and migrate phases. E.g. determining source files. |
| 60 | + prepareProgram(info: BaseProgramInfo<TsProgramType>): FullProgramInfo { |
| 61 | + assert(info.program instanceof NgtscProgram); |
| 62 | + |
| 63 | + const userProgram = info.program.getTsProgram(); |
| 64 | + const fullProgramSourceFiles = userProgram.getSourceFiles(); |
| 65 | + const sourceFiles = fullProgramSourceFiles.filter( |
| 66 | + (f) => |
| 67 | + !f.isDeclarationFile && |
| 68 | + // Note `isShim` will work for the initial program, but for TCB programs, the shims are no longer annotated. |
| 69 | + !isShim(f) && |
| 70 | + !f.fileName.endsWith('.ngtypecheck.ts'), |
| 71 | + ); |
| 72 | + |
| 73 | + const basePath = path.dirname(info.tsconfigAbsolutePath); |
| 74 | + const projectDirAbsPath = info.userOptions.rootDir ?? basePath; |
| 75 | + |
| 76 | + return { |
| 77 | + ...info, |
| 78 | + sourceFiles, |
| 79 | + fullProgramSourceFiles, |
| 80 | + projectDirAbsPath, |
| 81 | + } as FullProgramInfo; |
| 82 | + } |
| 83 | + |
| 84 | + /** Analyzes the given TypeScript project and returns serializable compilation unit data. */ |
| 85 | + abstract analyze(program: FullProgramInfo): Promise<Serializable<UnitAnalysisMetadata>>; |
| 86 | + |
| 87 | + /** Merges all compilation unit data from previous analysis phases into a global metadata. */ |
| 88 | + abstract merge(units: UnitAnalysisMetadata[]): Promise<Serializable<CombinedGlobalMetadata>>; |
| 89 | + |
| 90 | + /** |
| 91 | + * Computes migration updates for the given TypeScript project, leveraging the global |
| 92 | + * metadata built up from all analyzed projects and their merged "unit data". |
| 93 | + */ |
| 94 | + abstract migrate( |
| 95 | + globalMetadata: CombinedGlobalMetadata, |
| 96 | + program: FullProgramInfo, |
| 97 | + ): Promise<Replacement[]>; |
| 98 | +} |
0 commit comments