|
6 | 6 | * found in the LICENSE file at https://angular.dev/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {Rule, SchematicContext} from '@angular-devkit/schematics'; |
| 9 | +import {chain, Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; |
10 | 10 | import {
|
| 11 | + appendHtmlElementToHead, |
11 | 12 | createMigrationSchematicRule,
|
| 13 | + getProjectFromWorkspace, |
| 14 | + getProjectIndexFiles, |
| 15 | + getWorkspaceConfigGracefully, |
12 | 16 | NullableDevkitMigration,
|
13 | 17 | TargetVersion,
|
14 | 18 | } from '@angular/cdk/schematics';
|
| 19 | +import {getWorkspace} from '@schematics/angular/utility/workspace'; |
15 | 20 |
|
16 | 21 | import {materialUpgradeData} from './upgrade-data';
|
17 | 22 |
|
18 | 23 | const materialMigrations: NullableDevkitMigration[] = [];
|
19 | 24 |
|
20 | 25 | /** Entry point for the migration schematics with target of Angular Material v20 */
|
21 | 26 | export function updateToV20(): Rule {
|
22 |
| - return createMigrationSchematicRule( |
23 |
| - TargetVersion.V20, |
24 |
| - materialMigrations, |
25 |
| - materialUpgradeData, |
26 |
| - onMigrationComplete, |
27 |
| - ); |
| 27 | + return chain([ |
| 28 | + createMigrationSchematicRule( |
| 29 | + TargetVersion.V20, |
| 30 | + materialMigrations, |
| 31 | + materialUpgradeData, |
| 32 | + onMigrationComplete, |
| 33 | + ), |
| 34 | + // Updating to the new Material Symbols isn't a migration within materialMigrations since |
| 35 | + // the index files are never visited within the migration schematic rule. The |
| 36 | + // migrate() function within the update-tool only visits files referenced in |
| 37 | + // typescript files which excludes the index template files: |
| 38 | + // https://github.com/angular/components/blob/main/src/cdk/schematics/update-tool/index.ts#L71. |
| 39 | + updateIconFontToMaterialSymbolsRule(), |
| 40 | + ]); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Finds the index files and adds the import for Material Symbols font if needed. As of v20, |
| 45 | + * Material Symbols becomes the default font icon since Material Icons is deprecated. This |
| 46 | + * rule ensures the Material Symbols font is imported for existing applications. |
| 47 | + * @returns Rule that adds the import for the Material Symbols icon font to the index files |
| 48 | + */ |
| 49 | +function updateIconFontToMaterialSymbolsRule(): Rule { |
| 50 | + return async (tree: Tree, context: SchematicContext) => { |
| 51 | + const workspace = await getWorkspaceConfigGracefully(tree); |
| 52 | + const projectNames = workspace!.projects.keys(); |
| 53 | + |
| 54 | + let indexFiles: string[] = []; |
| 55 | + for (const projectName of projectNames) { |
| 56 | + const project = getProjectFromWorkspace(await getWorkspace(tree), projectName); |
| 57 | + indexFiles = [...indexFiles, ...getProjectIndexFiles(project)]; |
| 58 | + } |
| 59 | + |
| 60 | + const materialSymbolsFont = |
| 61 | + 'https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined'; |
| 62 | + for (const indexFile of indexFiles) { |
| 63 | + // Add Material Symbols font if not imported in index file. References to the deprecated |
| 64 | + // Material Icons are not removed since some applications may have manual overrides in their |
| 65 | + // component styles that still reference it. |
| 66 | + if (!tree.read(indexFile)?.includes(materialSymbolsFont)) { |
| 67 | + appendHtmlElementToHead( |
| 68 | + tree, |
| 69 | + indexFile, |
| 70 | + `<link href="${materialSymbolsFont}" rel="stylesheet">`, |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
28 | 75 | }
|
29 | 76 |
|
30 | 77 | /** Function that will be called when the migration completed. */
|
|
0 commit comments