Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 485904d

Browse files
Pete Bloismhevery
Pete Blois
authored andcommitted
Expression extractor transformer URI improvements
- Adding support for resolving 'packages/' uris (previously only handled '/packages/') from annotation templateUris. - Adding support for /packages/ uris to the html_files transformer parameter for referencing package-relative URIs. - Generating warnings for not-found files rather than build errors. Closes #879
1 parent 2d8dc25 commit 485904d

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

lib/tools/transformer/expression_generator.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class ExpressionGenerator extends Transformer with ResolverTransformer {
7777

7878
var controller = new StreamController<String>();
7979
var assets = options.htmlFiles
80-
.map((path) => new AssetId(id.package, path))
80+
.map((path) => _uriToAssetId(path, transform))
81+
.where((id) => id != null)
8182
.toList();
8283

8384
// Get all of the contents of templates in @NgComponent(templateUrl:'...')
@@ -95,7 +96,10 @@ class ExpressionGenerator extends Transformer with ResolverTransformer {
9596
// Add any manually specified HTML files.
9697
assets.map((id) => transform.readInputAsString(id))
9798
.map((future) =>
98-
future.then(controller.add).catchError(controller.addError))
99+
future.then(controller.add).catchError((e) {
100+
transform.logger.warning('Unable to find $id from html_files '
101+
'in pubspec.yaml.');
102+
}))
99103
).then((_) {
100104
controller.close();
101105
});
@@ -104,6 +108,19 @@ class ExpressionGenerator extends Transformer with ResolverTransformer {
104108
return controller.stream;
105109
}
106110

111+
AssetId _uriToAssetId(String uri, Transform transform) {
112+
if (path.url.isAbsolute(uri)) {
113+
var parts = path.url.split(uri);
114+
if (parts[1] == 'packages') {
115+
var pkgPath = path.url.join('lib', path.url.joinAll(parts.skip(3)));
116+
return new AssetId(parts[2], pkgPath);
117+
}
118+
transform.logger.warning('Cannot cache non-package absolute URIs. $uri');
119+
return null;
120+
}
121+
return new AssetId(transform.primaryInput.id.package, uri);
122+
}
123+
107124
/// Finds any HTML files referencing the primary input of the transform.
108125
Future<AssetId> _findHtmlEntry(Transform transform) {
109126
var id = transform.primaryInput.id;

lib/tools/transformer/referenced_uris.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,10 @@ class _Processor {
190190

191191
Future<_CacheEntry> cacheEntry(_CacheEntry entry) {
192192
return transform.readInputAsString(entry.assetId).then((contents) {
193-
if (contents == null) {
194-
warn('Unable to find ${entry.uri} at ${entry.assetId}', entry.element);
195-
}
196193
entry.contents = contents;
197194
return entry;
195+
}, onError: (e) {
196+
warn('Unable to find ${entry.uri} at ${entry.assetId}', entry.element);
198197
});
199198
}
200199

@@ -221,6 +220,10 @@ class _Processor {
221220
templateUriRewrites.forEach((regexp, replacement) {
222221
uri = uri.replaceFirst(regexp, replacement);
223222
});
223+
// Normalize packages/ uri's to be /packages/
224+
if (uri.startsWith('packages/')) {
225+
uri = '/' + uri;
226+
}
224227
return uri;
225228
}
226229

test/tools/transformer/expression_generator_spec.dart

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,29 @@ main() {
7171
selector: 'my-component')
7272
class FooComponent {}
7373
74+
@NgComponent(
75+
templateUrl: 'packages/b/bar.html',
76+
selector: 'my-component')
77+
class BarComponent {}
78+
7479
main() {}
7580
''',
7681
'a|lib/foo.html': '''
7782
<div>{{template.contents}}</div>''',
83+
'b|lib/bar.html': '''
84+
<div>{{bar}}</div>''',
7885
'a|web/index.html': '''
7986
<script src='main.dart' type='application/dart'></script>''',
8087
'angular|lib/angular.dart': libAngular,
8188
},
82-
getters: ['template', 'contents'],
83-
setters: ['template', 'contents'],
89+
getters: ['template', 'contents', 'bar'],
90+
setters: ['template', 'contents', 'bar'],
8491
symbols: []);
8592
});
8693

8794
it('should apply additional HTML files', () {
8895
htmlFiles.add('web/dummy.html');
96+
htmlFiles.add('/packages/b/bar.html');
8997
return generates(phases,
9098
inputs: {
9199
'a|web/main.dart': '''
@@ -95,24 +103,54 @@ main() {
95103
''',
96104
'a|web/dummy.html': '''
97105
<div>{{contents}}</div>''',
106+
'b|lib/bar.html': '''
107+
<div>{{bar}}</div>''',
98108
'a|web/index.html': '''
99109
<script src='main.dart' type='application/dart'></script>''',
100110
'angular|lib/angular.dart': libAngular,
101111
},
102-
getters: ['contents'],
103-
setters: ['contents'],
112+
getters: ['contents', 'bar'],
113+
setters: ['contents', 'bar'],
104114
symbols: []).whenComplete(() {
105115
htmlFiles.clear();
106116
});
107117
});
118+
119+
it('should warn on not-found HTML files', () {
120+
htmlFiles.add('web/not-found.html');
121+
return generates(phases,
122+
inputs: {
123+
'a|web/main.dart': '''
124+
import 'package:angular/angular.dart';
125+
126+
main() {}
127+
128+
@NgComponent(
129+
templateUrl: 'packages/b/not-found.html',
130+
selector: 'my-component')
131+
class BarComponent {}
132+
''',
133+
'a|web/index.html': '''
134+
<script src='main.dart' type='application/dart'></script>''',
135+
'angular|lib/angular.dart': libAngular,
136+
},
137+
messages: [
138+
'warning: Unable to find /packages/b/not-found.html at '
139+
'b|lib/not-found.html (web/main.dart 4 16)',
140+
'warning: Unable to find a|web/main.dart from html_files in '
141+
'pubspec.yaml.',
142+
]).whenComplete(() {
143+
htmlFiles.clear();
144+
});
145+
});
108146
});
109147
}
110148

111149
Future generates(List<List<Transformer>> phases,
112150
{ Map<String, String> inputs,
113-
List<String> getters,
114-
List<String> setters,
115-
List<String> symbols,
151+
List<String> getters: const [],
152+
List<String> setters: const [],
153+
List<String> symbols: const [],
116154
Iterable<String> messages: const []}) {
117155

118156
var buffer = new StringBuffer();

0 commit comments

Comments
 (0)