-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(material/schematics): add schematic to rename tokens #31051
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
andrewseguin
merged 4 commits into
angular:main
from
andrewseguin:rename-mdc-prefix-schematic
May 8, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa1fada
fix(material/schematics): add schematic to rename tokens
andrewseguin 302df51
fix(material/schematics): add schematic to rename tokens
andrewseguin c9db72a
fix(material/schematics): fix rename tests
e628565
fix(material/schematic): rename variables
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import {Rule, SchematicContext} from '@angular-devkit/schematics'; | ||
import {chain, Rule, SchematicContext} from '@angular-devkit/schematics'; | ||
import { | ||
createMigrationSchematicRule, | ||
NullableDevkitMigration, | ||
|
@@ -22,14 +22,72 @@ const materialMigrations: NullableDevkitMigration[] = [ | |
ExplicitSystemVariablePrefixMigration, | ||
]; | ||
|
||
/** Entry point for the migration schematics with target of Angular Material v19 */ | ||
/** Entry point for the migration schematics with target of Angular Material v20 */ | ||
export function updateToV20(): Rule { | ||
return createMigrationSchematicRule( | ||
TargetVersion.V20, | ||
materialMigrations, | ||
materialUpgradeData, | ||
onMigrationComplete, | ||
); | ||
return chain([ | ||
createMigrationSchematicRule( | ||
TargetVersion.V20, | ||
materialMigrations, | ||
materialUpgradeData, | ||
onMigrationComplete, | ||
), | ||
renameMdcTokens(), | ||
renameComponentTokens(), | ||
]); | ||
} | ||
|
||
// Renames any CSS variables beginning with "--mdc-" to be "--mat-". These CSS variables | ||
// refer to tokens that used to be derived from a mix of MDC and Angular. Now all the tokens | ||
// are converged on being prefixed "--mat-". | ||
function renameMdcTokens(): Rule { | ||
return tree => { | ||
tree.visit(path => { | ||
const content = tree.readText(path); | ||
const updatedContent = content.replace('--mdc-', '--mat-'); | ||
tree.overwrite(path, updatedContent); | ||
}); | ||
}; | ||
} | ||
|
||
// Renames Angular Material component token CSS variables that were renamed so that the base | ||
// component's name came first or otherwise renamed to match our terminology instead of MDC's. | ||
function renameComponentTokens(): Rule { | ||
const replacements = [ | ||
{oldStr: '--mat-circular-progress', newStr: '--mat-progress-spinner'}, | ||
{oldStr: '--mat-elevated-card', newStr: '--mat-card-elevated'}, | ||
{oldStr: '--mat-extended-fab', newStr: '--mat-fab-extended'}, | ||
{oldStr: '--mat-filled-button', newStr: '--mat-button-filled'}, | ||
{oldStr: '--mat-filled-text-field', newStr: '--mat-form-field-filled'}, | ||
{oldStr: '--mat-full-pseudo-checkbox', newStr: '--mat-pseudo-checkbox-full'}, | ||
{oldStr: '--mat-legacy-button-toggle', newStr: '--mat-button-toggle-legacy'}, | ||
{oldStr: '--mat-linear-progress', newStr: '--mat-progress-bar'}, | ||
{oldStr: '--mat-minimal-pseudo-checkbox', newStr: '--mat-pseudo-checkbox-minimal'}, | ||
{oldStr: '--mat-outlined-button', newStr: '--mat-button-outlined'}, | ||
{oldStr: '--mat-outlined-card', newStr: '--mat-card-outlined'}, | ||
{oldStr: '--mat-outlined-text-field', newStr: '--mat-form-field-outlined'}, | ||
{oldStr: '--mat-plain-tooltip', newStr: '--mat-tooltip'}, | ||
{oldStr: '--mat-protected-button', newStr: '--mat-button-protected'}, | ||
{oldStr: '--mat-secondary-navigation-tab', newStr: '--mat-tab'}, | ||
{oldStr: '--mat-standard-button-toggle', newStr: '--mat-button-toggle'}, | ||
{oldStr: '--mat-switch', newStr: '--mat-slide-toggle'}, | ||
{oldStr: '--mat-tab-header', newStr: '--mat-tab'}, | ||
{oldStr: '--mat-tab-header-with-background', newStr: '--mat-tab'}, | ||
{oldStr: '--mat-tab-indicator', newStr: '--mat-tab'}, | ||
{oldStr: '--mat-text-button', newStr: '--mat-button-text'}, | ||
{oldStr: '--mat-tonal-button', newStr: '--mat-button-tonal'}, | ||
|
||
]; | ||
return tree => { | ||
tree.visit(path => { | ||
const content = tree.readText(path); | ||
let updatedContent = content; | ||
for (const replacement of replacements) { | ||
updatedContent = updatedContent.replace(replacement.oldStr, replacement.newStr); | ||
} | ||
if (content !== updatedContent) { | ||
tree.overwrite(path, updatedContent); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** Function that will be called when the migration completed. */ | ||
|
51 changes: 51 additions & 0 deletions
51
src/material/schematics/ng-update/test-cases/rename-mdc-tokens.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createTestCaseSetup} from '../../../../cdk/schematics/testing'; | ||
import {MIGRATION_PATH} from '../../paths'; | ||
|
||
const THEME_FILE_PATH = '/projects/cdk-testing/src/theme.scss'; | ||
|
||
describe('v20 rename tokens migration', () => { | ||
let tree: UnitTestTree; | ||
let writeFile: (filename: string, content: string) => void; | ||
let runMigration: () => Promise<unknown>; | ||
|
||
function stripWhitespace(content: string): string { | ||
return content.replace(/\s/g, ''); | ||
} | ||
|
||
beforeEach(async () => { | ||
const testSetup = await createTestCaseSetup('migration-v20', MIGRATION_PATH, []); | ||
tree = testSetup.appTree; | ||
writeFile = testSetup.writeFile; | ||
runMigration = testSetup.runFixers; | ||
}); | ||
|
||
it('should rename mdc tokens to mat and change component ordering', async () => { | ||
writeFile( | ||
THEME_FILE_PATH, | ||
` | ||
html { | ||
--mdc-icon-button-icon-size: 24px; | ||
--mat-filled-button-color: red; | ||
--mat-filled-text-field-color: red; | ||
--mat-full-pseudo-checkbox-color: red; | ||
--mat-legacy-button-toggle-color: red; | ||
} | ||
`, | ||
); | ||
|
||
await runMigration(); | ||
|
||
expect(stripWhitespace(tree.readText(THEME_FILE_PATH))).toBe( | ||
stripWhitespace(` | ||
html { | ||
--mat-icon-button-icon-size: 24px; | ||
--mat-button-filled-color: red; | ||
--mat-form-field-filled-color: red; | ||
--mat-pseudo-checkbox-full-color: red; | ||
--mat-button-toggle-legacy-color: red; | ||
} | ||
`), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.