From d3c01efef398cc1ef3e2d5b2a45f1e359a8b8d5d Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 19 Aug 2024 14:47:25 +0000 Subject: [PATCH] refactor(migrations): improve generic assignability in tsurge and pass more info (#57451) * Improves some of the generic assignability for tsurge. Anything is allowed to be returned from an overridden `prepareProgram` method. This is useful for the signal input migration. * Passes the absolute root paths to migrations. This is helpful for the signal input migration and there is no other way to access it. It's better to pass specifically, compared to passing the whole unsafe `ParsedConfiguration` object. PR Close #57451 --- packages/core/schematics/utils/tsurge/BUILD.bazel | 2 +- .../schematics/utils/tsurge/executors/analyze_exec.ts | 10 ++++++++-- .../schematics/utils/tsurge/executors/merge_exec.ts | 10 ++++++++-- .../schematics/utils/tsurge/executors/migrate_exec.ts | 10 ++++++++-- .../schematics/utils/tsurge/helpers/ngtsc_program.ts | 1 + packages/core/schematics/utils/tsurge/migration.ts | 10 +++++----- packages/core/schematics/utils/tsurge/program_info.ts | 1 + 7 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/core/schematics/utils/tsurge/BUILD.bazel b/packages/core/schematics/utils/tsurge/BUILD.bazel index 53d46a4ccf5..233b7b2cad8 100644 --- a/packages/core/schematics/utils/tsurge/BUILD.bazel +++ b/packages/core/schematics/utils/tsurge/BUILD.bazel @@ -4,7 +4,7 @@ ts_library( name = "tsurge", srcs = glob(["**/*.ts"]), visibility = [ - "//packages/core/schematics/utils/tsurge/test:__pkg__", + "//packages/core/schematics:__subpackages__", ], deps = [ "//packages/compiler-cli", diff --git a/packages/core/schematics/utils/tsurge/executors/analyze_exec.ts b/packages/core/schematics/utils/tsurge/executors/analyze_exec.ts index 59714aff232..63c9e2801ed 100644 --- a/packages/core/schematics/utils/tsurge/executors/analyze_exec.ts +++ b/packages/core/schematics/utils/tsurge/executors/analyze_exec.ts @@ -8,6 +8,8 @@ import {TsurgeMigration} from '../migration'; import {Serializable} from '../helpers/serializable'; +import ts from 'typescript'; +import {NgtscProgram} from '../../../../../compiler-cli/src/ngtsc/program'; /** * Executes the analyze phase of the given migration against @@ -15,8 +17,12 @@ import {Serializable} from '../helpers/serializable'; * * @returns the serializable migration unit data. */ -export async function executeAnalyzePhase( - migration: TsurgeMigration, +export async function executeAnalyzePhase< + UnitData, + GlobalData, + TsProgramType extends ts.Program | NgtscProgram, +>( + migration: TsurgeMigration, tsconfigAbsolutePath: string, ): Promise> { const baseInfo = migration.createProgram(tsconfigAbsolutePath); diff --git a/packages/core/schematics/utils/tsurge/executors/merge_exec.ts b/packages/core/schematics/utils/tsurge/executors/merge_exec.ts index 7dbe5e037d0..b07f1c46a2b 100644 --- a/packages/core/schematics/utils/tsurge/executors/merge_exec.ts +++ b/packages/core/schematics/utils/tsurge/executors/merge_exec.ts @@ -6,8 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ +import ts from 'typescript'; import {Serializable} from '../helpers/serializable'; import {TsurgeMigration} from '../migration'; +import {NgtscProgram} from '../../../../../compiler-cli/src/ngtsc/program'; /** * Executes the merge phase for the given migration against @@ -15,8 +17,12 @@ import {TsurgeMigration} from '../migration'; * * @returns the serializable migration global data. */ -export async function executeMergePhase( - migration: TsurgeMigration, +export async function executeMergePhase< + UnitData, + GlobalData, + TsProgramType extends ts.Program | NgtscProgram, +>( + migration: TsurgeMigration, units: UnitData[], ): Promise> { return await migration.merge(units); diff --git a/packages/core/schematics/utils/tsurge/executors/migrate_exec.ts b/packages/core/schematics/utils/tsurge/executors/migrate_exec.ts index 48c9f441330..1af78f89813 100644 --- a/packages/core/schematics/utils/tsurge/executors/migrate_exec.ts +++ b/packages/core/schematics/utils/tsurge/executors/migrate_exec.ts @@ -8,6 +8,8 @@ import {TsurgeMigration} from '../migration'; import {Replacement} from '../replacement'; +import {NgtscProgram} from '../../../../../compiler-cli/src/ngtsc/program'; +import ts from 'typescript'; /** * Executes the migrate phase of the given migration against @@ -18,8 +20,12 @@ import {Replacement} from '../replacement'; * * @returns a list of text replacements to apply to disk. */ -export async function executeMigratePhase( - migration: TsurgeMigration, +export async function executeMigratePhase< + UnitData, + GlobalData, + TsProgramType extends ts.Program | NgtscProgram, +>( + migration: TsurgeMigration, globalMetadata: GlobalData, tsconfigAbsolutePath: string, ): Promise { diff --git a/packages/core/schematics/utils/tsurge/helpers/ngtsc_program.ts b/packages/core/schematics/utils/tsurge/helpers/ngtsc_program.ts index b6b02fae633..2d1eec04538 100644 --- a/packages/core/schematics/utils/tsurge/helpers/ngtsc_program.ts +++ b/packages/core/schematics/utils/tsurge/helpers/ngtsc_program.ts @@ -59,6 +59,7 @@ export function createNgtscProgram( return { program: ngtscProgram, userOptions: tsconfig.options, + programAbsoluteRootPaths: tsconfig.rootNames, tsconfigAbsolutePath: absoluteTsconfigPath, }; } diff --git a/packages/core/schematics/utils/tsurge/migration.ts b/packages/core/schematics/utils/tsurge/migration.ts index 5ef2cdc5465..aaea31a6356 100644 --- a/packages/core/schematics/utils/tsurge/migration.ts +++ b/packages/core/schematics/utils/tsurge/migration.ts @@ -48,7 +48,7 @@ export abstract class TsurgeMigration< UnitAnalysisMetadata, CombinedGlobalMetadata, TsProgramType extends ts.Program | NgtscProgram = NgtscProgram, - FullProgramInfo extends ProgramInfo = ProgramInfo, + PreparationInfo = ProgramInfo, > { // By default, ngtsc programs are being created. createProgram(tsconfigAbsPath: string, fs?: FileSystem): BaseProgramInfo { @@ -57,7 +57,7 @@ export abstract class TsurgeMigration< // Optional function to prepare the base `ProgramInfo` even further, // for the analyze and migrate phases. E.g. determining source files. - prepareProgram(info: BaseProgramInfo): FullProgramInfo { + prepareProgram(info: BaseProgramInfo): PreparationInfo { assert(info.program instanceof NgtscProgram); const userProgram = info.program.getTsProgram(); @@ -78,11 +78,11 @@ export abstract class TsurgeMigration< sourceFiles, fullProgramSourceFiles, projectDirAbsPath, - } as FullProgramInfo; + } as PreparationInfo; } /** Analyzes the given TypeScript project and returns serializable compilation unit data. */ - abstract analyze(program: FullProgramInfo): Promise>; + abstract analyze(program: PreparationInfo): Promise>; /** Merges all compilation unit data from previous analysis phases into a global metadata. */ abstract merge(units: UnitAnalysisMetadata[]): Promise>; @@ -93,6 +93,6 @@ export abstract class TsurgeMigration< */ abstract migrate( globalMetadata: CombinedGlobalMetadata, - program: FullProgramInfo, + program: PreparationInfo, ): Promise; } diff --git a/packages/core/schematics/utils/tsurge/program_info.ts b/packages/core/schematics/utils/tsurge/program_info.ts index 2a16e4817c9..cc68c23ae83 100644 --- a/packages/core/schematics/utils/tsurge/program_info.ts +++ b/packages/core/schematics/utils/tsurge/program_info.ts @@ -19,6 +19,7 @@ import ts from 'typescript'; export interface BaseProgramInfo { program: T; userOptions: NgCompilerOptions; + programAbsoluteRootPaths: string[]; tsconfigAbsolutePath: string; }