|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 | + |
| 10 | +/** |
| 11 | + * Extract i18n messages from source code |
| 12 | + */ |
| 13 | +import {ViewEncapsulation} from '@angular/core'; |
| 14 | + |
| 15 | +import {analyzeAndValidateNgModules, extractProgramSymbols, loadNgModuleDirectives} from '../aot/compiler'; |
| 16 | +import {StaticAndDynamicReflectionCapabilities} from '../aot/static_reflection_capabilities'; |
| 17 | +import {StaticReflector, StaticReflectorHost} from '../aot/static_reflector'; |
| 18 | +import {CompileDirectiveMetadata} from '../compile_metadata'; |
| 19 | +import {CompilerConfig} from '../config'; |
| 20 | +import {DirectiveNormalizer} from '../directive_normalizer'; |
| 21 | +import {DirectiveResolver} from '../directive_resolver'; |
| 22 | +import {CompileMetadataResolver} from '../metadata_resolver'; |
| 23 | +import {HtmlParser} from '../ml_parser/html_parser'; |
| 24 | +import {InterpolationConfig} from '../ml_parser/interpolation_config'; |
| 25 | +import {NgModuleResolver} from '../ng_module_resolver'; |
| 26 | +import {ParseError} from '../parse_util'; |
| 27 | +import {PipeResolver} from '../pipe_resolver'; |
| 28 | +import {Console} from '../private_import_core'; |
| 29 | +import {DomElementSchemaRegistry} from '../schema/dom_element_schema_registry'; |
| 30 | +import {createOfflineCompileUrlResolver} from '../url_resolver'; |
| 31 | + |
| 32 | +import {I18NHtmlParser} from './i18n_html_parser'; |
| 33 | +import {MessageBundle} from './message_bundle'; |
| 34 | + |
| 35 | +export interface ExtractorOptions { |
| 36 | + includeFilePattern?: RegExp; |
| 37 | + excludeFilePattern?: RegExp; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * The host of the Extractor disconnects the implementation from TypeScript / other language |
| 42 | + * services and from underlying file systems. |
| 43 | + */ |
| 44 | +export interface ExtractorHost extends StaticReflectorHost { |
| 45 | + /** |
| 46 | + * Loads a resource (e.g. html / css) |
| 47 | + */ |
| 48 | + loadResource(path: string): Promise<string>; |
| 49 | +} |
| 50 | + |
| 51 | +export class Extractor { |
| 52 | + constructor( |
| 53 | + private options: ExtractorOptions, public host: ExtractorHost, |
| 54 | + private staticReflector: StaticReflector, private messageBundle: MessageBundle, |
| 55 | + private metadataResolver: CompileMetadataResolver) {} |
| 56 | + |
| 57 | + extract(rootFiles: string[]): Promise<MessageBundle> { |
| 58 | + const programSymbols = extractProgramSymbols(this.staticReflector, rootFiles, this.options); |
| 59 | + const {ngModuleByPipeOrDirective, files, ngModules} = |
| 60 | + analyzeAndValidateNgModules(programSymbols, this.options, this.metadataResolver); |
| 61 | + return loadNgModuleDirectives(ngModules).then(() => { |
| 62 | + const errors: ParseError[] = []; |
| 63 | + |
| 64 | + files.forEach(file => { |
| 65 | + const compMetas: CompileDirectiveMetadata[] = []; |
| 66 | + file.directives.forEach(directiveType => { |
| 67 | + const dirMeta = this.metadataResolver.getDirectiveMetadata(directiveType); |
| 68 | + if (dirMeta && dirMeta.isComponent) { |
| 69 | + compMetas.push(dirMeta); |
| 70 | + } |
| 71 | + }); |
| 72 | + compMetas.forEach(compMeta => { |
| 73 | + const html = compMeta.template.template; |
| 74 | + const interpolationConfig = |
| 75 | + InterpolationConfig.fromArray(compMeta.template.interpolation); |
| 76 | + errors.push( |
| 77 | + ...this.messageBundle.updateFromTemplate(html, file.srcUrl, interpolationConfig)); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + if (errors.length) { |
| 82 | + throw new Error(errors.map(e => e.toString()).join('\n')); |
| 83 | + } |
| 84 | + |
| 85 | + return this.messageBundle; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + static create(host: ExtractorHost, options: ExtractorOptions): |
| 90 | + {extractor: Extractor, staticReflector: StaticReflector} { |
| 91 | + const htmlParser = new I18NHtmlParser(new HtmlParser()); |
| 92 | + |
| 93 | + const urlResolver = createOfflineCompileUrlResolver(); |
| 94 | + const staticReflector = new StaticReflector(host); |
| 95 | + StaticAndDynamicReflectionCapabilities.install(staticReflector); |
| 96 | + |
| 97 | + const config = new CompilerConfig({ |
| 98 | + genDebugInfo: false, |
| 99 | + defaultEncapsulation: ViewEncapsulation.Emulated, |
| 100 | + logBindingUpdate: false, |
| 101 | + useJit: false |
| 102 | + }); |
| 103 | + |
| 104 | + const normalizer = new DirectiveNormalizer( |
| 105 | + {get: (url: string) => host.loadResource(url)}, urlResolver, htmlParser, config); |
| 106 | + const elementSchemaRegistry = new DomElementSchemaRegistry(); |
| 107 | + const resolver = new CompileMetadataResolver( |
| 108 | + new NgModuleResolver(staticReflector), new DirectiveResolver(staticReflector), |
| 109 | + new PipeResolver(staticReflector), elementSchemaRegistry, normalizer, staticReflector); |
| 110 | + |
| 111 | + // TODO(vicb): implicit tags & attributes |
| 112 | + const messageBundle = new MessageBundle(htmlParser, [], {}); |
| 113 | + |
| 114 | + const extractor = new Extractor(options, host, staticReflector, messageBundle, resolver); |
| 115 | + return {extractor, staticReflector}; |
| 116 | + } |
| 117 | +} |
0 commit comments