From 65e75424967774abe8e8358557ce0d26700e93b3 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Thu, 14 Oct 2021 17:31:43 +0200 Subject: [PATCH] refactor(material/schematics): do not print hammer migration message when updating to v13 The Hammer gesture migration runs only for v9 and v10 but the global post migration messages are printed regardless of whether the update runs for v9 or v10. This commit fixes that by providing the target version to the `globalPostMigration` functions for migrations. --- .../ng-update/devkit-migration-rule.ts | 2 +- .../schematics/ng-update/devkit-migration.ts | 7 ++++- .../hammer-gestures-migration.ts | 29 ++++++++++++++----- .../theming-api-v12/theming-api-migration.ts | 6 +++- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/cdk/schematics/ng-update/devkit-migration-rule.ts b/src/cdk/schematics/ng-update/devkit-migration-rule.ts index 238e46ff8acf..8b906b520582 100644 --- a/src/cdk/schematics/ng-update/devkit-migration-rule.ts +++ b/src/cdk/schematics/ng-update/devkit-migration-rule.ts @@ -115,7 +115,7 @@ export function createMigrationSchematicRule( migrations.forEach(m => { const actionResult = isDevkitMigration(m) && m.globalPostMigration !== undefined - ? m.globalPostMigration(tree, context) + ? m.globalPostMigration(tree, targetVersion, context) : null; if (actionResult) { runPackageManager = runPackageManager || actionResult.runPackageManager; diff --git a/src/cdk/schematics/ng-update/devkit-migration.ts b/src/cdk/schematics/ng-update/devkit-migration.ts index 0fd1ec9e2cf7..2d4fa8f0723a 100644 --- a/src/cdk/schematics/ng-update/devkit-migration.ts +++ b/src/cdk/schematics/ng-update/devkit-migration.ts @@ -9,6 +9,7 @@ import {SchematicContext, Tree} from '@angular-devkit/schematics'; import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; import {Constructor, Migration, PostMigrationAction} from '../update-tool/migration'; +import {TargetVersion} from '../update-tool/target-version'; export type DevkitContext = { /** Devkit tree for the current migrations. Can be used to insert/remove files. */ @@ -34,7 +35,11 @@ export abstract class DevkitMigration extends Migration = Constructor> & diff --git a/src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts b/src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts index 63e4e899f113..a493f22c1ad9 100644 --- a/src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts +++ b/src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {join, Path, relative, dirname} from '@angular-devkit/core'; +import {dirname, join, Path, relative} from '@angular-devkit/core'; import {SchematicContext, Tree} from '@angular-devkit/schematics'; import { addSymbolToNgModuleMetadata, @@ -58,12 +58,12 @@ interface PackageJson { } export class HammerGesturesMigration extends DevkitMigration { - // Only enable this rule if the migration targets v9 or v10 and is running for a non-test - // target. We cannot migrate test targets since they have a limited scope - // (in regards to source files) and therefore the HammerJS usage detection can be incorrect. + // The migration is enabled when v9 or v10 are targeted, but actual targets are only + // migrated if they are not test targets. We cannot migrate test targets since they have + // a limited scope, in regards to their source files, and therefore the HammerJS usage + // detection could be incorrect. enabled = - (this.targetVersion === TargetVersion.V9 || this.targetVersion === TargetVersion.V10) && - !this.context.isTestTarget; + HammerGesturesMigration._isAllowedVersion(this.targetVersion) && !this.context.isTestTarget; private _printer = ts.createPrinter(); private _importManager = new ImportManager(this.fileSystem, this._printer); @@ -938,7 +938,16 @@ export class HammerGesturesMigration extends DevkitMigration { * on the analysis of the individual targets. For example: we only remove Hammer * from the "package.json" if it is not used in *any* project target. */ - static override globalPostMigration(tree: Tree, context: SchematicContext): PostMigrationAction { + static override globalPostMigration( + tree: Tree, + target: TargetVersion, + context: SchematicContext, + ): PostMigrationAction { + // Skip printing any global messages when the target version is not allowed. + if (!this._isAllowedVersion(target)) { + return; + } + // Always notify the developer that the Hammer v9 migration does not migrate tests. context.logger.info( '\n⚠ General notice: The HammerJS v9 migration for Angular Components is not able to ' + @@ -979,6 +988,12 @@ export class HammerGesturesMigration extends DevkitMigration { } return false; } + + /** Gets whether the migration is allowed to run for specified target version. */ + private static _isAllowedVersion(target: TargetVersion) { + // This migration is only allowed to run for v9 or v10 target versions. + return target === TargetVersion.V9 || target === TargetVersion.V10; + } } /** diff --git a/src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts b/src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts index 6624617c281c..08387e7c1d24 100644 --- a/src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts +++ b/src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts @@ -44,7 +44,11 @@ export class ThemingApiMigration extends DevkitMigration { } /** Logs out the number of migrated files at the end of the migration. */ - static override globalPostMigration(_tree: unknown, context: SchematicContext): void { + static override globalPostMigration( + _tree: unknown, + _targetVersion: TargetVersion, + context: SchematicContext, + ): void { const count = ThemingApiMigration.migratedFileCount; if (count > 0) {