Skip to content

Commit 44ba424

Browse files
authored
refactor(material/schematics): do not print hammer migration message when updating to v13 (angular#23752)
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.
1 parent 283d44d commit 44ba424

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/cdk/schematics/ng-update/devkit-migration-rule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function createMigrationSchematicRule(
115115
migrations.forEach(m => {
116116
const actionResult =
117117
isDevkitMigration(m) && m.globalPostMigration !== undefined
118-
? m.globalPostMigration(tree, context)
118+
? m.globalPostMigration(tree, targetVersion, context)
119119
: null;
120120
if (actionResult) {
121121
runPackageManager = runPackageManager || actionResult.runPackageManager;

src/cdk/schematics/ng-update/devkit-migration.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {SchematicContext, Tree} from '@angular-devkit/schematics';
1010
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
1111
import {Constructor, Migration, PostMigrationAction} from '../update-tool/migration';
12+
import {TargetVersion} from '../update-tool/target-version';
1213

1314
export type DevkitContext = {
1415
/** Devkit tree for the current migrations. Can be used to insert/remove files. */
@@ -34,7 +35,11 @@ export abstract class DevkitMigration<Data> extends Migration<Data, DevkitContex
3435
* migration result of all individual targets. e.g. removing HammerJS if it
3536
* is not needed in any project target.
3637
*/
37-
static globalPostMigration?(tree: Tree, context: SchematicContext): PostMigrationAction;
38+
static globalPostMigration?(
39+
tree: Tree,
40+
targetVersion: TargetVersion,
41+
context: SchematicContext,
42+
): PostMigrationAction;
3843
}
3944

4045
export type DevkitMigrationCtor<Data> = Constructor<DevkitMigration<Data>> &

src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {join, Path, relative, dirname} from '@angular-devkit/core';
9+
import {dirname, join, Path, relative} from '@angular-devkit/core';
1010
import {SchematicContext, Tree} from '@angular-devkit/schematics';
1111
import {
1212
addSymbolToNgModuleMetadata,
@@ -58,12 +58,12 @@ interface PackageJson {
5858
}
5959

6060
export class HammerGesturesMigration extends DevkitMigration<null> {
61-
// Only enable this rule if the migration targets v9 or v10 and is running for a non-test
62-
// target. We cannot migrate test targets since they have a limited scope
63-
// (in regards to source files) and therefore the HammerJS usage detection can be incorrect.
61+
// The migration is enabled when v9 or v10 are targeted, but actual targets are only
62+
// migrated if they are not test targets. We cannot migrate test targets since they have
63+
// a limited scope, in regards to their source files, and therefore the HammerJS usage
64+
// detection could be incorrect.
6465
enabled =
65-
(this.targetVersion === TargetVersion.V9 || this.targetVersion === TargetVersion.V10) &&
66-
!this.context.isTestTarget;
66+
HammerGesturesMigration._isAllowedVersion(this.targetVersion) && !this.context.isTestTarget;
6767

6868
private _printer = ts.createPrinter();
6969
private _importManager = new ImportManager(this.fileSystem, this._printer);
@@ -938,7 +938,16 @@ export class HammerGesturesMigration extends DevkitMigration<null> {
938938
* on the analysis of the individual targets. For example: we only remove Hammer
939939
* from the "package.json" if it is not used in *any* project target.
940940
*/
941-
static override globalPostMigration(tree: Tree, context: SchematicContext): PostMigrationAction {
941+
static override globalPostMigration(
942+
tree: Tree,
943+
target: TargetVersion,
944+
context: SchematicContext,
945+
): PostMigrationAction {
946+
// Skip printing any global messages when the target version is not allowed.
947+
if (!this._isAllowedVersion(target)) {
948+
return;
949+
}
950+
942951
// Always notify the developer that the Hammer v9 migration does not migrate tests.
943952
context.logger.info(
944953
'\n⚠ General notice: The HammerJS v9 migration for Angular Components is not able to ' +
@@ -979,6 +988,12 @@ export class HammerGesturesMigration extends DevkitMigration<null> {
979988
}
980989
return false;
981990
}
991+
992+
/** Gets whether the migration is allowed to run for specified target version. */
993+
private static _isAllowedVersion(target: TargetVersion) {
994+
// This migration is only allowed to run for v9 or v10 target versions.
995+
return target === TargetVersion.V9 || target === TargetVersion.V10;
996+
}
982997
}
983998

984999
/**

src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export class ThemingApiMigration extends DevkitMigration<null> {
4444
}
4545

4646
/** Logs out the number of migrated files at the end of the migration. */
47-
static override globalPostMigration(_tree: unknown, context: SchematicContext): void {
47+
static override globalPostMigration(
48+
_tree: unknown,
49+
_targetVersion: TargetVersion,
50+
context: SchematicContext,
51+
): void {
4852
const count = ThemingApiMigration.migratedFileCount;
4953

5054
if (count > 0) {

0 commit comments

Comments
 (0)