Skip to content

Commit ca5e31b

Browse files
author
Tim Blasi
committed
feat(dart/transform): Avoid overwriting assets
For easier debugging, avoid overwriting assets in Dart transform steps where possible.
1 parent e9c4c61 commit ca5e31b

7 files changed

Lines changed: 63 additions & 40 deletions

File tree

‎modules_dart/transform/lib/src/transform/common/names.dart‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ const CSS_EXTENSION = '.css';
88
const SHIMMED_STYLESHEET_EXTENSION = '.css.shim.dart';
99
const NON_SHIMMED_STYLESHEET_EXTENSION = '.css.dart';
1010
const DEPS_EXTENSION = '.ng_deps.dart';
11-
const DEPS_JSON_EXTENSION = '.ng_deps.json';
1211
const META_EXTENSION = '.ng_meta.json';
13-
const TEMPLATE_EXTENSION = '.template.dart';
1412
const REFLECTION_CAPABILITIES_NAME = 'ReflectionCapabilities';
1513
const REFLECTOR_IMPORT = 'package:angular2/src/core/reflection/reflection.dart';
1614
const REFLECTOR_PREFIX = '_ngRef';
1715
const REGISTER_TYPE_METHOD_NAME = 'registerType';
1816
const REGISTER_GETTERS_METHOD_NAME = 'registerGetters';
1917
const REGISTER_SETTERS_METHOD_NAME = 'registerSetters';
2018
const REGISTER_METHODS_METHOD_NAME = 'registerMethods';
19+
const SUMMARY_META_EXTENSION = '.ng_summary.json';
20+
const TEMPLATE_EXTENSION = '.template.dart';
2121

2222
/// Note that due to the implementation of `_toExtension`, ordering is
2323
/// important. For example, putting '.dart' first in this list will cause
2424
/// incorrect behavior.
2525
const ALL_EXTENSIONS = const [
2626
DEPS_EXTENSION,
27-
DEPS_JSON_EXTENSION,
2827
META_EXTENSION,
28+
SUMMARY_META_EXTENSION,
2929
TEMPLATE_EXTENSION,
3030
'.dart'
3131
];
@@ -38,10 +38,6 @@ String toMetaExtension(String uri) =>
3838
String toDepsExtension(String uri) =>
3939
_toExtension(uri, ALL_EXTENSIONS, DEPS_EXTENSION);
4040

41-
/// Returns `uri` with its extension updated to [DEPS_JSON_EXTENSION].
42-
String toJsonExtension(String uri) =>
43-
_toExtension(uri, ALL_EXTENSIONS, DEPS_JSON_EXTENSION);
44-
4541
/// Returns `uri` with its extension updated to [TEMPLATES_EXTENSION].
4642
String toTemplateExtension(String uri) =>
4743
_toExtension(uri, ALL_EXTENSIONS, TEMPLATE_EXTENSION);
@@ -54,6 +50,10 @@ String toShimmedStylesheetExtension(String uri) =>
5450
String toNonShimmedStylesheetExtension(String uri) =>
5551
_toExtension(uri, const [CSS_EXTENSION], NON_SHIMMED_STYLESHEET_EXTENSION);
5652

53+
/// Returns `uri` with its extension updated to [SUMMARY_META_EXTENSION].
54+
String toSummaryExtension(String uri) =>
55+
_toExtension(uri, ALL_EXTENSIONS, SUMMARY_META_EXTENSION);
56+
5757
/// Returns `uri` with its extension updated to `toExtension` if its
5858
/// extension is currently in `fromExtension`.
5959
String _toExtension(

‎modules_dart/transform/lib/src/transform/directive_metadata_linker/ng_deps_linker.dart‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader,
3333
return ngDepsModel;
3434
}
3535

36+
final seen = new Set<String>();
3637
for (var i = ngDepsModel.imports.length - 1; i >= 0; --i) {
3738
var import = ngDepsModel.imports[i];
38-
if (linkedDepsMap.containsKey(import.uri)) {
39+
if (linkedDepsMap.containsKey(import.uri) && !seen.contains(import.uri)) {
40+
seen.add(import.uri);
3941
var linkedModel = new ImportModel()
4042
..isNgDeps = true
4143
..uri = toDepsExtension(import.uri)
@@ -46,7 +48,8 @@ Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader,
4648
}
4749
for (var i = 0, iLen = ngDepsModel.exports.length; i < iLen; ++i) {
4850
var export = ngDepsModel.exports[i];
49-
if (linkedDepsMap.containsKey(export.uri)) {
51+
if (linkedDepsMap.containsKey(export.uri) && !seen.contains(export.uri)) {
52+
seen.add(export.uri);
5053
var linkedModel = new ImportModel()
5154
..isNgDeps = true
5255
..uri = toDepsExtension(export.uri)
@@ -73,16 +76,15 @@ Future<Map<String, String>> _processNgImports(NgDepsModel model,
7376
return Future
7477
.wait(
7578
importsAndExports.where(_isNotDartDirective).map((dynamic directive) {
76-
// The uri of the import or export with .dart replaced with .ng_meta.json.
77-
// This is the json file containing Angular 2 codegen info, if one exists.
78-
var linkedJsonUri =
79-
resolver.resolve(assetUri, toMetaExtension(directive.uri));
80-
return reader.hasInput(fromUri(linkedJsonUri)).then((hasInput) {
79+
// Check whether the import or export generated summary NgMeta information.
80+
final summaryJsonUri =
81+
resolver.resolve(assetUri, toSummaryExtension(directive.uri));
82+
return reader.hasInput(fromUri(summaryJsonUri)).then((hasInput) {
8183
if (hasInput) {
82-
retVal[directive.uri] = linkedJsonUri;
84+
retVal[directive.uri] = summaryJsonUri;
8385
}
8486
}, onError: (err, stack) {
85-
logger.warning('Error while looking for $linkedJsonUri. '
87+
logger.warning('Error while looking for $summaryJsonUri. '
8688
'Message: $err\n'
8789
'Stack: $stack');
8890
});

‎modules_dart/transform/lib/src/transform/directive_metadata_linker/ng_meta_linker.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Future _linkRecursive(NgMeta ngMeta, AssetReader reader, AssetId assetId,
6464
return Future.wait(ngMeta.ngDeps.exports
6565
.where((export) => !isDartCoreUri(export.uri))
6666
.map((export) =>
67-
_urlResolver.resolve(assetUri, toMetaExtension(export.uri)))
67+
_urlResolver.resolve(assetUri, toSummaryExtension(export.uri)))
6868
.where((uri) => !seen.contains(uri))
6969
.map((uri) async {
7070
seen.add(uri);

‎modules_dart/transform/lib/src/transform/directive_metadata_linker/transformer.dart‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@ import 'package:barback/barback.dart';
1010

1111
import 'ng_meta_linker.dart';
1212

13-
/// Transformer responsible for processing .ng_meta.json files created by
13+
/// Transformer responsible for processing .ng_summary.json files created by
1414
/// {@link DirectiveProcessor} and "linking" them.
1515
///
1616
/// This step ensures that for libraries that export, all `Directive`s reachable
1717
/// from that library are declared in its associated .ng_meta.json file.
1818
///
19+
/// Said another way, after this step there should be an entry in this `NgMeta`
20+
/// object for all `Directives` visible from its associated `.dart` file.
21+
///
22+
/// This step also ensures that, if `a.dart` imports `b.dart`, `a.ng_deps.dart`
23+
/// imports `b.ng_deps.dart` (if it exists) and we note that this is a
24+
/// ngDeps dependency, ensuring that a's `initReflector` function calls b's
25+
/// `initReflector' function.
26+
///
1927
/// See `common/ng_meta.dart` for the JSON format of these files are serialized
2028
/// to.
29+
///
30+
/// This transformer is part of a multi-phase transform.
31+
/// See `angular2/src/transform/transformer.dart` for transformer ordering.
2132
class DirectiveMetadataLinker extends Transformer {
2233
final _encoder = const JsonEncoder.withIndent(' ');
2334

2435
@override
25-
bool isPrimary(AssetId id) => id.path.endsWith(META_EXTENSION);
36+
bool isPrimary(AssetId id) => id.path.endsWith(SUMMARY_META_EXTENSION);
2637

2738
@override
2839
Future apply(Transform transform) {
@@ -32,14 +43,21 @@ class DirectiveMetadataLinker extends Transformer {
3243
return linkDirectiveMetadata(
3344
new AssetReader.fromTransform(transform), primaryId).then((ngMeta) {
3445
if (ngMeta != null) {
46+
final outputId = _ngLinkedAssetId(primaryId);
3547
if (!ngMeta.isEmpty) {
3648
transform.addOutput(new Asset.fromString(
37-
primaryId, _encoder.convert(ngMeta.toJson())));
49+
outputId, _encoder.convert(ngMeta.toJson())));
3850
} else {
3951
// Not outputting an asset could confuse barback.
40-
transform.addOutput(new Asset.fromString(primaryId, ''));
52+
transform.addOutput(new Asset.fromString(outputId, ''));
4153
}
42-
} });
54+
}
55+
});
4356
});
4457
}
4558
}
59+
60+
AssetId _ngLinkedAssetId(AssetId primaryInputId) {
61+
return new AssetId(
62+
primaryInputId.package, toMetaExtension(primaryInputId.path));
63+
}

‎modules_dart/transform/lib/src/transform/directive_processor/transformer.dart‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ import 'package:barback/barback.dart';
1313
import 'rewriter.dart';
1414

1515
/// Transformer responsible for processing all .dart assets and creating
16-
/// .ng_deps.dart files which register @Injectable annotated classes with the
17-
/// reflector.
16+
/// .ng_summary.json files which summarize those assets.
1817
///
19-
/// This will also create .ng_deps.dart files for classes annotated
20-
/// with @Component, @View, @Directive, etc.
18+
/// See `angular2/src/transform/common/ng_meta.dart` for the structure of these
19+
/// output files.
2120
///
22-
/// This transformer is the first phase in a two-phase transform. It should
23-
/// be followed by {@link DirectiveLinker}.
21+
/// This transformer is part of a multi-phase transform.
22+
/// See `angular2/src/transform/transformer.dart` for transformer ordering.
2423
class DirectiveProcessor extends Transformer {
2524
final TransformerOptions options;
2625
final _encoder = const JsonEncoder.withIndent(' ');
@@ -42,12 +41,12 @@ class DirectiveProcessor extends Transformer {
4241
return;
4342
}
4443
transform.addOutput(new Asset.fromString(
45-
_ngMetaAssetId(primaryId), _encoder.convert(ngMeta.toJson())));
44+
_ngSummaryAssetId(primaryId), _encoder.convert(ngMeta.toJson())));
4645
});
4746
}
4847
}
4948

50-
AssetId _ngMetaAssetId(AssetId primaryInputId) {
49+
AssetId _ngSummaryAssetId(AssetId primaryInputId) {
5150
return new AssetId(
52-
primaryInputId.package, toMetaExtension(primaryInputId.path));
51+
primaryInputId.package, toSummaryExtension(primaryInputId.path));
5352
}

‎modules_dart/transform/lib/src/transform/template_compiler/transformer.dart‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import 'package:barback/barback.dart';
1313

1414
import 'generator.dart';
1515

16-
/// {@link Transformer} responsible for detecting and processing Angular 2 templates.
16+
/// {@link Transformer} responsible for processing Angular 2 templates.
1717
///
18-
/// {@link TemplateCompiler} uses the Angular 2 `Compiler` to process the templates,
19-
/// extracting information about what reflection is necessary to render and
20-
/// use that template. It then generates code in place of those reflective
21-
/// accesses.
18+
/// {@link TemplateCompiler} uses the Angular 2 `TemplateCompiler` to process
19+
/// the templates, extracting information about what reflection is necessary to
20+
/// render and use that template. It then generates code in place of those
21+
/// reflective accesses.
22+
///
23+
/// This transformer is part of a multi-phase transform.
24+
/// See `angular2/src/transform/transformer.dart` for transformer ordering.
2225
class TemplateCompiler extends Transformer {
2326
final TransformerOptions options;
2427

‎modules_dart/transform/test/transform/directive_metadata_linker/all_tests.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:convert';
55

66
import 'package:angular2/src/transform/common/asset_reader.dart';
77
import 'package:angular2/src/transform/common/logging.dart' as log;
8+
import 'package:angular2/src/transform/common/names.dart';
89
import 'package:angular2/src/transform/common/model/import_export_model.pb.dart';
910
import 'package:angular2/src/transform/directive_metadata_linker/ng_meta_linker.dart';
1011
import 'package:barback/barback.dart';
@@ -49,9 +50,9 @@ void allTests() {
4950
bazNgMeta = new NgMeta(ngDeps: new NgDepsModel());
5051
barNgMeta.types[bazComponentMeta.type.name] = bazComponentMeta;
5152

52-
fooAssetId = new AssetId('a', 'lib/foo.ng_meta.json');
53-
barAssetId = new AssetId('a', 'lib/bar.ng_meta.json');
54-
bazAssetId = new AssetId('a', 'lib/baz.ng_meta.json');
53+
fooAssetId = new AssetId('a', toSummaryExtension('lib/foo.dart'));
54+
barAssetId = new AssetId('a', toSummaryExtension('lib/bar.dart'));
55+
bazAssetId = new AssetId('a', toSummaryExtension('lib/baz.dart'));
5556
updateReader();
5657
});
5758

@@ -102,7 +103,7 @@ void allTests() {
102103
fooNgMeta.ngDeps.exports
103104
.add(new ExportModel()..uri = 'package:bar/bar.dart');
104105
updateReader();
105-
reader.addAsset(new AssetId('bar', 'lib/bar.ng_meta.json'),
106+
reader.addAsset(new AssetId('bar', toSummaryExtension('lib/bar.dart')),
106107
JSON.encode(barNgMeta.toJson()));
107108

108109
var extracted = await _testLink(reader, fooAssetId);

0 commit comments

Comments
 (0)