Skip to content

fix(ng-update): better detection for workspace project in v9 hammer migration #18525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cdk/schematics/update-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export function runMigrationRules<T>(
// Create instances of all specified migration rules.
for (const ruleCtor of ruleTypes) {
const rule = new ruleCtor(
program, typeChecker, targetVersion, upgradeData, tree, getUpdateRecorder, basePath, logger,
isTestTarget, tsconfigPath);
project, program, typeChecker, targetVersion, upgradeData, tree, getUpdateRecorder,
basePath, logger, isTestTarget, tsconfigPath);
rule.init();
if (rule.ruleEnabled) {
rules.push(rule);
Expand Down
3 changes: 3 additions & 0 deletions src/cdk/schematics/update-tool/migration-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {logging} from '@angular-devkit/core';
import {SchematicContext, Tree, UpdateRecorder} from '@angular-devkit/schematics';
import {WorkspaceProject} from '@schematics/angular/utility/workspace-models';
import * as ts from 'typescript';
import {ResolvedResource} from './component-resource-collector';
import {TargetVersion} from './target-version';
Expand All @@ -32,6 +33,8 @@ export class MigrationRule<T> {
ruleEnabled = true;

constructor(
/** Workspace project the migration rule runs against. */
public project: WorkspaceProject,
/** TypeScript program for the migration. */
public program: ts.Program,
/** TypeChecker instance for the analysis program. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ describe('v9 HammerJS removal', () => {
`);
}

it('should not throw if project tsconfig does not have explicit root file names', async () => {
// Generates a second project in the workspace. This is necessary to ensure that the
// migration runs logic to determine the correct workspace project.
await runner.runExternalSchematicAsync(
'@schematics/angular', 'application', {name: 'second-project'}, tree).toPromise();
// Overwrite the default tsconfig to not specify any explicit source files. This replicates
// the scenario observed in: https://github.com/angular/components/issues/18504.
writeFile('/projects/cdk-testing/tsconfig.app.json', JSON.stringify({
extends: '../../tsconfig.json',
compilerOptions: {
outDir: '../../out-tsc/app',
types: []
}}
));
addPackageToPackageJson(tree, 'hammerjs', '0.0.0');
await expectAsync(runMigration()).not.toBeRejected();
});

describe('hammerjs not used', () => {
it('should remove hammerjs from "package.json" file', async () => {
addPackageToPackageJson(tree, 'hammerjs', '0.0.0');
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
normalize as devkitNormalize,
Path as DevkitPath
} from '@angular-devkit/core';
import {SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {SchematicContext, Tree} from '@angular-devkit/schematics';
import {
getImportOfIdentifier,
getProjectIndexFiles,
Expand All @@ -27,13 +27,11 @@ import {
getMetadataField
} from '@angular/cdk/schematics';
import {InsertChange} from '@schematics/angular/utility/change';
import {getWorkspace} from '@schematics/angular/utility/config';
import {WorkspaceProject} from '@schematics/angular/utility/workspace-models';
import {readFileSync} from 'fs';
import {dirname, join, relative} from 'path';
import * as ts from 'typescript';

import {getProjectFromProgram} from './cli-workspace';
import {findHammerScriptImportElements} from './find-hammer-script-tags';
import {findMainModuleExpression} from './find-main-module';
import {isHammerJsUsedInTemplate} from './hammer-template-check';
Expand Down Expand Up @@ -242,8 +240,7 @@ export class HammerGesturesRule extends MigrationRule<null> {
* 4) Setup the "HammerModule" in the root app module (if not done already).
*/
private _setupHammerWithCustomEvents() {
const project = this._getProjectOrThrow();
const sourceRoot = devkitNormalize(project.sourceRoot || project.root);
const sourceRoot = devkitNormalize(this.project.sourceRoot || this.project.root);
const newConfigPath =
devkitJoin(sourceRoot, this._getAvailableGestureConfigFileName(sourceRoot));

Expand All @@ -261,20 +258,18 @@ export class HammerGesturesRule extends MigrationRule<null> {
// Setup the gesture config provider and the "HammerModule" in the root module
// if not done already. The "HammerModule" is needed in v9 since it enables the
// Hammer event plugin that was previously enabled by default in v8.
this._setupNewGestureConfigInRootModule(project, newConfigPath);
this._setupHammerModuleInRootModule(project);
this._setupNewGestureConfigInRootModule(newConfigPath);
this._setupHammerModuleInRootModule();
}

/**
* Sets up the standard hammer module in the project and removes all
* references to the deprecated Angular Material gesture config.
*/
private _setupHammerWithStandardEvents() {
const project = this._getProjectOrThrow();

// Setup the HammerModule. The HammerModule enables support for
// the standard HammerJS events.
this._setupHammerModuleInRootModule(project);
this._setupHammerModuleInRootModule();
this._removeMaterialGestureConfigSetup();
}

Expand All @@ -285,13 +280,11 @@ export class HammerGesturesRule extends MigrationRule<null> {
* 3) Remove "hammerjs" from all index HTML files of the current project.
*/
private _removeHammerSetup() {
const project = this._getProjectOrThrow();

this._installImports.forEach(i => this._importManager.deleteImportByDeclaration(i));

this._removeMaterialGestureConfigSetup();
this._removeHammerModuleReferences();
this._removeHammerFromIndexFile(project);
this._removeHammerFromIndexFile(this.project);
}

/**
Expand Down Expand Up @@ -645,8 +638,8 @@ export class HammerGesturesRule extends MigrationRule<null> {
}

/** Sets up the Hammer gesture config in the root module if needed. */
private _setupNewGestureConfigInRootModule(project: WorkspaceProject, gestureConfigPath: string) {
const mainFilePath = join(this.basePath, getProjectMainFile(project));
private _setupNewGestureConfigInRootModule(gestureConfigPath: string) {
const mainFilePath = join(this.basePath, getProjectMainFile(this.project));
const rootModuleSymbol = this._getRootModuleSymbol(mainFilePath);

if (rootModuleSymbol === null) {
Expand Down Expand Up @@ -722,8 +715,8 @@ export class HammerGesturesRule extends MigrationRule<null> {
}

/** Sets up the "HammerModule" in the root module of the project. */
private _setupHammerModuleInRootModule(project: WorkspaceProject) {
const mainFilePath = join(this.basePath, getProjectMainFile(project));
private _setupHammerModuleInRootModule() {
const mainFilePath = join(this.basePath, getProjectMainFile(this.project));
const rootModuleSymbol = this._getRootModuleSymbol(mainFilePath);

if (rootModuleSymbol === null) {
Expand Down Expand Up @@ -820,23 +813,6 @@ export class HammerGesturesRule extends MigrationRule<null> {
});
}

/**
* Gets the project from the current program or throws if no project
* could be found.
*/
private _getProjectOrThrow(): WorkspaceProject {
const workspace = getWorkspace(this.tree);
const project = getProjectFromProgram(workspace, this.program);

if (!project) {
throw new SchematicsException(
'Could not find project to perform HammerJS v9 migration. ' +
'Please ensure your workspace configuration defines a project.');
}

return project;
}

/** Global state of whether Hammer is used in any analyzed project target. */
static globalUsesHammer = false;

Expand Down