devkit-migration-rule.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  10. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  11. return new (P || (P = Promise))(function (resolve, reject) {
  12. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  13. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  14. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  15. step((generator = generator.apply(thisArg, _arguments || [])).next());
  16. });
  17. };
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.cdkMigrations = void 0;
  20. exports.createMigrationSchematicRule = createMigrationSchematicRule;
  21. exports.isDevkitMigration = isDevkitMigration;
  22. const tasks_1 = require("@angular-devkit/schematics/tasks");
  23. const update_tool_1 = require("../update-tool");
  24. const project_tsconfig_paths_1 = require("../utils/project-tsconfig-paths");
  25. const devkit_file_system_1 = require("./devkit-file-system");
  26. const devkit_migration_1 = require("./devkit-migration");
  27. const find_stylesheets_1 = require("./find-stylesheets");
  28. const attribute_selectors_1 = require("./migrations/attribute-selectors");
  29. const class_inheritance_1 = require("./migrations/class-inheritance");
  30. const class_names_1 = require("./migrations/class-names");
  31. const constructor_signature_1 = require("./migrations/constructor-signature");
  32. const css_selectors_1 = require("./migrations/css-selectors");
  33. const css_tokens_1 = require("./migrations/css-tokens");
  34. const element_selectors_1 = require("./migrations/element-selectors");
  35. const input_names_1 = require("./migrations/input-names");
  36. const method_call_arguments_1 = require("./migrations/method-call-arguments");
  37. const misc_template_1 = require("./migrations/misc-template");
  38. const output_names_1 = require("./migrations/output-names");
  39. const property_names_1 = require("./migrations/property-names");
  40. const symbol_removal_1 = require("./migrations/symbol-removal");
  41. /** List of migrations which run for the CDK update. */
  42. exports.cdkMigrations = [
  43. attribute_selectors_1.AttributeSelectorsMigration,
  44. class_inheritance_1.ClassInheritanceMigration,
  45. class_names_1.ClassNamesMigration,
  46. constructor_signature_1.ConstructorSignatureMigration,
  47. css_selectors_1.CssSelectorsMigration,
  48. css_tokens_1.CssTokensMigration,
  49. element_selectors_1.ElementSelectorsMigration,
  50. input_names_1.InputNamesMigration,
  51. method_call_arguments_1.MethodCallArgumentsMigration,
  52. misc_template_1.MiscTemplateMigration,
  53. output_names_1.OutputNamesMigration,
  54. property_names_1.PropertyNamesMigration,
  55. symbol_removal_1.SymbolRemovalMigration,
  56. ];
  57. /**
  58. * Creates a Angular schematic rule that runs the upgrade for the
  59. * specified target version.
  60. */
  61. function createMigrationSchematicRule(targetVersion, extraMigrations, upgradeData, onMigrationCompleteFn) {
  62. return (tree, context) => __awaiter(this, void 0, void 0, function* () {
  63. const logger = context.logger;
  64. const workspace = yield (0, project_tsconfig_paths_1.getWorkspaceConfigGracefully)(tree);
  65. if (workspace === null) {
  66. logger.error('Could not find workspace configuration file.');
  67. return;
  68. }
  69. // Keep track of all project source files which have been checked/migrated. This is
  70. // necessary because multiple TypeScript projects can contain the same source file and
  71. // we don't want to check these again, as this would result in duplicated failure messages.
  72. const analyzedFiles = new Set();
  73. const fileSystem = new devkit_file_system_1.DevkitFileSystem(tree);
  74. const projectNames = workspace.projects.keys();
  75. const migrations = [...exports.cdkMigrations, ...extraMigrations];
  76. let hasFailures = false;
  77. for (const projectName of projectNames) {
  78. const project = workspace.projects.get(projectName);
  79. const buildTsconfigPath = (0, project_tsconfig_paths_1.getTargetTsconfigPath)(project, 'build');
  80. const testTsconfigPath = (0, project_tsconfig_paths_1.getTargetTsconfigPath)(project, 'test');
  81. if (!buildTsconfigPath && !testTsconfigPath) {
  82. logger.warn(`Skipping migration for project ${projectName}. Unable to determine 'tsconfig.json' file in workspace config.`);
  83. continue;
  84. }
  85. // In some applications, developers will have global stylesheets which are not
  86. // specified in any Angular component. Therefore we glob up all CSS and SCSS files
  87. // in the project and migrate them if needed.
  88. // TODO: rework this to collect global stylesheets from the workspace config.
  89. // TODO: https://github.com/angular/components/issues/24032.
  90. const additionalStylesheetPaths = (0, find_stylesheets_1.findStylesheetFiles)(tree, project.root);
  91. if (buildTsconfigPath !== null) {
  92. runMigrations(project, projectName, buildTsconfigPath, additionalStylesheetPaths, false);
  93. }
  94. if (testTsconfigPath !== null) {
  95. runMigrations(project, projectName, testTsconfigPath, additionalStylesheetPaths, true);
  96. }
  97. }
  98. let runPackageManager = false;
  99. // Run the global post migration static members for all
  100. // registered devkit migrations.
  101. migrations.forEach(m => {
  102. const actionResult = isDevkitMigration(m) && m.globalPostMigration !== undefined
  103. ? m.globalPostMigration(tree, targetVersion, context)
  104. : null;
  105. if (actionResult) {
  106. runPackageManager = runPackageManager || actionResult.runPackageManager;
  107. }
  108. });
  109. // If a migration requested the package manager to run, we run it as an
  110. // asynchronous post migration task. We cannot run it synchronously,
  111. // as file changes from the current migration task are not applied to
  112. // the file system yet.
  113. if (runPackageManager) {
  114. context.addTask(new tasks_1.NodePackageInstallTask({ quiet: false }));
  115. }
  116. if (onMigrationCompleteFn) {
  117. onMigrationCompleteFn(context, targetVersion, hasFailures);
  118. }
  119. /** Runs the migrations for the specified workspace project. */
  120. function runMigrations(project, projectName, tsconfigPath, additionalStylesheetPaths, isTestTarget) {
  121. const program = update_tool_1.UpdateProject.createProgramFromTsconfig(tsconfigPath, fileSystem);
  122. const updateContext = {
  123. isTestTarget,
  124. projectName,
  125. project,
  126. tree,
  127. };
  128. const updateProject = new update_tool_1.UpdateProject(updateContext, program, fileSystem, analyzedFiles, context.logger);
  129. const result = updateProject.migrate(migrations, targetVersion, upgradeData, additionalStylesheetPaths);
  130. // Commit all recorded edits in the update recorder. We apply the edits after all
  131. // migrations ran because otherwise offsets in the TypeScript program would be
  132. // shifted and individual migrations could no longer update the same source file.
  133. fileSystem.commitEdits();
  134. hasFailures = hasFailures || result.hasFailures;
  135. }
  136. });
  137. }
  138. /** Whether the given migration type refers to a devkit migration */
  139. function isDevkitMigration(value) {
  140. return devkit_migration_1.DevkitMigration.isPrototypeOf(value);
  141. }
  142. //# sourceMappingURL=devkit-migration-rule.js.map