Skip to content

Commit

Permalink
fix(material/schematics): respect specified directory in mdc migration (
Browse files Browse the repository at this point in the history
#25810)

(cherry picked from commit 03408fd)
  • Loading branch information
mmalerba committed Oct 14, 2022
1 parent 4926cc5 commit de301a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
27 changes: 19 additions & 8 deletions src/cdk/schematics/update-tool/index.ts
Expand Up @@ -50,13 +50,17 @@ export class UpdateProject<Context> {
* @param data Upgrade data that is passed to all migration rules.
* @param additionalStylesheetPaths Additional stylesheets that should be migrated, if not
* referenced in an Angular component. This is helpful for global stylesheets in a project.
* @param limitToDirectory If specified, changes will be limited to the given directory.
*/
migrate<Data>(
migrationTypes: MigrationCtor<Data, Context>[],
target: TargetVersion | null,
data: Data,
additionalStylesheetPaths?: string[],
limitToDirectory?: string,
): {hasFailures: boolean} {
limitToDirectory &&= this._fileSystem.resolve(limitToDirectory);

// Create instances of the specified migrations.
const migrations = this._createMigrations(migrationTypes, target, data);
// Creates the component resource collector. The collector can visit arbitrary
Expand All @@ -65,9 +69,14 @@ export class UpdateProject<Context> {
const resourceCollector = new ComponentResourceCollector(this._typeChecker, this._fileSystem);
// Collect all of the TypeScript source files we want to migrate. We don't
// migrate type definition files, or source files from external libraries.
const sourceFiles = this._program
.getSourceFiles()
.filter(f => !f.isDeclarationFile && !this._program.isSourceFileFromExternalLibrary(f));
const sourceFiles = this._program.getSourceFiles().filter(f => {
return (
!f.isDeclarationFile &&
(limitToDirectory == null ||
this._fileSystem.resolve(f.fileName).startsWith(limitToDirectory)) &&
!this._program.isSourceFileFromExternalLibrary(f)
);
});

// Helper function that visits a given TypeScript node and collects all referenced
// component resources (i.e. stylesheets or templates). Additionally, the helper
Expand Down Expand Up @@ -121,11 +130,13 @@ export class UpdateProject<Context> {
if (additionalStylesheetPaths) {
additionalStylesheetPaths.forEach(filePath => {
const resolvedPath = this._fileSystem.resolve(filePath);
const stylesheet = resourceCollector.resolveExternalStylesheet(resolvedPath, null);
// Do not visit stylesheets which have been referenced from a component.
if (!this._analyzedFiles.has(resolvedPath) && stylesheet) {
migrations.forEach(r => r.visitStylesheet(stylesheet));
this._analyzedFiles.add(resolvedPath);
if (limitToDirectory == null || resolvedPath.startsWith(limitToDirectory)) {
const stylesheet = resourceCollector.resolveExternalStylesheet(resolvedPath, null);
// Do not visit stylesheets which have been referenced from a component.
if (!this._analyzedFiles.has(resolvedPath) && stylesheet) {
migrations.forEach(r => r.visitStylesheet(stylesheet));
this._analyzedFiles.add(resolvedPath);
}
}
});
}
Expand Down
13 changes: 7 additions & 6 deletions src/material/schematics/ng-generate/mdc-migration/index.ts
Expand Up @@ -68,6 +68,7 @@ function runMigrations(
migrators: ComponentMigrator[],
analyzedFiles: Set<WorkspacePath>,
additionalStylesheetPaths: string[],
limitToDirectory?: string,
): boolean {
const program = UpdateProject.createProgramFromTsconfig(tsconfigPath, fileSystem);
const project = new UpdateProject(context, program, fileSystem, analyzedFiles, context.logger);
Expand All @@ -76,6 +77,7 @@ function runMigrations(
null,
migrators,
additionalStylesheetPaths,
limitToDirectory,
).hasFailures;
}

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

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

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

if (!options.directory) {
additionalStylesheetPaths = findStylesheetFiles(tree, project.root);
}
const additionalStylesheetPaths = findStylesheetFiles(tree, project.root);

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

Expand All @@ -128,6 +128,7 @@ export default function (options: Schema): Rule {
migrators,
analyzedFiles,
additionalStylesheetPaths,
options.directory || undefined,
);
}
}
Expand Down
Expand Up @@ -13,7 +13,7 @@ export interface Schema {
* Source files determined outside of this directory will be ignored,
* allowing for an incremental migration.
*
* If not set, the directory is determined based on the specified tsconfig.
* If not set, the directory is determined based on the workspace.
*/
directory?: string;

Expand Down
Expand Up @@ -9,7 +9,7 @@
"format": "path",
"description": "Workspace-relative path to a directory which will be migrated.",
"alias": "d",
"default": ""
"x-prompt": "Limit the migration to a specific directory? (Enter directory or leave blank for all directories)"
},
"components": {
"type": "array",
Expand Down

0 comments on commit de301a8

Please sign in to comment.