-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Output ng generate #58299
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
Closed
pkozlowski-opensource
wants to merge
3
commits into
angular:main
from
pkozlowski-opensource:output_ng_generate_test
Closed
Output ng generate #58299
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
30 changes: 30 additions & 0 deletions
30
packages/core/schematics/ng-generate/output-migration/BUILD.bazel
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,30 @@ | ||
| load("//tools:defaults.bzl", "ts_library") | ||
|
|
||
| package( | ||
| default_visibility = [ | ||
| "//packages/core/schematics:__pkg__", | ||
| "//packages/core/schematics/migrations/google3:__pkg__", | ||
| "//packages/core/schematics/ng-generate/signals:__pkg__", | ||
| "//packages/core/schematics/test:__pkg__", | ||
| ], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "static_files", | ||
| srcs = ["schema.json"], | ||
| ) | ||
|
|
||
| ts_library( | ||
| name = "output-migration", | ||
| srcs = glob(["**/*.ts"]), | ||
| tsconfig = "//packages/core/schematics:tsconfig.json", | ||
| deps = [ | ||
| "//packages/compiler-cli/src/ngtsc/file_system", | ||
| "//packages/core/schematics/migrations/output-migration:migration", | ||
| "//packages/core/schematics/utils", | ||
| "//packages/core/schematics/utils/tsurge", | ||
| "//packages/core/schematics/utils/tsurge/helpers/angular_devkit", | ||
| "@npm//@angular-devkit/schematics", | ||
| "@npm//@types/node", | ||
| ], | ||
| ) |
108 changes: 108 additions & 0 deletions
108
packages/core/schematics/ng-generate/output-migration/index.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,108 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import {Rule, SchematicsException} from '@angular-devkit/schematics'; | ||
|
|
||
| import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; | ||
| import {DevkitMigrationFilesystem} from '../../utils/tsurge/helpers/angular_devkit/devkit_filesystem'; | ||
| import {groupReplacementsByFile} from '../../utils/tsurge/helpers/group_replacements'; | ||
| import {setFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system'; | ||
| import { | ||
| CompilationUnitData, | ||
| OutputMigration, | ||
| } from '../../migrations/output-migration/output-migration'; | ||
| import {ProjectRootRelativePath, TextUpdate} from '../../utils/tsurge'; | ||
|
|
||
| interface Options { | ||
| path: string; | ||
| analysisDir: string; | ||
| } | ||
|
|
||
| export function migrate(options: Options): Rule { | ||
| return async (tree, context) => { | ||
| const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); | ||
|
|
||
| if (!buildPaths.length && !testPaths.length) { | ||
| throw new SchematicsException( | ||
| 'Could not find any tsconfig file. Cannot run output migration.', | ||
| ); | ||
| } | ||
|
|
||
| const fs = new DevkitMigrationFilesystem(tree); | ||
| setFileSystem(fs); | ||
|
|
||
| const migration = new OutputMigration({ | ||
| shouldMigrate: (_, file) => { | ||
| return ( | ||
| file.rootRelativePath.startsWith(fs.normalize(options.path)) && | ||
| !/(^|\/)node_modules\//.test(file.rootRelativePath) | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const analysisPath = fs.resolve(options.analysisDir); | ||
| const unitResults: CompilationUnitData[] = []; | ||
| const programInfos = [...buildPaths, ...testPaths].map((tsconfigPath) => { | ||
| context.logger.info(`Preparing analysis for: ${tsconfigPath}..`); | ||
|
|
||
| const baseInfo = migration.createProgram(tsconfigPath, fs); | ||
| const info = migration.prepareProgram(baseInfo); | ||
|
|
||
| // Support restricting the analysis to subfolders for larger projects. | ||
| if (analysisPath !== '/') { | ||
| info.sourceFiles = info.sourceFiles.filter((sf) => sf.fileName.startsWith(analysisPath)); | ||
| info.fullProgramSourceFiles = info.fullProgramSourceFiles.filter((sf) => | ||
| sf.fileName.startsWith(analysisPath), | ||
| ); | ||
| } | ||
|
|
||
| return {info, tsconfigPath}; | ||
| }); | ||
|
|
||
| // Analyze phase. Treat all projects as compilation units as | ||
| // this allows us to support references between those. | ||
| for (const {info, tsconfigPath} of programInfos) { | ||
| context.logger.info(`Scanning for outputs: ${tsconfigPath}..`); | ||
| unitResults.push(await migration.analyze(info)); | ||
| } | ||
|
|
||
| context.logger.info(``); | ||
| context.logger.info(`Processing analysis data between targets..`); | ||
| context.logger.info(``); | ||
|
|
||
| const merged = await migration.merge(unitResults); | ||
| const replacementsPerFile: Map<ProjectRootRelativePath, TextUpdate[]> = new Map(); | ||
|
|
||
| for (const {info, tsconfigPath} of programInfos) { | ||
| context.logger.info(`Migrating: ${tsconfigPath}..`); | ||
|
|
||
| const {replacements} = await migration.migrate(merged); | ||
| const changesPerFile = groupReplacementsByFile(replacements); | ||
|
|
||
| for (const [file, changes] of changesPerFile) { | ||
| if (!replacementsPerFile.has(file)) { | ||
| replacementsPerFile.set(file, changes); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| context.logger.info(`Applying changes..`); | ||
| for (const [file, changes] of replacementsPerFile) { | ||
| const recorder = tree.beginUpdate(file); | ||
| for (const c of changes) { | ||
| recorder | ||
| .remove(c.data.position, c.data.end - c.data.position) | ||
| .insertLeft(c.data.position, c.data.toInsert); | ||
| } | ||
| tree.commitUpdate(recorder); | ||
| } | ||
|
|
||
| context.logger.info(''); | ||
| context.logger.info(`Successfully migrated to outputs as functions 🎉`); | ||
| }; | ||
| } |
19 changes: 19 additions & 0 deletions
19
packages/core/schematics/ng-generate/output-migration/schema.json
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,19 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "$id": "AngularOutputMigration", | ||
| "title": "Angular Output migration", | ||
| "type": "object", | ||
| "properties": { | ||
| "path": { | ||
| "type": "string", | ||
| "description": "Path to the directory where all outputs should be migrated.", | ||
| "x-prompt": "Which directory do you want to migrate?", | ||
| "default": "./" | ||
| }, | ||
| "analysisDir": { | ||
| "type": "string", | ||
| "description": "Path to the directory that should be analyzed. References to migrated outputs are migrated based on this folder. Useful for larger projects if the analysis takes too long and the analysis scope can be narrowed.", | ||
| "default": "./" | ||
| } | ||
| } | ||
| } |
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
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.