Skip to content

Commit

Permalink
fix(material/theming): Add schematic to update token names in overrid…
Browse files Browse the repository at this point in the history
…e mixins
  • Loading branch information
mmalerba committed Jul 8, 2024
1 parent 2d469a8 commit 527df18
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/cdk/schematics/update-tool/target-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// tslint:disable-next-line:prefer-const-enum
export enum TargetVersion {
V18 = 'version 18',
V19 = 'version 19',
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/material/button/_fab-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@
@include _theme-from-tokens(inspection.get-theme-tokens($theme, typography));
} @else {
@include sass-utils.current-selector-or-root() {
@include mdc-extended-fab-theme.theme(tokens-mdc-extended-fab.get-typography-tokens($theme));
@include token-utils.create-token-values(
tokens-mdc-extended-fab.$prefix,
tokens-mdc-extended-fab.get-typography-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mat-fab.$prefix,
tokens-mat-fab.get-typography-tokens($theme)
Expand Down Expand Up @@ -257,9 +260,13 @@
tokens-mat-fab-small.$prefix,
$options...
);
@include mdc-extended-fab-theme.theme($mdc-extended-fab-tokens);
@include mdc-fab-theme.theme($mdc-fab-tokens);
@include mdc-fab-small-theme.theme($mdc-fab-small-tokens);

@include token-utils.create-token-values(
tokens-mdc-extended-fab.$prefix,
$mdc-extended-fab-tokens
);
@include token-utils.create-token-values(tokens-mdc-fab.$prefix, $mdc-fab-tokens);
@include token-utils.create-token-values(tokens-mdc-fab-small.$prefix, $mdc-fab-small-tokens);
@include token-utils.create-token-values(tokens-mat-fab.$prefix, $mat-fab-tokens);
@include token-utils.create-token-values(tokens-mat-fab-small.$prefix, $mat-fab-small-tokens);
}
6 changes: 3 additions & 3 deletions src/material/core/theming/tests/m3-theme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('M3 theme', () => {
(
(namespace: (mat, minimal-pseudo-checkbox), prefix: 'minimal-'),
(mat, full-pseudo-checkbox)
),
),
(
minimal-selected-checkmark-color: magenta,
selected-checkmark-color: cyan
Expand All @@ -310,7 +310,7 @@ describe('M3 theme', () => {
$theme: token-utils.extend-theme($theme,
(
(namespace: (mat, minimal-pseudo-checkbox), prefix: 'minimal-'),
),
),
(
selected-checkmark-color: magenta
)
Expand All @@ -334,7 +334,7 @@ describe('M3 theme', () => {
(
(namespace: (mat, minimal-pseudo-checkbox), prefix: 'both-'),
(namespace: (mat, full-pseudo-checkbox), prefix: 'both-')
),
),
(
both-selected-checkmark-color: magenta
)
Expand Down
5 changes: 5 additions & 0 deletions src/material/schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"version": "18.0.0-0",
"description": "Updates Angular Material to v18",
"factory": "./ng-update/index_bundled#updateToV18"
},
"migration-v19": {
"version": "19.0.0-0",
"description": "Updates Angular Material to v19",
"factory": "./ng-update/index_bundled#updateToV19"
}
}
}
20 changes: 16 additions & 4 deletions src/material/schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@

import {Rule, SchematicContext} from '@angular-devkit/schematics';
import {
createMigrationSchematicRule,
NullableDevkitMigration,
TargetVersion,
createMigrationSchematicRule,
} from '@angular/cdk/schematics';

import {materialUpgradeData} from './upgrade-data';
import {M2ThemingMigration} from './migrations/m2-theming-v18';
import {TokenOverridesMigration} from './migrations/token-overrides-v19';
import {materialUpgradeData} from './upgrade-data';

const materialMigrations: NullableDevkitMigration[] = [M2ThemingMigration];
const materialMigrationsV18: NullableDevkitMigration[] = [M2ThemingMigration];
const materialMigrationsV19: NullableDevkitMigration[] = [TokenOverridesMigration];

/** Entry point for the migration schematics with target of Angular Material v18 */
export function updateToV18(): Rule {
return createMigrationSchematicRule(
TargetVersion.V18,
materialMigrations,
materialMigrationsV18,
materialUpgradeData,
onMigrationComplete,
);
}

/** Entry point for the migration schematics with target of Angular Material v19 */
export function updateToV19(): Rule {
return createMigrationSchematicRule(
TargetVersion.V19,
materialMigrationsV19,
materialUpgradeData,
onMigrationComplete,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @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 {extname} from '@angular-devkit/core';
import {DevkitMigration, ResolvedResource, TargetVersion} from '@angular/cdk/schematics';
import {migrateTokenOverridesUsages} from './migration';

/** Migration that updates usages of the token overrides APIs in v19. */
export class TokenOverridesMigration extends DevkitMigration<null> {
private _potentialThemes: ResolvedResource[] = [];

/** Whether to run this migration. */
enabled = this.targetVersion === TargetVersion.V19;

override visitStylesheet(stylesheet: ResolvedResource): void {
if (
extname(stylesheet.filePath) === '.scss' &&
// Note: intended to exclude `@angular/material-experimental`.
stylesheet.content.match(/@angular\/material["']/)
) {
this._potentialThemes.push(stylesheet);
}
}

override postAnalysis(): void {
for (const theme of this._potentialThemes) {
const migrated = migrateTokenOverridesUsages(theme.content);

if (migrated !== theme.content) {
this.fileSystem
.edit(theme.filePath)
.remove(0, theme.content.length)
.insertLeft(0, migrated);
this.fileSystem.commitEdits();
}
}
}
}
Loading

0 comments on commit 527df18

Please sign in to comment.