feat(dart/transform): Avoid overwriting assets

For easier debugging, avoid overwriting assets in Dart transform steps
where possible.
This commit is contained in:
Tim Blasi 2015-10-14 12:46:35 -07:00
parent e9c4c61986
commit ca5e31bc77
7 changed files with 63 additions and 40 deletions

View File

@ -8,9 +8,7 @@ const CSS_EXTENSION = '.css';
const SHIMMED_STYLESHEET_EXTENSION = '.css.shim.dart'; const SHIMMED_STYLESHEET_EXTENSION = '.css.shim.dart';
const NON_SHIMMED_STYLESHEET_EXTENSION = '.css.dart'; const NON_SHIMMED_STYLESHEET_EXTENSION = '.css.dart';
const DEPS_EXTENSION = '.ng_deps.dart'; const DEPS_EXTENSION = '.ng_deps.dart';
const DEPS_JSON_EXTENSION = '.ng_deps.json';
const META_EXTENSION = '.ng_meta.json'; const META_EXTENSION = '.ng_meta.json';
const TEMPLATE_EXTENSION = '.template.dart';
const REFLECTION_CAPABILITIES_NAME = 'ReflectionCapabilities'; const REFLECTION_CAPABILITIES_NAME = 'ReflectionCapabilities';
const REFLECTOR_IMPORT = 'package:angular2/src/core/reflection/reflection.dart'; const REFLECTOR_IMPORT = 'package:angular2/src/core/reflection/reflection.dart';
const REFLECTOR_PREFIX = '_ngRef'; const REFLECTOR_PREFIX = '_ngRef';
@ -18,14 +16,16 @@ const REGISTER_TYPE_METHOD_NAME = 'registerType';
const REGISTER_GETTERS_METHOD_NAME = 'registerGetters'; const REGISTER_GETTERS_METHOD_NAME = 'registerGetters';
const REGISTER_SETTERS_METHOD_NAME = 'registerSetters'; const REGISTER_SETTERS_METHOD_NAME = 'registerSetters';
const REGISTER_METHODS_METHOD_NAME = 'registerMethods'; const REGISTER_METHODS_METHOD_NAME = 'registerMethods';
const SUMMARY_META_EXTENSION = '.ng_summary.json';
const TEMPLATE_EXTENSION = '.template.dart';
/// Note that due to the implementation of `_toExtension`, ordering is /// Note that due to the implementation of `_toExtension`, ordering is
/// important. For example, putting '.dart' first in this list will cause /// important. For example, putting '.dart' first in this list will cause
/// incorrect behavior. /// incorrect behavior.
const ALL_EXTENSIONS = const [ const ALL_EXTENSIONS = const [
DEPS_EXTENSION, DEPS_EXTENSION,
DEPS_JSON_EXTENSION,
META_EXTENSION, META_EXTENSION,
SUMMARY_META_EXTENSION,
TEMPLATE_EXTENSION, TEMPLATE_EXTENSION,
'.dart' '.dart'
]; ];
@ -38,10 +38,6 @@ String toMetaExtension(String uri) =>
String toDepsExtension(String uri) => String toDepsExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, DEPS_EXTENSION); _toExtension(uri, ALL_EXTENSIONS, DEPS_EXTENSION);
/// Returns `uri` with its extension updated to [DEPS_JSON_EXTENSION].
String toJsonExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, DEPS_JSON_EXTENSION);
/// Returns `uri` with its extension updated to [TEMPLATES_EXTENSION]. /// Returns `uri` with its extension updated to [TEMPLATES_EXTENSION].
String toTemplateExtension(String uri) => String toTemplateExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, TEMPLATE_EXTENSION); _toExtension(uri, ALL_EXTENSIONS, TEMPLATE_EXTENSION);
@ -54,6 +50,10 @@ String toShimmedStylesheetExtension(String uri) =>
String toNonShimmedStylesheetExtension(String uri) => String toNonShimmedStylesheetExtension(String uri) =>
_toExtension(uri, const [CSS_EXTENSION], NON_SHIMMED_STYLESHEET_EXTENSION); _toExtension(uri, const [CSS_EXTENSION], NON_SHIMMED_STYLESHEET_EXTENSION);
/// Returns `uri` with its extension updated to [SUMMARY_META_EXTENSION].
String toSummaryExtension(String uri) =>
_toExtension(uri, ALL_EXTENSIONS, SUMMARY_META_EXTENSION);
/// Returns `uri` with its extension updated to `toExtension` if its /// Returns `uri` with its extension updated to `toExtension` if its
/// extension is currently in `fromExtension`. /// extension is currently in `fromExtension`.
String _toExtension( String _toExtension(

View File

@ -33,9 +33,11 @@ Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader,
return ngDepsModel; return ngDepsModel;
} }
final seen = new Set<String>();
for (var i = ngDepsModel.imports.length - 1; i >= 0; --i) { for (var i = ngDepsModel.imports.length - 1; i >= 0; --i) {
var import = ngDepsModel.imports[i]; var import = ngDepsModel.imports[i];
if (linkedDepsMap.containsKey(import.uri)) { if (linkedDepsMap.containsKey(import.uri) && !seen.contains(import.uri)) {
seen.add(import.uri);
var linkedModel = new ImportModel() var linkedModel = new ImportModel()
..isNgDeps = true ..isNgDeps = true
..uri = toDepsExtension(import.uri) ..uri = toDepsExtension(import.uri)
@ -46,7 +48,8 @@ Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader,
} }
for (var i = 0, iLen = ngDepsModel.exports.length; i < iLen; ++i) { for (var i = 0, iLen = ngDepsModel.exports.length; i < iLen; ++i) {
var export = ngDepsModel.exports[i]; var export = ngDepsModel.exports[i];
if (linkedDepsMap.containsKey(export.uri)) { if (linkedDepsMap.containsKey(export.uri) && !seen.contains(export.uri)) {
seen.add(export.uri);
var linkedModel = new ImportModel() var linkedModel = new ImportModel()
..isNgDeps = true ..isNgDeps = true
..uri = toDepsExtension(export.uri) ..uri = toDepsExtension(export.uri)
@ -73,16 +76,15 @@ Future<Map<String, String>> _processNgImports(NgDepsModel model,
return Future return Future
.wait( .wait(
importsAndExports.where(_isNotDartDirective).map((dynamic directive) { importsAndExports.where(_isNotDartDirective).map((dynamic directive) {
// The uri of the import or export with .dart replaced with .ng_meta.json. // Check whether the import or export generated summary NgMeta information.
// This is the json file containing Angular 2 codegen info, if one exists. final summaryJsonUri =
var linkedJsonUri = resolver.resolve(assetUri, toSummaryExtension(directive.uri));
resolver.resolve(assetUri, toMetaExtension(directive.uri)); return reader.hasInput(fromUri(summaryJsonUri)).then((hasInput) {
return reader.hasInput(fromUri(linkedJsonUri)).then((hasInput) {
if (hasInput) { if (hasInput) {
retVal[directive.uri] = linkedJsonUri; retVal[directive.uri] = summaryJsonUri;
} }
}, onError: (err, stack) { }, onError: (err, stack) {
logger.warning('Error while looking for $linkedJsonUri. ' logger.warning('Error while looking for $summaryJsonUri. '
'Message: $err\n' 'Message: $err\n'
'Stack: $stack'); 'Stack: $stack');
}); });

View File

@ -64,7 +64,7 @@ Future _linkRecursive(NgMeta ngMeta, AssetReader reader, AssetId assetId,
return Future.wait(ngMeta.ngDeps.exports return Future.wait(ngMeta.ngDeps.exports
.where((export) => !isDartCoreUri(export.uri)) .where((export) => !isDartCoreUri(export.uri))
.map((export) => .map((export) =>
_urlResolver.resolve(assetUri, toMetaExtension(export.uri))) _urlResolver.resolve(assetUri, toSummaryExtension(export.uri)))
.where((uri) => !seen.contains(uri)) .where((uri) => !seen.contains(uri))
.map((uri) async { .map((uri) async {
seen.add(uri); seen.add(uri);

View File

@ -10,19 +10,30 @@ import 'package:barback/barback.dart';
import 'ng_meta_linker.dart'; import 'ng_meta_linker.dart';
/// Transformer responsible for processing .ng_meta.json files created by /// Transformer responsible for processing .ng_summary.json files created by
/// {@link DirectiveProcessor} and "linking" them. /// {@link DirectiveProcessor} and "linking" them.
/// ///
/// This step ensures that for libraries that export, all `Directive`s reachable /// This step ensures that for libraries that export, all `Directive`s reachable
/// from that library are declared in its associated .ng_meta.json file. /// from that library are declared in its associated .ng_meta.json file.
/// ///
/// Said another way, after this step there should be an entry in this `NgMeta`
/// object for all `Directives` visible from its associated `.dart` file.
///
/// This step also ensures that, if `a.dart` imports `b.dart`, `a.ng_deps.dart`
/// imports `b.ng_deps.dart` (if it exists) and we note that this is a
/// ngDeps dependency, ensuring that a's `initReflector` function calls b's
/// `initReflector' function.
///
/// See `common/ng_meta.dart` for the JSON format of these files are serialized /// See `common/ng_meta.dart` for the JSON format of these files are serialized
/// to. /// to.
///
/// This transformer is part of a multi-phase transform.
/// See `angular2/src/transform/transformer.dart` for transformer ordering.
class DirectiveMetadataLinker extends Transformer { class DirectiveMetadataLinker extends Transformer {
final _encoder = const JsonEncoder.withIndent(' '); final _encoder = const JsonEncoder.withIndent(' ');
@override @override
bool isPrimary(AssetId id) => id.path.endsWith(META_EXTENSION); bool isPrimary(AssetId id) => id.path.endsWith(SUMMARY_META_EXTENSION);
@override @override
Future apply(Transform transform) { Future apply(Transform transform) {
@ -32,14 +43,21 @@ class DirectiveMetadataLinker extends Transformer {
return linkDirectiveMetadata( return linkDirectiveMetadata(
new AssetReader.fromTransform(transform), primaryId).then((ngMeta) { new AssetReader.fromTransform(transform), primaryId).then((ngMeta) {
if (ngMeta != null) { if (ngMeta != null) {
final outputId = _ngLinkedAssetId(primaryId);
if (!ngMeta.isEmpty) { if (!ngMeta.isEmpty) {
transform.addOutput(new Asset.fromString( transform.addOutput(new Asset.fromString(
primaryId, _encoder.convert(ngMeta.toJson()))); outputId, _encoder.convert(ngMeta.toJson())));
} else { } else {
// Not outputting an asset could confuse barback. // Not outputting an asset could confuse barback.
transform.addOutput(new Asset.fromString(primaryId, '')); transform.addOutput(new Asset.fromString(outputId, ''));
} }
} }); }
});
}); });
} }
} }
AssetId _ngLinkedAssetId(AssetId primaryInputId) {
return new AssetId(
primaryInputId.package, toMetaExtension(primaryInputId.path));
}

View File

@ -13,14 +13,13 @@ import 'package:barback/barback.dart';
import 'rewriter.dart'; import 'rewriter.dart';
/// Transformer responsible for processing all .dart assets and creating /// Transformer responsible for processing all .dart assets and creating
/// .ng_deps.dart files which register @Injectable annotated classes with the /// .ng_summary.json files which summarize those assets.
/// reflector.
/// ///
/// This will also create .ng_deps.dart files for classes annotated /// See `angular2/src/transform/common/ng_meta.dart` for the structure of these
/// with @Component, @View, @Directive, etc. /// output files.
/// ///
/// This transformer is the first phase in a two-phase transform. It should /// This transformer is part of a multi-phase transform.
/// be followed by {@link DirectiveLinker}. /// See `angular2/src/transform/transformer.dart` for transformer ordering.
class DirectiveProcessor extends Transformer { class DirectiveProcessor extends Transformer {
final TransformerOptions options; final TransformerOptions options;
final _encoder = const JsonEncoder.withIndent(' '); final _encoder = const JsonEncoder.withIndent(' ');
@ -42,12 +41,12 @@ class DirectiveProcessor extends Transformer {
return; return;
} }
transform.addOutput(new Asset.fromString( transform.addOutput(new Asset.fromString(
_ngMetaAssetId(primaryId), _encoder.convert(ngMeta.toJson()))); _ngSummaryAssetId(primaryId), _encoder.convert(ngMeta.toJson())));
}); });
} }
} }
AssetId _ngMetaAssetId(AssetId primaryInputId) { AssetId _ngSummaryAssetId(AssetId primaryInputId) {
return new AssetId( return new AssetId(
primaryInputId.package, toMetaExtension(primaryInputId.path)); primaryInputId.package, toSummaryExtension(primaryInputId.path));
} }

View File

@ -13,12 +13,15 @@ import 'package:barback/barback.dart';
import 'generator.dart'; import 'generator.dart';
/// {@link Transformer} responsible for detecting and processing Angular 2 templates. /// {@link Transformer} responsible for processing Angular 2 templates.
/// ///
/// {@link TemplateCompiler} uses the Angular 2 `Compiler` to process the templates, /// {@link TemplateCompiler} uses the Angular 2 `TemplateCompiler` to process
/// extracting information about what reflection is necessary to render and /// the templates, extracting information about what reflection is necessary to
/// use that template. It then generates code in place of those reflective /// render and use that template. It then generates code in place of those
/// accesses. /// reflective accesses.
///
/// This transformer is part of a multi-phase transform.
/// See `angular2/src/transform/transformer.dart` for transformer ordering.
class TemplateCompiler extends Transformer { class TemplateCompiler extends Transformer {
final TransformerOptions options; final TransformerOptions options;

View File

@ -5,6 +5,7 @@ import 'dart:convert';
import 'package:angular2/src/transform/common/asset_reader.dart'; import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart' as log; import 'package:angular2/src/transform/common/logging.dart' as log;
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/model/import_export_model.pb.dart'; import 'package:angular2/src/transform/common/model/import_export_model.pb.dart';
import 'package:angular2/src/transform/directive_metadata_linker/ng_meta_linker.dart'; import 'package:angular2/src/transform/directive_metadata_linker/ng_meta_linker.dart';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
@ -49,9 +50,9 @@ void allTests() {
bazNgMeta = new NgMeta(ngDeps: new NgDepsModel()); bazNgMeta = new NgMeta(ngDeps: new NgDepsModel());
barNgMeta.types[bazComponentMeta.type.name] = bazComponentMeta; barNgMeta.types[bazComponentMeta.type.name] = bazComponentMeta;
fooAssetId = new AssetId('a', 'lib/foo.ng_meta.json'); fooAssetId = new AssetId('a', toSummaryExtension('lib/foo.dart'));
barAssetId = new AssetId('a', 'lib/bar.ng_meta.json'); barAssetId = new AssetId('a', toSummaryExtension('lib/bar.dart'));
bazAssetId = new AssetId('a', 'lib/baz.ng_meta.json'); bazAssetId = new AssetId('a', toSummaryExtension('lib/baz.dart'));
updateReader(); updateReader();
}); });
@ -102,7 +103,7 @@ void allTests() {
fooNgMeta.ngDeps.exports fooNgMeta.ngDeps.exports
.add(new ExportModel()..uri = 'package:bar/bar.dart'); .add(new ExportModel()..uri = 'package:bar/bar.dart');
updateReader(); updateReader();
reader.addAsset(new AssetId('bar', 'lib/bar.ng_meta.json'), reader.addAsset(new AssetId('bar', toSummaryExtension('lib/bar.dart')),
JSON.encode(barNgMeta.toJson())); JSON.encode(barNgMeta.toJson()));
var extracted = await _testLink(reader, fooAssetId); var extracted = await _testLink(reader, fooAssetId);