Skip to content

Commit 46dd5fc

Browse files
author
Tim Blasi
committed
refactor(transform): Remove reflection_entry_points parameter
Remove the now unnecessary `reflection_entry_points` parameter from the Angular 2 transformer. Support glob syntax for `entry_points`.
1 parent 0f54ed0 commit 46dd5fc

File tree

14 files changed

+50
-62
lines changed

14 files changed

+50
-62
lines changed

modules/angular2/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies:
1313
barback: '^0.15.2+2'
1414
code_transformers: '^0.2.8'
1515
dart_style: '>=0.1.8 <0.3.0'
16+
glob: '^1.0.0'
1617
html: '^0.12.0'
1718
intl: '^0.12.4'
1819
logging: '>=0.9.0 <0.12.0'

modules_dart/transform/lib/src/transform/common/options.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
library angular2.transform.common.options;
22

3+
import 'package:glob/glob.dart';
4+
35
import 'annotation_matcher.dart';
46
import 'mirror_mode.dart';
57

@@ -19,13 +21,11 @@ const REFLECTION_ENTRY_POINT_PARAM = 'reflection_entry_points';
1921

2022
/// Provides information necessary to transform an Angular2 app.
2123
class TransformerOptions {
24+
final List<Glob> entryPointGlobs;
25+
2226
/// The path to the files where the application's calls to `bootstrap` are.
2327
final List<String> entryPoints;
2428

25-
/// The paths to the files where the application's {@link ReflectionCapabilities}
26-
/// are set.
27-
final List<String> reflectionEntryPoints;
28-
2929
/// The `BarbackMode#name` we are running in.
3030
final String modeName;
3131

@@ -62,7 +62,7 @@ class TransformerOptions {
6262

6363
TransformerOptions._internal(
6464
this.entryPoints,
65-
this.reflectionEntryPoints,
65+
this.entryPointGlobs,
6666
this.modeName,
6767
this.mirrorMode,
6868
this.initReflector,
@@ -74,8 +74,7 @@ class TransformerOptions {
7474
this.formatCode});
7575

7676
factory TransformerOptions(List<String> entryPoints,
77-
{List<String> reflectionEntryPoints,
78-
String modeName: 'release',
77+
{String modeName: 'release',
7978
MirrorMode mirrorMode: MirrorMode.none,
8079
bool initReflector: true,
8180
List<ClassDescriptor> customAnnotationDescriptors: const [],
@@ -84,15 +83,15 @@ class TransformerOptions {
8483
bool generateChangeDetectors: true,
8584
bool reflectPropertiesAsAttributes: true,
8685
bool formatCode: false}) {
87-
if (reflectionEntryPoints == null || reflectionEntryPoints.isEmpty) {
88-
reflectionEntryPoints = entryPoints;
89-
}
9086
var annotationMatcher = new AnnotationMatcher()
9187
..addAll(customAnnotationDescriptors);
9288
optimizationPhases = optimizationPhases.isNegative ? 0 : optimizationPhases;
89+
var entryPointGlobs = entryPoints != null
90+
? entryPoints.map((path) => new Glob(path)).toList(growable: false)
91+
: null;
9392
return new TransformerOptions._internal(
9493
entryPoints,
95-
reflectionEntryPoints,
94+
entryPointGlobs,
9695
modeName,
9796
mirrorMode,
9897
initReflector,

modules_dart/transform/lib/src/transform/common/options_reader.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import 'options.dart';
77

88
TransformerOptions parseBarbackSettings(BarbackSettings settings) {
99
var config = settings.configuration;
10+
_warnDeprecated(config);
1011
var entryPoints = _readFileList(config, ENTRY_POINT_PARAM);
11-
var reflectionEntryPoints =
12-
_readFileList(config, REFLECTION_ENTRY_POINT_PARAM);
1312
var initReflector =
1413
_readBool(config, INIT_REFLECTOR_PARAM, defaultValue: true);
1514
var inlineViews = _readBool(config, INLINE_VIEWS_PARAM, defaultValue: true);
@@ -35,7 +34,6 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
3534
var optimizationPhases = _readInt(config, OPTIMIZATION_PHASES_PARAM,
3635
defaultValue: DEFAULT_OPTIMIZATION_PHASES);
3736
return new TransformerOptions(entryPoints,
38-
reflectionEntryPoints: reflectionEntryPoints,
3937
modeName: settings.mode.name,
4038
mirrorMode: mirrorMode,
4139
initReflector: initReflector,
@@ -132,3 +130,10 @@ const CUSTOM_ANNOTATIONS_ERROR = '''
132130
- name: ...
133131
import: ...
134132
superClass: ...''';
133+
134+
void _warnDeprecated(Map config) {
135+
if (config.containsKey(REFLECTION_ENTRY_POINT_PARAM)) {
136+
print('${REFLECTION_ENTRY_POINT_PARAM} is no longer necessary for '
137+
'Angular 2 apps. Please remove it from your pubspec.');
138+
}
139+
}
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
library angular2.transform.reflection_remover.codegen;
22

3-
import 'package:path/path.dart' as path;
4-
53
import 'package:angular2/src/transform/common/names.dart';
4+
import 'package:barback/barback.dart';
5+
import 'package:path/path.dart' as path;
66

77
class Codegen {
88
static const _PREFIX_BASE = 'ngStaticInit';
9+
final AssetId reflectionEntryPoint;
910

1011
/// The prefix used to import our generated file.
1112
final String prefix;
1213

13-
/// The import uris
14-
final Iterable<String> importUris;
15-
16-
Codegen(String reflectionEntryPointPath, Iterable<String> newEntryPointPaths,
17-
{String prefix})
18-
: this.prefix = prefix == null ? _PREFIX_BASE : prefix,
19-
importUris = newEntryPointPaths.map((p) => path
20-
.relative(p, from: path.dirname(reflectionEntryPointPath))
21-
.replaceAll('\\', '/')) {
14+
Codegen(this.reflectionEntryPoint, {String prefix})
15+
: this.prefix = prefix == null ? _PREFIX_BASE : prefix {
2216
if (this.prefix.isEmpty) throw new ArgumentError.value('(empty)', 'prefix');
2317
}
2418

@@ -28,18 +22,14 @@ class Codegen {
2822
/// The code generated here should follow the example of code generated for
2923
/// an {@link ImportDirective} node.
3024
String codegenImport() {
31-
var count = 0;
32-
return importUris
33-
.map((importUri) => 'import \'${importUri}\' as ${prefix}${count++};')
34-
.join('');
25+
var importUri = path
26+
.basename(reflectionEntryPoint.changeExtension(DEPS_EXTENSION).path);
27+
return '''import '$importUri' as $prefix;''';
3528
}
3629

3730
/// Generates code to call the method which sets up Angular2 reflection
3831
/// statically.
3932
String codegenSetupReflectionCall() {
40-
var count = 0;
41-
return importUris
42-
.map((_) => '${prefix}${count++}.${SETUP_METHOD_NAME}();')
43-
.join('');
33+
return '$prefix.$SETUP_METHOD_NAME();';
4434
}
4535
}

modules_dart/transform/lib/src/transform/reflection_remover/remove_reflection_capabilities.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ library angular2.transform.reflection_remover.remove_reflection_capabilities;
22

33
import 'dart:async';
44
import 'package:analyzer/analyzer.dart';
5-
import 'package:barback/barback.dart';
65
import 'package:angular2/src/transform/common/asset_reader.dart';
76
import 'package:angular2/src/transform/common/mirror_mode.dart';
7+
import 'package:barback/barback.dart';
88

99
import 'codegen.dart';
1010
import 'rewriter.dart';
@@ -15,16 +15,14 @@ import 'rewriter.dart';
1515
///
1616
/// This only searches the code in `reflectionEntryPoint`, not `part`s,
1717
/// `import`s, `export`s, etc.
18-
Future<String> removeReflectionCapabilities(AssetReader reader,
19-
AssetId reflectionEntryPoint, Iterable<AssetId> newEntryPoints,
18+
Future<String> removeReflectionCapabilities(
19+
AssetReader reader, AssetId reflectionEntryPoint,
2020
{MirrorMode mirrorMode: MirrorMode.none,
2121
bool writeStaticInit: true}) async {
2222
var code = await reader.readAsString(reflectionEntryPoint);
23-
var reflectionEntryPointPath = reflectionEntryPoint.path;
24-
var newEntryPointPaths = newEntryPoints.map((id) => id.path);
2523

26-
var codegen = new Codegen(reflectionEntryPointPath, newEntryPointPaths);
24+
var codegen = new Codegen(reflectionEntryPoint);
2725
return new Rewriter(code, codegen,
2826
mirrorMode: mirrorMode, writeStaticInit: writeStaticInit)
29-
.rewrite(parseCompilationUnit(code, name: reflectionEntryPointPath));
27+
.rewrite(parseCompilationUnit(code, name: reflectionEntryPoint.path));
3028
}

modules_dart/transform/lib/src/transform/reflection_remover/transformer.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ class ReflectionRemover extends Transformer {
2525
ReflectionRemover(this.options);
2626

2727
@override
28-
bool isPrimary(AssetId id) => options.reflectionEntryPoints != null &&
29-
options.reflectionEntryPoints.contains(id.path);
28+
bool isPrimary(AssetId id) => options.entryPointGlobs != null &&
29+
options.entryPointGlobs.any((g) => g.matches(id.path));
3030

3131
@override
3232
Future apply(Transform transform) async {
3333
await log.initZoned(transform, () async {
34-
var newEntryPoints = options.entryPoints.map((entryPoint) {
35-
return new AssetId(transform.primaryInput.id.package, entryPoint)
36-
.changeExtension(DEPS_EXTENSION);
37-
});
38-
var reader = new AssetReader.fromTransform(transform);
39-
4034
var mirrorMode = options.mirrorMode;
4135
var writeStaticInit = options.initReflector;
4236
if (options.modeName == TRANSFORM_DYNAMIC_MODE) {
@@ -48,7 +42,7 @@ class ReflectionRemover extends Transformer {
4842
}
4943

5044
var transformedCode = await removeReflectionCapabilities(
51-
reader, transform.primaryInput.id, newEntryPoints,
45+
new AssetReader.fromTransform(transform), transform.primaryInput.id,
5246
mirrorMode: mirrorMode, writeStaticInit: writeStaticInit);
5347
transform.addOutput(
5448
new Asset.fromString(transform.primaryInput.id, transformedCode));

modules_dart/transform/test/transform/integration/all_tests.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ main() {
1414
var formatter = new DartFormatter();
1515
var transform = new AngularTransformerGroup(new TransformerOptions(
1616
['web/index.dart'],
17-
reflectionEntryPoints: ['web/index.dart'],
1817
formatCode: true));
1918

2019
class IntegrationTestConfig {

modules_dart/transform/test/transform/integration/simple_annotation_files/expected/index.ng_deps.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'index.dart';
44
export 'index.dart';
55
import 'package:angular2/src/core/reflection/reflection.dart' as _ngRef;
66
import 'package:angular2/bootstrap_static.dart';
7-
import 'index.ng_deps.dart' as ngStaticInit0;
7+
import 'index.ng_deps.dart' as ngStaticInit;
88
import 'package:angular2/src/core/reflection/reflection.dart';
99
import 'bar.dart';
1010
import 'bar.ng_deps.dart' as i0;

modules_dart/transform/test/transform/reflection_remover/all_tests.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:analyzer/analyzer.dart';
44
import 'package:angular2/src/transform/common/mirror_mode.dart';
55
import 'package:angular2/src/transform/reflection_remover/codegen.dart';
66
import 'package:angular2/src/transform/reflection_remover/rewriter.dart';
7+
import 'package:barback/barback.dart';
78
import 'package:guinness/guinness.dart';
89

910
import 'reflection_remover_files/expected/index.dart' as expected;
@@ -16,7 +17,8 @@ import '../common/read_file.dart';
1617
main() => allTests();
1718

1819
void allTests() {
19-
var codegen = new Codegen('web/index.dart', ['web/index.ng_deps.dart']);
20+
var assetId = new AssetId('a', 'web/index.dart');
21+
var codegen = new Codegen(assetId);
2022
var code = readFile('reflection_remover/index.dart').replaceAll('\r\n', '\n');
2123
var bootstrapCode = readFile('reflection_remover/bootstrap_files/index.dart')
2224
.replaceAll('\r\n', '\n');

modules_dart/transform/test/transform/reflection_remover/bootstrap_files/expected/index.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ library angular2.test.transform.reflection_remover.reflection_remover_files;
1111
var code = """
1212
library web_foo;
1313
14-
import 'package:angular2/bootstrap_static.dart';import 'index.ng_deps.dart' as ngStaticInit0;
14+
import 'package:angular2/bootstrap_static.dart';import 'index.ng_deps.dart' as ngStaticInit;
1515
1616
void main() async {
17-
var appRef = await bootstrapStatic(MyComponent, null, () { ngStaticInit0.initReflector(); });
17+
var appRef = await bootstrapStatic(MyComponent, null, () { ngStaticInit.initReflector(); });
1818
}
1919
""";

0 commit comments

Comments
 (0)