Skip to content

Commit 8087ccb

Browse files
devversionjelbourn
authored andcommitted
refactor(schematics): add entry point for v7 upgrade (#12884)
* 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.
1 parent f7b130c commit 8087ccb

File tree

5 files changed

+131
-71
lines changed

5 files changed

+131
-71
lines changed

src/lib/schematics/migration.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{
22
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json",
33
"schematics": {
4-
// Update from v5 to v6
54
"migration-01": {
6-
"version": "6.0.0-rc.12",
7-
"description": "Updates Angular Material from v5 to v6",
8-
"factory": "./update/update"
5+
"version": "6",
6+
"description": "Updates Angular Material to v6",
7+
"factory": "./update/index#updateToV6"
8+
},
9+
"migration-02": {
10+
"version": "7",
11+
"description": "Updates Angular Material to v7",
12+
"factory": "./update/index#updateToV7"
913
},
1014
"ng-post-update": {
1115
"description": "Performs cleanup after ng-update.",
12-
"factory": "./update/update#postUpdate",
16+
"factory": "./update/index#postUpdate",
1317
"private": true
1418
}
1519
}

src/lib/schematics/update/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Rule} from '@angular-devkit/schematics';
10+
import {createUpdateRule} from './update';
11+
12+
/** Possible versions that can be automatically migrated by `ng update`. */
13+
export enum TargetVersion {
14+
V6,
15+
V7
16+
}
17+
18+
/** Entry point for the migration schematics with target of Angular Material 6.0.0 */
19+
export function updateToV6(): Rule {
20+
return createUpdateRule(TargetVersion.V6);
21+
}
22+
23+
/** Entry point for the migration schematics with target of Angular Material 7.0.0 */
24+
export function updateToV7(): Rule {
25+
return createUpdateRule(TargetVersion.V7);
26+
}
27+
28+
/** Post-update schematic to be called when update is finished. */
29+
export function postUpdate(): Rule {
30+
return () => console.log(
31+
'\nComplete! Please check the output above for any issues that were detected but could not' +
32+
' be automatically fixed.');
33+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {join} from 'path';
10+
import {TargetVersion} from './index';
11+
12+
/** List of rules that need to be enabled when running the TSLint fix task. */
13+
const upgradeRules = [
14+
// Attribute selector update rules.
15+
'attribute-selectors-string-literal',
16+
'attribute-selectors-stylesheet',
17+
'attribute-selectors-template',
18+
19+
// Class name update rules
20+
'class-names-identifier',
21+
'class-names-identifier-misc',
22+
23+
// CSS selectors update rules
24+
'css-selectors-string-literal',
25+
'css-selectors-stylesheet',
26+
'css-selectors-template',
27+
28+
// Element selector update rules
29+
'element-selectors-string-literal',
30+
'element-selectors-stylesheet',
31+
'element-selectors-template',
32+
33+
// Input name update rules
34+
'input-names-stylesheet',
35+
'input-names-template',
36+
37+
// Output name update rules
38+
'output-names-template',
39+
40+
// Property name update rules
41+
'property-names-access',
42+
'property-names-misc',
43+
44+
// Method call checks
45+
'method-calls-check',
46+
47+
// Class inheritance
48+
'class-inheritance-check',
49+
'class-inheritance-misc',
50+
51+
// Additional misc rules.
52+
'check-import-misc',
53+
'check-template-misc'
54+
];
55+
56+
/** List of absolute paths that refer to directories that contain the upgrade rules. */
57+
const rulesDirectory = [
58+
// TODO(devversion): consider automatically resolving rule directories.
59+
join(__dirname, 'rules/'),
60+
join(__dirname, 'rules/attribute-selectors'),
61+
join(__dirname, 'rules/class-names'),
62+
join(__dirname, 'rules/class-inheritance'),
63+
join(__dirname, 'rules/input-names'),
64+
join(__dirname, 'rules/output-names'),
65+
join(__dirname, 'rules/css-selectors'),
66+
join(__dirname, 'rules/element-selectors'),
67+
join(__dirname, 'rules/property-names'),
68+
join(__dirname, 'rules/method-calls'),
69+
];
70+
71+
/**
72+
* Creates a TSLint configuration object that can be passed to the schematic `TSLintFixTask`.
73+
* Each rule will have the specified target version as option which can be used to swap out
74+
* the upgrade data based on the given target version.
75+
*/
76+
export function createTslintConfig(target: TargetVersion) {
77+
const rules = upgradeRules.reduce((result, ruleName) => {
78+
result[ruleName] = [true, target];
79+
return result;
80+
}, {});
81+
82+
return {rulesDirectory, rules};
83+
}

src/lib/schematics/update/update.spec.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lib/schematics/update/update.ts

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import {Rule, SchematicContext, TaskId, Tree} from '@angular-devkit/schematics';
1010
import {RunSchematicTask, TslintFixTask} from '@angular-devkit/schematics/tasks';
1111
import {getWorkspace} from '@schematics/angular/utility/config';
12-
import * as path from 'path';
12+
import {TargetVersion} from './index';
13+
import {createTslintConfig} from './tslint-update';
1314

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

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

27+
const tslintConfig = createTslintConfig(targetVersion);
28+
2629
for (const tsconfig of allTsConfigPaths) {
2730
// Run the update tslint rules.
28-
tslintFixTasks.push(context.addTask(new TslintFixTask({
29-
rulesDirectory: [
30-
path.join(__dirname, 'rules/'),
31-
path.join(__dirname, 'rules/attribute-selectors'),
32-
path.join(__dirname, 'rules/class-names'),
33-
path.join(__dirname, 'rules/class-inheritance'),
34-
path.join(__dirname, 'rules/input-names'),
35-
path.join(__dirname, 'rules/output-names'),
36-
path.join(__dirname, 'rules/css-selectors'),
37-
path.join(__dirname, 'rules/element-selectors'),
38-
path.join(__dirname, 'rules/property-names'),
39-
path.join(__dirname, 'rules/method-calls'),
40-
],
41-
rules: {
42-
// Attribute selector update rules.
43-
'attribute-selectors-string-literal': true,
44-
'attribute-selectors-stylesheet': true,
45-
'attribute-selectors-template': true,
46-
47-
// Class name update rules
48-
'class-names-identifier': true,
49-
'class-names-identifier-misc': true,
50-
51-
// CSS selectors update rules
52-
'css-selectors-string-literal': true,
53-
'css-selectors-stylesheet': true,
54-
'css-selectors-template': true,
55-
56-
// Element selector update rules
57-
'element-selectors-string-literal': true,
58-
'element-selectors-stylesheet': true,
59-
'element-selectors-template': true,
60-
61-
// Input name update rules
62-
'input-names-stylesheet': true,
63-
'input-names-template': true,
64-
65-
// Output name update rules
66-
'output-names-template': true,
67-
68-
// Property name update rules
69-
'property-names-access': true,
70-
'property-names-misc': true,
71-
72-
// Method call checks
73-
'method-calls-check': true,
74-
75-
// Class inheritance
76-
'class-inheritance-check': true,
77-
'class-inheritance-misc': true,
78-
79-
// Additional misc rules.
80-
'check-import-misc': true,
81-
'check-template-misc': true
82-
}
83-
}, {
31+
tslintFixTasks.push(context.addTask(new TslintFixTask(tslintConfig, {
8432
silent: false,
8533
ignoreErrors: true,
8634
tsConfigPath: tsconfig,
@@ -92,13 +40,6 @@ export default function(): Rule {
9240
};
9341
}
9442

95-
/** Post-update schematic to be called when update is finished. */
96-
export function postUpdate(): Rule {
97-
return () => console.log(
98-
'\nComplete! Please check the output above for any issues that were detected but could not' +
99-
' be automatically fixed.');
100-
}
101-
10243
/**
10344
* Gets all tsconfig paths from a CLI project by reading the workspace configuration
10445
* and looking for common tsconfig locations.

0 commit comments

Comments
 (0)