|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; |
| 10 | +import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks'; |
| 11 | +import {WorkspaceProject} from '@schematics/angular/utility/workspace-models'; |
| 12 | +import {sync as globSync} from 'glob'; |
| 13 | +import {join} from 'path'; |
| 14 | + |
| 15 | +import {UpdateProject} from '../update-tool'; |
| 16 | +import {MigrationCtor} from '../update-tool/migration'; |
| 17 | +import {TargetVersion} from '../update-tool/target-version'; |
| 18 | +import {getTargetTsconfigPath, getWorkspaceConfigGracefully} from '../utils/project-tsconfig-paths'; |
| 19 | + |
| 20 | +import {DevkitFileSystem} from './devkit-file-system'; |
| 21 | +import {DevkitContext, DevkitMigration, DevkitMigrationCtor} from './devkit-migration'; |
| 22 | +import {AttributeSelectorsMigration} from './migrations/attribute-selectors'; |
| 23 | +import {ClassInheritanceMigration} from './migrations/class-inheritance'; |
| 24 | +import {ClassNamesMigration} from './migrations/class-names'; |
| 25 | +import {ConstructorSignatureMigration} from './migrations/constructor-signature'; |
| 26 | +import {CssSelectorsMigration} from './migrations/css-selectors'; |
| 27 | +import {ElementSelectorsMigration} from './migrations/element-selectors'; |
| 28 | +import {InputNamesMigration} from './migrations/input-names'; |
| 29 | +import {MethodCallArgumentsMigration} from './migrations/method-call-arguments'; |
| 30 | +import {MiscTemplateMigration} from './migrations/misc-template'; |
| 31 | +import {OutputNamesMigration} from './migrations/output-names'; |
| 32 | +import {PropertyNamesMigration} from './migrations/property-names'; |
| 33 | +import {UpgradeData} from './upgrade-data'; |
| 34 | + |
| 35 | + |
| 36 | +/** List of migrations which run for the CDK update. */ |
| 37 | +export const cdkMigrations: MigrationCtor<UpgradeData>[] = [ |
| 38 | + AttributeSelectorsMigration, |
| 39 | + ClassInheritanceMigration, |
| 40 | + ClassNamesMigration, |
| 41 | + ConstructorSignatureMigration, |
| 42 | + CssSelectorsMigration, |
| 43 | + ElementSelectorsMigration, |
| 44 | + InputNamesMigration, |
| 45 | + MethodCallArgumentsMigration, |
| 46 | + MiscTemplateMigration, |
| 47 | + OutputNamesMigration, |
| 48 | + PropertyNamesMigration, |
| 49 | +]; |
| 50 | + |
| 51 | +export type NullableDevkitMigration = MigrationCtor<UpgradeData|null, DevkitContext>; |
| 52 | + |
| 53 | +type PostMigrationFn = |
| 54 | + (context: SchematicContext, targetVersion: TargetVersion, hasFailure: boolean) => void; |
| 55 | + |
| 56 | +/** |
| 57 | + * Creates a Angular schematic rule that runs the upgrade for the |
| 58 | + * specified target version. |
| 59 | + */ |
| 60 | +export function createMigrationSchematicRule( |
| 61 | + targetVersion: TargetVersion, extraMigrations: NullableDevkitMigration[], |
| 62 | + upgradeData: UpgradeData, onMigrationCompleteFn?: PostMigrationFn): Rule { |
| 63 | + return async (tree: Tree, context: SchematicContext) => { |
| 64 | + const logger = context.logger; |
| 65 | + const workspace = getWorkspaceConfigGracefully(tree); |
| 66 | + |
| 67 | + if (workspace === null) { |
| 68 | + logger.error('Could not find workspace configuration file.'); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Keep track of all project source files which have been checked/migrated. This is |
| 73 | + // necessary because multiple TypeScript projects can contain the same source file and |
| 74 | + // we don't want to check these again, as this would result in duplicated failure messages. |
| 75 | + const analyzedFiles = new Set<string>(); |
| 76 | + // The CLI uses the working directory as the base directory for the virtual file system tree. |
| 77 | + const workspaceFsPath = process.cwd(); |
| 78 | + const fileSystem = new DevkitFileSystem(tree, workspaceFsPath); |
| 79 | + const projectNames = Object.keys(workspace.projects); |
| 80 | + const migrations: NullableDevkitMigration[] = [...cdkMigrations, ...extraMigrations]; |
| 81 | + let hasFailures = false; |
| 82 | + |
| 83 | + for (const projectName of projectNames) { |
| 84 | + const project = workspace.projects[projectName]; |
| 85 | + const buildTsconfigPath = getTargetTsconfigPath(project, 'build'); |
| 86 | + const testTsconfigPath = getTargetTsconfigPath(project, 'test'); |
| 87 | + |
| 88 | + if (!buildTsconfigPath && !testTsconfigPath) { |
| 89 | + logger.warn(`Could not find TypeScript project for project: ${projectName}`); |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (buildTsconfigPath !== null) { |
| 93 | + runMigrations(project, projectName, buildTsconfigPath, false); |
| 94 | + } |
| 95 | + if (testTsconfigPath !== null) { |
| 96 | + runMigrations(project, projectName, testTsconfigPath, true); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + let runPackageManager = false; |
| 101 | + // Run the global post migration static members for all |
| 102 | + // registered devkit migrations. |
| 103 | + migrations.forEach(m => { |
| 104 | + const actionResult = isDevkitMigration(m) && m.globalPostMigration !== undefined ? |
| 105 | + m.globalPostMigration(tree, context) : null; |
| 106 | + if (actionResult) { |
| 107 | + runPackageManager = runPackageManager || actionResult.runPackageManager; |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // If a migration requested the package manager to run, we run it as an |
| 112 | + // asynchronous post migration task. We cannot run it synchronously, |
| 113 | + // as file changes from the current migration task are not applied to |
| 114 | + // the file system yet. |
| 115 | + if (runPackageManager) { |
| 116 | + context.addTask(new NodePackageInstallTask({quiet: false})); |
| 117 | + } |
| 118 | + |
| 119 | + if (onMigrationCompleteFn) { |
| 120 | + onMigrationCompleteFn(context, targetVersion, hasFailures); |
| 121 | + } |
| 122 | + |
| 123 | + /** Runs the migrations for the specified workspace project. */ |
| 124 | + function runMigrations(project: WorkspaceProject, projectName: string, |
| 125 | + tsconfigPath: string, isTestTarget: boolean) { |
| 126 | + const projectRootFsPath = join(workspaceFsPath, project.root); |
| 127 | + const tsconfigFsPath = join(workspaceFsPath, tsconfigPath); |
| 128 | + const program = UpdateProject.createProgramFromTsconfig(tsconfigFsPath, fileSystem); |
| 129 | + const updateContext: DevkitContext = { |
| 130 | + workspaceFsPath, |
| 131 | + isTestTarget, |
| 132 | + projectName, |
| 133 | + project, |
| 134 | + tree, |
| 135 | + }; |
| 136 | + |
| 137 | + const updateProject = new UpdateProject( |
| 138 | + updateContext, |
| 139 | + program, |
| 140 | + fileSystem, |
| 141 | + analyzedFiles, |
| 142 | + context.logger, |
| 143 | + ); |
| 144 | + |
| 145 | + // In some applications, developers will have global stylesheets which are not |
| 146 | + // specified in any Angular component. Therefore we glob up all CSS and SCSS files |
| 147 | + // outside of node_modules and dist. The files will be read by the individual |
| 148 | + // stylesheet rules and checked. |
| 149 | + // TODO: rework this to collect global stylesheets from the workspace config. COMP-280. |
| 150 | + const additionalStylesheets = globSync( |
| 151 | + '!(node_modules|dist)/**/*.+(css|scss)', |
| 152 | + {absolute: true, cwd: projectRootFsPath, nodir: true}); |
| 153 | + |
| 154 | + const result = |
| 155 | + updateProject.migrate(migrations, targetVersion, upgradeData, additionalStylesheets); |
| 156 | + |
| 157 | + // Commit all recorded edits in the update recorder. We apply the edits after all |
| 158 | + // migrations ran because otherwise offsets in the TypeScript program would be |
| 159 | + // shifted and individual migrations could no longer update the same source file. |
| 160 | + fileSystem.commitEdits(); |
| 161 | + |
| 162 | + hasFailures = hasFailures || result.hasFailures; |
| 163 | + } |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +/** Whether the given migration type refers to a devkit migration */ |
| 168 | +export function isDevkitMigration(value: MigrationCtor<any, any>) |
| 169 | + : value is DevkitMigrationCtor<any> { |
| 170 | + return DevkitMigration.isPrototypeOf(value); |
| 171 | +} |
0 commit comments