From 47f8444a37ea53f5d0a57bae756c481ccad07ac0 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Aug 2018 13:35:07 +0200 Subject: [PATCH] refactor(schematics): add entry point for v7 upgrade * Refactors the update schematics to support running `ng update` for V7 of Angular Material. Note that the actual upgrade data and general version distinction is not implemented yet. This is an initial commit that should allow the data distinction in future changes. --- src/lib/schematics/migration.json | 14 ++-- src/lib/schematics/update/index.ts | 33 +++++++++ src/lib/schematics/update/tslint-update.ts | 83 ++++++++++++++++++++++ src/lib/schematics/update/update.spec.ts | 1 - src/lib/schematics/update/update.ts | 71 ++---------------- 5 files changed, 131 insertions(+), 71 deletions(-) create mode 100644 src/lib/schematics/update/index.ts create mode 100644 src/lib/schematics/update/tslint-update.ts delete mode 100644 src/lib/schematics/update/update.spec.ts diff --git a/src/lib/schematics/migration.json b/src/lib/schematics/migration.json index e3c9030dbd1f..220b6c054783 100644 --- a/src/lib/schematics/migration.json +++ b/src/lib/schematics/migration.json @@ -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 } } diff --git a/src/lib/schematics/update/index.ts b/src/lib/schematics/update/index.ts new file mode 100644 index 000000000000..0532de8e40f4 --- /dev/null +++ b/src/lib/schematics/update/index.ts @@ -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.'); +} diff --git a/src/lib/schematics/update/tslint-update.ts b/src/lib/schematics/update/tslint-update.ts new file mode 100644 index 000000000000..5b9ce8a9b6a5 --- /dev/null +++ b/src/lib/schematics/update/tslint-update.ts @@ -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}; +} diff --git a/src/lib/schematics/update/update.spec.ts b/src/lib/schematics/update/update.spec.ts deleted file mode 100644 index f4ab32e3f008..000000000000 --- a/src/lib/schematics/update/update.spec.ts +++ /dev/null @@ -1 +0,0 @@ -describe('Material Update Tool', () => {}); diff --git a/src/lib/schematics/update/update.ts b/src/lib/schematics/update/update.ts index 91daf86a4daa..a67d5c45cf52 100644 --- a/src/lib/schematics/update/update.ts +++ b/src/lib/schematics/update/update.ts @@ -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); @@ -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, @@ -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.