refactor(compiler): module collector is reusable (#12095)

This commit is contained in:
Chuck Jazdzewski 2016-10-06 15:10:44 -07:00 committed by Tobias Bosch
parent 8c975ed156
commit c9b765f5c0
3 changed files with 39 additions and 18 deletions

View File

@ -21,7 +21,7 @@ import {CompileMetadataResolver, DirectiveNormalizer, DomElementSchemaRegistry,
import {Console} from './private_import_core'; import {Console} from './private_import_core';
import {ReflectorHost, ReflectorHostContext} from './reflector_host'; import {ReflectorHost, ReflectorHostContext} from './reflector_host';
import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities'; import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities';
import {StaticReflector, StaticSymbol} from './static_reflector'; import {StaticReflector, StaticReflectorHost, StaticSymbol} from './static_reflector';
const nodeFs = require('fs'); const nodeFs = require('fs');
@ -36,11 +36,26 @@ const PREAMBLE = `/**
`; `;
export class CodeGenerator { export class CodeGeneratorModuleCollector {
constructor( constructor(
private options: AngularCompilerOptions, private program: ts.Program, private staticReflector: StaticReflector, private reflectorHost: StaticReflectorHost,
public host: ts.CompilerHost, private staticReflector: StaticReflector, private program: ts.Program, private options: AngularCompilerOptions) {}
private compiler: compiler.OfflineCompiler, private reflectorHost: ReflectorHost) {}
getModuleSymbols(program: ts.Program): {fileMetas: FileMetadata[], ngModules: StaticSymbol[]} {
// Compare with false since the default should be true
const skipFileNames = (this.options.generateCodeForLibraries === false) ?
GENERATED_OR_DTS_FILES :
GENERATED_FILES;
let filePaths = this.program.getSourceFiles()
.filter(sf => !skipFileNames.test(sf.fileName))
.map(sf => this.reflectorHost.getCanonicalFileName(sf.fileName));
const fileMetas = filePaths.map((filePath) => this.readFileMetadata(filePath));
const ngModules = fileMetas.reduce((ngModules, fileMeta) => {
ngModules.push(...fileMeta.ngModules);
return ngModules;
}, <StaticSymbol[]>[]);
return {fileMetas, ngModules};
}
private readFileMetadata(absSourcePath: string): FileMetadata { private readFileMetadata(absSourcePath: string): FileMetadata {
const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath); const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath);
@ -71,6 +86,18 @@ export class CodeGenerator {
} }
return result; return result;
} }
}
export class CodeGenerator {
private moduleCollector: CodeGeneratorModuleCollector;
constructor(
private options: AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: StaticReflector,
private compiler: compiler.OfflineCompiler, private reflectorHost: StaticReflectorHost) {
this.moduleCollector =
new CodeGeneratorModuleCollector(staticReflector, reflectorHost, program, options);
}
// Write codegen in a directory structure matching the sources. // Write codegen in a directory structure matching the sources.
private calculateEmitPath(filePath: string): string { private calculateEmitPath(filePath: string): string {
@ -95,18 +122,7 @@ export class CodeGenerator {
} }
codegen(): Promise<any> { codegen(): Promise<any> {
// Compare with false since the default should be true const {fileMetas, ngModules} = this.moduleCollector.getModuleSymbols(this.program);
const skipFileNames = (this.options.generateCodeForLibraries === false) ?
GENERATED_OR_DTS_FILES :
GENERATED_FILES;
let filePaths = this.program.getSourceFiles()
.filter(sf => !skipFileNames.test(sf.fileName))
.map(sf => this.reflectorHost.getCanonicalFileName(sf.fileName));
const fileMetas = filePaths.map((filePath) => this.readFileMetadata(filePath));
const ngModules = fileMetas.reduce((ngModules, fileMeta) => {
ngModules.push(...fileMeta.ngModules);
return ngModules;
}, <StaticSymbol[]>[]);
const analyzedNgModules = this.compiler.analyzeModules(ngModules); const analyzedNgModules = this.compiler.analyzeModules(ngModules);
return Promise.all(fileMetas.map( return Promise.all(fileMetas.map(
(fileMeta) => (fileMeta) =>
@ -185,7 +201,7 @@ export class CodeGenerator {
} }
} }
interface FileMetadata { export interface FileMetadata {
fileUrl: string; fileUrl: string;
components: StaticSymbol[]; components: StaticSymbol[];
ngModules: StaticSymbol[]; ngModules: StaticSymbol[];

View File

@ -44,6 +44,8 @@ export interface StaticReflectorHost {
animationMetadata: string, animationMetadata: string,
provider: string provider: string
}; };
getCanonicalFileName(fileName: string): string;
} }
/** /**

View File

@ -450,6 +450,9 @@ class MockReflectorHost implements StaticReflectorHost {
provider: 'angular2/src/core/di/provider' provider: 'angular2/src/core/di/provider'
}; };
} }
getCanonicalFileName(fileName: string): string { return fileName; }
getStaticSymbol(declarationFile: string, name: string, members?: string[]): StaticSymbol { getStaticSymbol(declarationFile: string, name: string, members?: string[]): StaticSymbol {
var cacheKey = `${declarationFile}:${name}${members?'.'+members.join('.'):''}`; var cacheKey = `${declarationFile}:${name}${members?'.'+members.join('.'):''}`;
var result = this.staticTypeCache.get(cacheKey); var result = this.staticTypeCache.get(cacheKey);