X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AnnotationVisitor extends SimpleAstVisitor<AnnotationModel> {
}

/// Defines the format in which an [AnnotationModel] is expressed as Dart code
/// in a `.ng_deps.dart` file.
/// when registered with the reflector.
abstract class AnnotationWriterMixin {
StringBuffer get buffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void _populateCombinators(NamespaceDirective node, dynamic model) {
}
}

/// Defines the format in which an [ImportModel] is expressed as Dart code in a
/// `.ng_deps.dart` file.
/// Defines the format in which an [ImportModel] is expressed as Dart code when
/// registered with the reflector.
abstract class ImportWriterMixin {
StringBuffer get buffer;

Expand All @@ -96,8 +96,8 @@ abstract class ImportWriterMixin {
}
}

/// Defines the format in which an [ExportModel] is expressed as Dart code in a
/// `.ng_deps.dart` file.
/// Defines the format in which an [ExportModel] is expressed as Dart code when
/// registered with the reflector.
abstract class ExportWriterMixin {
StringBuffer get buffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import 'reflection_info_code.dart';
import 'parameter_code.dart';
import 'queries_code.dart';

/// Visitor responsible for parsing source Dart files (that is, not
/// `.ng_deps.dart` files) into [NgDepsModel] objects.
/// Visitor responsible for parsing Dart source into [NgDepsModel] objects.
class NgDepsVisitor extends RecursiveAstVisitor<Object> {
final AssetId processedFile;
final _importVisitor = new ImportVisitor();
Expand Down Expand Up @@ -113,7 +112,7 @@ class NgDepsVisitor extends RecursiveAstVisitor<Object> {
}

/// Defines the format in which an [NgDepsModel] is expressed as Dart code
/// in a `.ng_deps.dart` file.
/// when registered with the reflector.
class NgDepsWriter extends Object
with
AnnotationWriterMixin,
Expand All @@ -139,10 +138,10 @@ abstract class NgDepsWriterMixin

void writeNgDepsModel(NgDepsModel model) {
if (model.libraryUri.isNotEmpty) {
buffer.writeln('library ${model.libraryUri}${DEPS_EXTENSION};\n');
buffer.writeln('library ${model.libraryUri}${TEMPLATE_EXTENSION};\n');
}

// We need to import & export the source file.
// We need to import & export (see below) the source file.
writeImportModel(new ImportModel()..uri = model.sourceFile);

// Used to register reflective information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ParameterVisitor extends SimpleAstVisitor<ParameterModel> {
}

/// Defines the format in which a [ParameterModel] is expressed as Dart code
/// in a `.ng_deps.dart` file.
/// when registered with the reflector.
abstract class ParameterWriterMixin {
StringBuffer get buffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class _PropertyMetadataVisitor
}

/// Defines the format in which an [ReflectionInfoModel] is expressed as Dart
/// code in a `.ng_deps.dart` file.
/// code when registered with the reflector.
abstract class ReflectionWriterMixin
implements AnnotationWriterMixin, ParameterWriterMixin {
StringBuffer get buffer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
library angular2.transform.common.code.source_module;

import 'package:analyzer/src/generated/scanner.dart' show Keyword;
import 'package:angular2/src/compiler/source_module.dart';
import 'package:analyzer/src/generated/scanner.dart' show Keyword;
import 'package:angular2/src/transform/common/model/ng_deps_model.pb.dart';
import 'package:angular2/src/transform/common/model/source_module.dart';

import 'uri.dart';
import 'ng_deps_code.dart';

/// Writes the full Dart code for the provided [SourceModule].
String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
if (sourceModule == null) return null;
var buf = new StringBuffer();
final writer = new NgDepsWriter(buf);
var sourceWithImports = sourceModule.getSourceWithImports();
libraryName = _sanitizeLibName(
libraryName != null ? libraryName : sourceModule.moduleUrl);
buf..writeln('library $libraryName;')..writeln();
sourceWithImports.imports.forEach((import) {
// Format for importLine := [uri, prefix]
if (import.length != 2) {
throw new FormatException(
'Unexpected import format! '
'Angular 2 compiler returned imports in an unexpected format. '
'Expected [<import_uri>, <prefix>].',
import.join(', '));
}
buf.writeln(writeImportUri(import[0],
prefix: import[1], fromAbsolute: sourceModule.moduleUrl));

extractImports(sourceWithImports, sourceModule.moduleUrl).forEach((import) {
writer.writeImportModel(import);
});
buf..writeln()..writeln(sourceWithImports.source);

return buf.toString();
}

/// Uses `writer` to write a Dart library representing `model` and
/// `sourceModule`.
void writeTemplateFile(
NgDepsWriterMixin writer, NgDepsModel model, SourceModule sourceModule) {
if (model == null) return null;
var sourceModuleCode = '';
if (sourceModule != null) {
var sourceWithImports = sourceModule.getSourceWithImports();
sourceModuleCode = sourceWithImports.source;

// Since we modify `imports`, make a copy to avoid changing the provided
// value.
var sourceModuleImports =
extractImports(sourceWithImports, sourceModule.moduleUrl);
model = model.clone();
model.imports.addAll(sourceModuleImports);
}
writer.writeNgDepsModel(model);
writer.buffer..writeln()..writeln(sourceModuleCode);
}

final _unsafeCharsPattern = new RegExp(r'[^a-zA-Z0-9_\.]');
String _sanitizeLibName(String moduleUrl) {
var sanitized =
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ message NgDepsModel {
// framework.
repeated string methods = 9;

// Imports for the .ng_deps.dart files associated with the declared `imports`
// and `exports` for this file.
// Imports of the generated files associated with the declared `imports`
// and `exports` of the source file.
repeated ImportModel dep_imports = 10;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
library angular2.transform.common.code.uri;
library angular2.transform.common.model.source_module;

import 'package:angular2/src/transform/common/url_resolver.dart';
import 'package:path/path.dart' as path;

/// Generates an `import` statement for the file specified by `importPath`.
import 'package:angular2/src/compiler/source_module.dart';
import 'package:angular2/src/transform/common/url_resolver.dart';

import 'import_export_model.pb.dart';

/// Generates [ImportModel]s for all imports in `sourceWithImports`.
///
/// Imports in `sourceWithImports` are resolved relative to `moduleUrl`.
List<ImportModel> extractImports(
SourceWithImports sourceWithImports, String moduleUrl) {
if (sourceWithImports == null) return const <ImportModel>[];
return sourceWithImports.imports.map((import) {
// Format for importLine := [uri, prefix]
if (import.length != 2) {
throw new FormatException(
'Internal Angular 2 compiler error. '
'Angular 2 compiler returned imports in an unexpected format. '
'Expected [<import_uri>, <prefix>].',
import.join(', '));
}
return toImportModel(import[0], prefix: import[1], fromAbsolute: moduleUrl);
}).toList();
}

/// Generates an [ImportModel] for the file specified by `importPath`.
///
/// If `fromAbsolute` is specified, `importPath` may be a relative path,
/// otherwise it is expected to be absolute.
String writeImportUri(String importPath, {String prefix, String fromAbsolute}) {
ImportModel toImportModel(String importPath,
{String prefix, String fromAbsolute}) {
var urlResolver = const TransformerUrlResolver();
var codegenImportPath;

Expand All @@ -33,10 +57,12 @@ String writeImportUri(String importPath, {String prefix, String fromAbsolute}) {
}
}

final model = new ImportModel()..uri = codegenImportPath;

if (prefix != null && prefix.isNotEmpty) {
prefix = ' as $prefix';
model.prefix = prefix;
}
return 'import \'$codegenImportPath\'$prefix;';
return model;
}

// For a relative import, the scheme, first (package) and second (lib|test|web)
Expand Down
7 changes: 0 additions & 7 deletions modules_dart/transform/lib/src/transform/common/names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const TRANSFORM_DYNAMIC_MODE = 'transform_dynamic';
const CSS_EXTENSION = '.css';
const SHIMMED_STYLESHEET_EXTENSION = '.css.shim.dart';
const NON_SHIMMED_STYLESHEET_EXTENSION = '.css.dart';
const DEPS_EXTENSION = '.ng_deps.dart';
const META_EXTENSION = '.ng_meta.json';
const REFLECTION_CAPABILITIES_NAME = 'ReflectionCapabilities';
const REFLECTOR_IMPORT = 'package:angular2/src/core/reflection/reflection.dart';
Expand All @@ -24,7 +23,6 @@ const TEMPLATE_EXTENSION = '.template.dart';
/// important. For example, putting '.dart' first in this list will cause
/// incorrect behavior.
const ALL_EXTENSIONS = const [
DEPS_EXTENSION,
META_EXTENSION,
SUMMARY_META_EXTENSION,
TEMPLATE_EXTENSION,
Expand All @@ -38,7 +36,6 @@ const ALL_EXTENSIONS = const [
/// any files named like transformer outputs will be reported as generated.
bool isGenerated(String uri) {
return const [
DEPS_EXTENSION,
META_EXTENSION,
NON_SHIMMED_STYLESHEET_EXTENSION,
SHIMMED_STYLESHEET_EXTENSION,
Expand All @@ -51,10 +48,6 @@ bool isGenerated(String uri) {
String toMetaExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, META_EXTENSION);

/// Returns `uri` with its extension updated to [DEPS_EXTENSION].
String toDepsExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, DEPS_EXTENSION);

/// Returns `uri` with its extension updated to [TEMPLATES_EXTENSION].
String toTemplateExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, TEMPLATE_EXTENSION);
Expand Down
11 changes: 6 additions & 5 deletions modules_dart/transform/lib/src/transform/common/ng_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'logging.dart';
import 'model/ng_deps_model.pb.dart';
import 'url_resolver.dart' show isDartCoreUri;

/// Metadata about directives, directive aliases, and injectable values.
/// Metadata about directives, pipes, directive aliases, and injectable values.
///
/// [NgMeta] is used in three stages of the transformation process:
///
Expand All @@ -20,12 +20,12 @@ import 'url_resolver.dart' show isDartCoreUri;
/// 2. Use the [NgDepsModel] to write Dart code registering all injectable
/// values with the Angular 2 runtime reflection system.
///
/// Further down the compilation process, the template compiler needs to reason
/// Later in the compilation process, the template compiler needs to reason
/// about the namespace of import prefixes, so it will combine multiple [NgMeta]
/// instances together if they were imported into a file with the same prefix.
///
/// Instances of this class are serialized into `.ng_meta.json` files as
/// intermediate assets to make the compilation process easier.
/// Instances of this class are serialized into `.ng_summary.json` and
/// `.ng_meta.json` files as intermediate assets during the compilation process.
class NgMeta {
static const _ALIAS_VALUE = 'alias';
static const _KIND_KEY = 'kind';
Expand Down Expand Up @@ -58,7 +58,8 @@ class NgMeta {
bool get isNgDepsEmpty {
if (ngDeps == null) return true;
// If this file imports only dart: libraries and does not define any
// reflectables of its own, it doesn't need a .ng_deps.dart file.
// reflectables of its own, we don't need to register any information from
// it with the Angular 2 reflector.
if (ngDeps.reflectables == null || ngDeps.reflectables.isEmpty) {
if ((ngDeps.imports == null || ngDeps.imports.every(_isDartImport)) &&
(ngDeps.exports == null || ngDeps.exports.every(_isDartImport))) {
Expand Down
24 changes: 19 additions & 5 deletions modules_dart/transform/lib/src/transform/common/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,32 @@ class TransformerOptions {
/// at any time.
final bool lazyTransformers;

/// Whether to generate compiled templates.
///
/// This option is strictly for internal testing and is not available as an
/// option on the transformer.
/// Setting this to `false` means that our generated .template.dart files do
/// not have any compiled templates or change detectors defined in them.
/// These files will not be usable, but this allows us to test the code output
/// of the transformer without breaking when compiled template internals
/// change.
final bool genCompiledTemplates;

TransformerOptions._internal(
this.entryPoints,
this.entryPointGlobs,
this.modeName,
this.mirrorMode,
this.initReflector,
this.annotationMatcher,
{this.genChangeDetectionDebugInfo,
this.reflectPropertiesAsAttributes,
this.platformDirectives,
this.platformPipes,
{this.formatCode,
this.genChangeDetectionDebugInfo,
this.genCompiledTemplates,
this.inlineViews,
this.lazyTransformers,
this.formatCode});
this.platformDirectives,
this.platformPipes,
this.reflectPropertiesAsAttributes});

factory TransformerOptions(List<String> entryPoints,
{String modeName: 'release',
Expand All @@ -95,6 +107,7 @@ class TransformerOptions {
List<ClassDescriptor> customAnnotationDescriptors: const [],
bool inlineViews: false,
bool genChangeDetectionDebugInfo: false,
bool genCompiledTemplates: true,
bool reflectPropertiesAsAttributes: false,
List<String> platformDirectives,
List<String> platformPipes,
Expand All @@ -108,6 +121,7 @@ class TransformerOptions {
return new TransformerOptions._internal(entryPoints, entryPointGlobs,
modeName, mirrorMode, initReflector, annotationMatcher,
genChangeDetectionDebugInfo: genChangeDetectionDebugInfo,
genCompiledTemplates: genCompiledTemplates,
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
Expand Down
Loading
X Tutup