2019-02-27 14:30:38 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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.io/license
|
|
|
|
*/
|
|
|
|
|
2019-05-08 05:02:37 -04:00
|
|
|
import {logging} from '@angular-devkit/core';
|
2019-04-12 11:55:51 -04:00
|
|
|
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
|
2019-03-13 11:29:25 -04:00
|
|
|
import {dirname, relative} from 'path';
|
2019-04-12 18:14:33 -04:00
|
|
|
import {from} from 'rxjs';
|
2019-03-13 11:29:25 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2019-04-08 10:16:56 -04:00
|
|
|
import {NgComponentTemplateVisitor} from '../../utils/ng_component_template';
|
2019-02-27 14:30:38 -05:00
|
|
|
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
|
2019-03-28 06:24:02 -04:00
|
|
|
import {parseTsconfigFile} from '../../utils/typescript/parse_tsconfig';
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
import {NgQueryResolveVisitor} from './angular/ng_query_visitor';
|
2019-04-12 11:55:51 -04:00
|
|
|
import {QueryTemplateStrategy} from './strategies/template_strategy/template_strategy';
|
2019-04-22 15:11:29 -04:00
|
|
|
import {QueryTestStrategy} from './strategies/test_strategy/test_strategy';
|
2019-04-12 11:55:51 -04:00
|
|
|
import {TimingStrategy} from './strategies/timing-strategy';
|
2019-04-08 13:37:31 -04:00
|
|
|
import {QueryUsageStrategy} from './strategies/usage_strategy/usage_strategy';
|
2019-05-03 09:02:08 -04:00
|
|
|
import {SELECTED_STRATEGY, promptForMigrationStrategy} from './strategy_prompt';
|
2019-03-13 11:29:25 -04:00
|
|
|
import {getTransformedQueryCallExpr} from './transform';
|
|
|
|
|
2019-05-03 09:02:08 -04:00
|
|
|
interface AnalyzedProject {
|
|
|
|
program: ts.Program;
|
|
|
|
host: ts.CompilerHost;
|
|
|
|
queryVisitor: NgQueryResolveVisitor;
|
|
|
|
sourceFiles: ts.SourceFile[];
|
|
|
|
basePath: string;
|
|
|
|
typeChecker: ts.TypeChecker;
|
|
|
|
tsconfigPath: string;
|
2019-04-22 15:11:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:30:38 -05:00
|
|
|
/** Entry point for the V8 static-query migration. */
|
|
|
|
export default function(): Rule {
|
2019-04-12 11:55:51 -04:00
|
|
|
return (tree: Tree, context: SchematicContext) => {
|
2019-04-12 18:14:33 -04:00
|
|
|
// We need to cast the returned "Observable" to "any" as there is a
|
|
|
|
// RxJS version mismatch that breaks the TS compilation.
|
|
|
|
return from(runMigration(tree, context).then(() => tree)) as any;
|
2019-02-27 14:30:38 -05:00
|
|
|
};
|
|
|
|
}
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-04-12 18:14:33 -04:00
|
|
|
/** Runs the V8 migration static-query migration for all determined TypeScript projects. */
|
|
|
|
async function runMigration(tree: Tree, context: SchematicContext) {
|
2019-04-22 15:11:29 -04:00
|
|
|
const {buildPaths, testPaths} = getProjectTsConfigPaths(tree);
|
2019-04-12 18:14:33 -04:00
|
|
|
const basePath = process.cwd();
|
|
|
|
const logger = context.logger;
|
|
|
|
|
|
|
|
logger.info('------ Static Query migration ------');
|
|
|
|
logger.info('In preparation for Ivy, developers can now explicitly specify the');
|
|
|
|
logger.info('timing of their queries. Read more about this here:');
|
|
|
|
logger.info('https://github.com/angular/angular/pull/28810');
|
|
|
|
logger.info('');
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
if (!buildPaths.length && !testPaths.length) {
|
2019-04-12 18:14:33 -04:00
|
|
|
throw new SchematicsException(
|
|
|
|
'Could not find any tsconfig file. Cannot migrate queries ' +
|
|
|
|
'to explicit timing.');
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:35:27 -04:00
|
|
|
const analyzedFiles = new Set<string>();
|
2019-05-03 09:02:08 -04:00
|
|
|
const buildProjects = new Set<AnalyzedProject>();
|
2019-04-12 18:14:33 -04:00
|
|
|
const failures = [];
|
2019-04-22 15:11:29 -04:00
|
|
|
|
|
|
|
for (const tsconfigPath of buildPaths) {
|
2019-05-14 14:35:27 -04:00
|
|
|
const project = analyzeProject(tree, tsconfigPath, basePath, analyzedFiles);
|
2019-05-03 09:02:08 -04:00
|
|
|
if (project) {
|
|
|
|
buildProjects.add(project);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case there are projects which contain queries that need to be migrated,
|
|
|
|
// we want to prompt for the migration strategy and run the migration.
|
|
|
|
if (buildProjects.size) {
|
|
|
|
const strategy = await promptForMigrationStrategy(logger);
|
|
|
|
for (let project of Array.from(buildProjects.values())) {
|
2019-05-08 05:02:37 -04:00
|
|
|
failures.push(...await runStaticQueryMigration(tree, project, strategy, logger));
|
2019-05-03 09:02:08 -04:00
|
|
|
}
|
2019-04-22 15:11:29 -04:00
|
|
|
}
|
2019-05-03 09:02:08 -04:00
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
// For the "test" tsconfig projects we always want to use the test strategy as
|
|
|
|
// we can't detect the proper timing within spec files.
|
|
|
|
for (const tsconfigPath of testPaths) {
|
2019-05-14 14:35:27 -04:00
|
|
|
const project = await analyzeProject(tree, tsconfigPath, basePath, analyzedFiles);
|
2019-05-03 09:02:08 -04:00
|
|
|
if (project) {
|
2019-05-08 05:02:37 -04:00
|
|
|
failures.push(
|
|
|
|
...await runStaticQueryMigration(tree, project, SELECTED_STRATEGY.TESTS, logger));
|
2019-05-03 09:02:08 -04:00
|
|
|
}
|
2019-04-12 18:14:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failures.length) {
|
2019-05-03 09:02:08 -04:00
|
|
|
logger.info('Some queries could not be migrated automatically. Please go');
|
|
|
|
logger.info('through those manually and apply the appropriate timing:');
|
2019-04-12 18:14:33 -04:00
|
|
|
failures.forEach(failure => logger.warn(`⮑ ${failure}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('------------------------------------------------');
|
|
|
|
}
|
|
|
|
|
2019-03-13 11:29:25 -04:00
|
|
|
/**
|
2019-05-03 09:02:08 -04:00
|
|
|
* Analyzes the given TypeScript project by looking for queries that need to be
|
|
|
|
* migrated. In case there are no queries that can be migrated, null is returned.
|
2019-03-13 11:29:25 -04:00
|
|
|
*/
|
2019-05-14 14:35:27 -04:00
|
|
|
function analyzeProject(
|
|
|
|
tree: Tree, tsconfigPath: string, basePath: string, analyzedFiles: Set<string>):
|
2019-05-03 09:02:08 -04:00
|
|
|
AnalyzedProject|null {
|
|
|
|
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
|
|
|
|
const host = ts.createCompilerHost(parsed.options, true);
|
|
|
|
|
|
|
|
// We need to overwrite the host "readFile" method, as we want the TypeScript
|
|
|
|
// program to be based on the file contents in the virtual file tree. Otherwise
|
|
|
|
// if we run the migration for multiple tsconfig files which have intersecting
|
|
|
|
// source files, it can end up updating query definitions multiple times.
|
|
|
|
host.readFile = fileName => {
|
|
|
|
const buffer = tree.read(relative(basePath, fileName));
|
|
|
|
return buffer ? buffer.toString() : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
|
|
|
|
const typeChecker = program.getTypeChecker();
|
2019-05-05 06:02:35 -04:00
|
|
|
const sourceFiles = program.getSourceFiles().filter(
|
|
|
|
f => !f.isDeclarationFile && !program.isSourceFileFromExternalLibrary(f));
|
2019-05-03 09:02:08 -04:00
|
|
|
const queryVisitor = new NgQueryResolveVisitor(typeChecker);
|
|
|
|
|
|
|
|
// Analyze all project source-files and collect all queries that
|
|
|
|
// need to be migrated.
|
2019-05-14 14:35:27 -04:00
|
|
|
sourceFiles.forEach(sourceFile => {
|
|
|
|
const relativePath = relative(basePath, sourceFile.fileName);
|
|
|
|
|
|
|
|
// Only look for queries within the current source files if the
|
|
|
|
// file has not been analyzed before.
|
|
|
|
if (!analyzedFiles.has(relativePath)) {
|
|
|
|
analyzedFiles.add(relativePath);
|
|
|
|
queryVisitor.visitNode(sourceFile);
|
|
|
|
}
|
|
|
|
});
|
2019-05-03 09:02:08 -04:00
|
|
|
|
|
|
|
if (queryVisitor.resolvedQueries.size === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-05-03 09:02:08 -04:00
|
|
|
return {program, host, tsconfigPath, typeChecker, basePath, queryVisitor, sourceFiles};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the static query migration for the given project. The schematic analyzes all
|
|
|
|
* queries within the project and sets up the query timing based on the current usage
|
|
|
|
* of the query property. e.g. a view query that is not used in any lifecycle hook does
|
|
|
|
* not need to be static and can be set up with "static: false".
|
|
|
|
*/
|
|
|
|
async function runStaticQueryMigration(
|
2019-05-08 05:02:37 -04:00
|
|
|
tree: Tree, project: AnalyzedProject, selectedStrategy: SELECTED_STRATEGY,
|
|
|
|
logger: logging.LoggerApi) {
|
2019-05-03 09:02:08 -04:00
|
|
|
const {sourceFiles, typeChecker, host, queryVisitor, tsconfigPath, basePath} = project;
|
2019-03-13 11:29:25 -04:00
|
|
|
const printer = ts.createPrinter();
|
2019-04-22 15:11:29 -04:00
|
|
|
const failureMessages: string[] = [];
|
2019-05-03 09:02:08 -04:00
|
|
|
const templateVisitor = new NgComponentTemplateVisitor(typeChecker);
|
2019-04-12 11:55:51 -04:00
|
|
|
|
|
|
|
// If the "usage" strategy is selected, we also need to add the query visitor
|
|
|
|
// to the analysis visitors so that query usage in templates can be also checked.
|
2019-04-22 15:11:29 -04:00
|
|
|
if (selectedStrategy === SELECTED_STRATEGY.USAGE) {
|
2019-05-03 09:02:08 -04:00
|
|
|
sourceFiles.forEach(s => templateVisitor.visitNode(s));
|
2019-04-12 11:55:51 -04:00
|
|
|
}
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
const {resolvedQueries, classMetadata} = queryVisitor;
|
2019-04-12 11:55:51 -04:00
|
|
|
const {resolvedTemplates} = templateVisitor;
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
if (selectedStrategy === SELECTED_STRATEGY.USAGE) {
|
2019-04-12 11:55:51 -04:00
|
|
|
// Add all resolved templates to the class metadata if the usage strategy is used. This
|
|
|
|
// is necessary in order to be able to check component templates for static query usage.
|
|
|
|
resolvedTemplates.forEach(template => {
|
|
|
|
if (classMetadata.has(template.container)) {
|
|
|
|
classMetadata.get(template.container) !.template = template;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
let strategy: TimingStrategy;
|
|
|
|
if (selectedStrategy === SELECTED_STRATEGY.USAGE) {
|
|
|
|
strategy = new QueryUsageStrategy(classMetadata, typeChecker);
|
|
|
|
} else if (selectedStrategy === SELECTED_STRATEGY.TESTS) {
|
|
|
|
strategy = new QueryTestStrategy();
|
|
|
|
} else {
|
|
|
|
strategy = new QueryTemplateStrategy(tsconfigPath, classMetadata, host);
|
|
|
|
}
|
2019-04-08 10:16:56 -04:00
|
|
|
|
2019-05-08 05:02:37 -04:00
|
|
|
try {
|
|
|
|
strategy.setup();
|
|
|
|
} catch (e) {
|
|
|
|
// In case the strategy could not be set up properly, we just exit the
|
|
|
|
// migration. We don't want to throw an exception as this could mean
|
|
|
|
// that other migrations are interrupted.
|
|
|
|
logger.warn(
|
|
|
|
`Could not setup migration strategy for "${project.tsconfigPath}". The ` +
|
|
|
|
`following error has been reported:`);
|
|
|
|
if (selectedStrategy === SELECTED_STRATEGY.TEMPLATE) {
|
|
|
|
logger.warn(
|
|
|
|
`The template migration strategy uses the Angular compiler ` +
|
|
|
|
`internally and therefore projects that no longer build successfully after ` +
|
|
|
|
`the update cannot use the template migration strategy. Please ensure ` +
|
|
|
|
`there are no AOT compilation errors.`);
|
|
|
|
}
|
2019-05-14 13:59:26 -04:00
|
|
|
logger.error(e.toString());
|
2019-05-08 05:02:37 -04:00
|
|
|
logger.info(
|
|
|
|
'Migration can be rerun with: "ng update @angular/core --from 7 --to 8 --migrate-only"');
|
2019-04-12 18:14:33 -04:00
|
|
|
return [];
|
2019-04-12 11:55:51 -04:00
|
|
|
}
|
2019-04-08 13:37:31 -04:00
|
|
|
|
2019-03-13 11:29:25 -04:00
|
|
|
// Walk through all source files that contain resolved queries and update
|
|
|
|
// the source files if needed. Note that we need to update multiple queries
|
|
|
|
// within a source file within the same recorder in order to not throw off
|
|
|
|
// the TypeScript node offsets.
|
|
|
|
resolvedQueries.forEach((queries, sourceFile) => {
|
2019-04-12 11:55:51 -04:00
|
|
|
const relativePath = relative(basePath, sourceFile.fileName);
|
|
|
|
const update = tree.beginUpdate(relativePath);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-04-12 11:55:51 -04:00
|
|
|
// Compute the query timing for all resolved queries and update the
|
|
|
|
// query definitions to explicitly set the determined query timing.
|
2019-03-13 11:29:25 -04:00
|
|
|
queries.forEach(q => {
|
|
|
|
const queryExpr = q.decorator.node.expression;
|
2019-04-12 11:55:51 -04:00
|
|
|
const {timing, message} = strategy.detectTiming(q);
|
2019-04-28 10:40:59 -04:00
|
|
|
const result = getTransformedQueryCallExpr(q, timing, !!message);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-04-28 10:40:59 -04:00
|
|
|
if (!result) {
|
2019-03-13 11:29:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-28 10:40:59 -04:00
|
|
|
const newText = printer.printNode(ts.EmitHint.Unspecified, result.node, sourceFile);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
// Replace the existing query decorator call expression with the updated
|
|
|
|
// call expression node.
|
|
|
|
update.remove(queryExpr.getStart(), queryExpr.getWidth());
|
|
|
|
update.insertRight(queryExpr.getStart(), newText);
|
2019-04-12 11:55:51 -04:00
|
|
|
|
2019-04-28 10:40:59 -04:00
|
|
|
if (result.failureMessage || message) {
|
2019-04-12 18:14:33 -04:00
|
|
|
const {line, character} =
|
|
|
|
ts.getLineAndCharacterOfPosition(sourceFile, q.decorator.node.getStart());
|
2019-04-28 10:40:59 -04:00
|
|
|
failureMessages.push(
|
|
|
|
`${relativePath}@${line + 1}:${character + 1}: ${result.failureMessage || message}`);
|
2019-04-12 18:14:33 -04:00
|
|
|
}
|
2019-03-13 11:29:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
tree.commitUpdate(update);
|
|
|
|
});
|
2019-04-12 11:55:51 -04:00
|
|
|
|
2019-04-12 18:14:33 -04:00
|
|
|
return failureMessages;
|
2019-03-13 11:29:25 -04:00
|
|
|
}
|