Skip to content

refactor(schematics): add entry point for v7 upgrade #12884

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
14 changes: 9 additions & 5 deletions src/lib/schematics/migration.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
// Update from v5 to v6
"migration-01": {
"version": "6.0.0-rc.12",
"description": "Updates Angular Material from v5 to v6",
"factory": "./update/update"
"version": "6",
"description": "Updates Angular Material to v6",
"factory": "./update/index#updateToV6"
},
"migration-02": {
"version": "7",
"description": "Updates Angular Material to v7",
"factory": "./update/index#updateToV7"
},
"ng-post-update": {
"description": "Performs cleanup after ng-update.",
"factory": "./update/update#postUpdate",
"factory": "./update/index#postUpdate",
"private": true
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/lib/schematics/update/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Rule} from '@angular-devkit/schematics';
import {createUpdateRule} from './update';

/** Possible versions that can be automatically migrated by `ng update`. */
export enum TargetVersion {
V6,
V7
}

/** Entry point for the migration schematics with target of Angular Material 6.0.0 */
export function updateToV6(): Rule {
return createUpdateRule(TargetVersion.V6);
}

/** Entry point for the migration schematics with target of Angular Material 7.0.0 */
export function updateToV7(): Rule {
return createUpdateRule(TargetVersion.V7);
}

/** Post-update schematic to be called when update is finished. */
export function postUpdate(): Rule {
return () => console.log(
'\nComplete! Please check the output above for any issues that were detected but could not' +
' be automatically fixed.');
}
83 changes: 83 additions & 0 deletions src/lib/schematics/update/tslint-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {join} from 'path';
import {TargetVersion} from './index';

/** List of rules that need to be enabled when running the TSLint fix task. */
const upgradeRules = [
// Attribute selector update rules.
'attribute-selectors-string-literal',
'attribute-selectors-stylesheet',
'attribute-selectors-template',

// Class name update rules
'class-names-identifier',
'class-names-identifier-misc',

// CSS selectors update rules
'css-selectors-string-literal',
'css-selectors-stylesheet',
'css-selectors-template',

// Element selector update rules
'element-selectors-string-literal',
'element-selectors-stylesheet',
'element-selectors-template',

// Input name update rules
'input-names-stylesheet',
'input-names-template',

// Output name update rules
'output-names-template',

// Property name update rules
'property-names-access',
'property-names-misc',

// Method call checks
'method-calls-check',

// Class inheritance
'class-inheritance-check',
'class-inheritance-misc',

// Additional misc rules.
'check-import-misc',
'check-template-misc'
];

/** List of absolute paths that refer to directories that contain the upgrade rules. */
const rulesDirectory = [
// TODO(devversion): consider automatically resolving rule directories.
join(__dirname, 'rules/'),
join(__dirname, 'rules/attribute-selectors'),
join(__dirname, 'rules/class-names'),
join(__dirname, 'rules/class-inheritance'),
join(__dirname, 'rules/input-names'),
join(__dirname, 'rules/output-names'),
join(__dirname, 'rules/css-selectors'),
join(__dirname, 'rules/element-selectors'),
join(__dirname, 'rules/property-names'),
join(__dirname, 'rules/method-calls'),
];

/**
* Creates a TSLint configuration object that can be passed to the schematic `TSLintFixTask`.
* Each rule will have the specified target version as option which can be used to swap out
* the upgrade data based on the given target version.
*/
export function createTslintConfig(target: TargetVersion) {
const rules = upgradeRules.reduce((result, ruleName) => {
result[ruleName] = [true, target];
return result;
}, {});

return {rulesDirectory, rules};
}
1 change: 0 additions & 1 deletion src/lib/schematics/update/update.spec.ts

This file was deleted.

71 changes: 6 additions & 65 deletions src/lib/schematics/update/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import {Rule, SchematicContext, TaskId, Tree} from '@angular-devkit/schematics';
import {RunSchematicTask, TslintFixTask} from '@angular-devkit/schematics/tasks';
import {getWorkspace} from '@schematics/angular/utility/config';
import * as path from 'path';
import {TargetVersion} from './index';
import {createTslintConfig} from './tslint-update';

/** Entry point for `ng update` from Angular CLI. */
export default function(): Rule {
export function createUpdateRule(targetVersion: TargetVersion): Rule {
return (tree: Tree, context: SchematicContext) => {

const allTsConfigPaths = getTsConfigPaths(tree);
Expand All @@ -23,64 +24,11 @@ export default function(): Rule {
'Material repository that includes the name of your TypeScript configuration.');
}

const tslintConfig = createTslintConfig(targetVersion);

for (const tsconfig of allTsConfigPaths) {
// Run the update tslint rules.
tslintFixTasks.push(context.addTask(new TslintFixTask({
rulesDirectory: [
path.join(__dirname, 'rules/'),
path.join(__dirname, 'rules/attribute-selectors'),
path.join(__dirname, 'rules/class-names'),
path.join(__dirname, 'rules/class-inheritance'),
path.join(__dirname, 'rules/input-names'),
path.join(__dirname, 'rules/output-names'),
path.join(__dirname, 'rules/css-selectors'),
path.join(__dirname, 'rules/element-selectors'),
path.join(__dirname, 'rules/property-names'),
path.join(__dirname, 'rules/method-calls'),
],
rules: {
// Attribute selector update rules.
'attribute-selectors-string-literal': true,
'attribute-selectors-stylesheet': true,
'attribute-selectors-template': true,

// Class name update rules
'class-names-identifier': true,
'class-names-identifier-misc': true,

// CSS selectors update rules
'css-selectors-string-literal': true,
'css-selectors-stylesheet': true,
'css-selectors-template': true,

// Element selector update rules
'element-selectors-string-literal': true,
'element-selectors-stylesheet': true,
'element-selectors-template': true,

// Input name update rules
'input-names-stylesheet': true,
'input-names-template': true,

// Output name update rules
'output-names-template': true,

// Property name update rules
'property-names-access': true,
'property-names-misc': true,

// Method call checks
'method-calls-check': true,

// Class inheritance
'class-inheritance-check': true,
'class-inheritance-misc': true,

// Additional misc rules.
'check-import-misc': true,
'check-template-misc': true
}
}, {
tslintFixTasks.push(context.addTask(new TslintFixTask(tslintConfig, {
silent: false,
ignoreErrors: true,
tsConfigPath: tsconfig,
Expand All @@ -92,13 +40,6 @@ export default function(): Rule {
};
}

/** Post-update schematic to be called when update is finished. */
export function postUpdate(): Rule {
return () => console.log(
'\nComplete! Please check the output above for any issues that were detected but could not' +
' be automatically fixed.');
}

/**
* Gets all tsconfig paths from a CLI project by reading the workspace configuration
* and looking for common tsconfig locations.
Expand Down