Skip to content

Commit de301a8

Browse files
committed
fix(material/schematics): respect specified directory in mdc migration (#25810)
(cherry picked from commit 03408fd)
1 parent 4926cc5 commit de301a8

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

src/cdk/schematics/update-tool/index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ export class UpdateProject<Context> {
5050
* @param data Upgrade data that is passed to all migration rules.
5151
* @param additionalStylesheetPaths Additional stylesheets that should be migrated, if not
5252
* referenced in an Angular component. This is helpful for global stylesheets in a project.
53+
* @param limitToDirectory If specified, changes will be limited to the given directory.
5354
*/
5455
migrate<Data>(
5556
migrationTypes: MigrationCtor<Data, Context>[],
5657
target: TargetVersion | null,
5758
data: Data,
5859
additionalStylesheetPaths?: string[],
60+
limitToDirectory?: string,
5961
): {hasFailures: boolean} {
62+
limitToDirectory &&= this._fileSystem.resolve(limitToDirectory);
63+
6064
// Create instances of the specified migrations.
6165
const migrations = this._createMigrations(migrationTypes, target, data);
6266
// Creates the component resource collector. The collector can visit arbitrary
@@ -65,9 +69,14 @@ export class UpdateProject<Context> {
6569
const resourceCollector = new ComponentResourceCollector(this._typeChecker, this._fileSystem);
6670
// Collect all of the TypeScript source files we want to migrate. We don't
6771
// migrate type definition files, or source files from external libraries.
68-
const sourceFiles = this._program
69-
.getSourceFiles()
70-
.filter(f => !f.isDeclarationFile && !this._program.isSourceFileFromExternalLibrary(f));
72+
const sourceFiles = this._program.getSourceFiles().filter(f => {
73+
return (
74+
!f.isDeclarationFile &&
75+
(limitToDirectory == null ||
76+
this._fileSystem.resolve(f.fileName).startsWith(limitToDirectory)) &&
77+
!this._program.isSourceFileFromExternalLibrary(f)
78+
);
79+
});
7180

7281
// Helper function that visits a given TypeScript node and collects all referenced
7382
// component resources (i.e. stylesheets or templates). Additionally, the helper
@@ -121,11 +130,13 @@ export class UpdateProject<Context> {
121130
if (additionalStylesheetPaths) {
122131
additionalStylesheetPaths.forEach(filePath => {
123132
const resolvedPath = this._fileSystem.resolve(filePath);
124-
const stylesheet = resourceCollector.resolveExternalStylesheet(resolvedPath, null);
125-
// Do not visit stylesheets which have been referenced from a component.
126-
if (!this._analyzedFiles.has(resolvedPath) && stylesheet) {
127-
migrations.forEach(r => r.visitStylesheet(stylesheet));
128-
this._analyzedFiles.add(resolvedPath);
133+
if (limitToDirectory == null || resolvedPath.startsWith(limitToDirectory)) {
134+
const stylesheet = resourceCollector.resolveExternalStylesheet(resolvedPath, null);
135+
// Do not visit stylesheets which have been referenced from a component.
136+
if (!this._analyzedFiles.has(resolvedPath) && stylesheet) {
137+
migrations.forEach(r => r.visitStylesheet(stylesheet));
138+
this._analyzedFiles.add(resolvedPath);
139+
}
129140
}
130141
});
131142
}

src/material/schematics/ng-generate/mdc-migration/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function runMigrations(
6868
migrators: ComponentMigrator[],
6969
analyzedFiles: Set<WorkspacePath>,
7070
additionalStylesheetPaths: string[],
71+
limitToDirectory?: string,
7172
): boolean {
7273
const program = UpdateProject.createProgramFromTsconfig(tsconfigPath, fileSystem);
7374
const project = new UpdateProject(context, program, fileSystem, analyzedFiles, context.logger);
@@ -76,6 +77,7 @@ function runMigrations(
7677
null,
7778
migrators,
7879
additionalStylesheetPaths,
80+
limitToDirectory,
7981
).hasFailures;
8082
}
8183

@@ -93,11 +95,11 @@ export default function (options: Schema): Rule {
9395
const analyzedFiles = new Set<WorkspacePath>();
9496
const componentsToMigrate = getComponentsToMigrate(options.components);
9597
const migrators = MIGRATORS.filter(m => componentsToMigrate.has(m.component));
96-
let additionalStylesheetPaths = options.directory
97-
? findStylesheetFiles(tree, options.directory)
98-
: [];
9998
let success = true;
10099

100+
if (options.directory) {
101+
logger.info(`Limiting migration to: ${options.directory}`);
102+
}
101103
logger.info(`Migrating components:\n${[...componentsToMigrate].join('\n')}`);
102104

103105
for (const projectName of projectNames) {
@@ -114,9 +116,7 @@ export default function (options: Schema): Rule {
114116
continue;
115117
}
116118

117-
if (!options.directory) {
118-
additionalStylesheetPaths = findStylesheetFiles(tree, project.root);
119-
}
119+
const additionalStylesheetPaths = findStylesheetFiles(tree, project.root);
120120

121121
logger.info(`Migrating project: ${projectName}`);
122122

@@ -128,6 +128,7 @@ export default function (options: Schema): Rule {
128128
migrators,
129129
analyzedFiles,
130130
additionalStylesheetPaths,
131+
options.directory || undefined,
131132
);
132133
}
133134
}

src/material/schematics/ng-generate/mdc-migration/schema.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface Schema {
1313
* Source files determined outside of this directory will be ignored,
1414
* allowing for an incremental migration.
1515
*
16-
* If not set, the directory is determined based on the specified tsconfig.
16+
* If not set, the directory is determined based on the workspace.
1717
*/
1818
directory?: string;
1919

src/material/schematics/ng-generate/mdc-migration/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format": "path",
1010
"description": "Workspace-relative path to a directory which will be migrated.",
1111
"alias": "d",
12-
"default": ""
12+
"x-prompt": "Limit the migration to a specific directory? (Enter directory or leave blank for all directories)"
1313
},
1414
"components": {
1515
"type": "array",

0 commit comments

Comments
 (0)