|
1 | 1 | library angular2.transform.common.code.source_module; |
2 | 2 |
|
3 | | -import 'package:analyzer/src/generated/scanner.dart' show Keyword; |
4 | 3 | import 'package:angular2/src/compiler/source_module.dart'; |
| 4 | +import 'package:analyzer/src/generated/scanner.dart' show Keyword; |
| 5 | +import 'package:angular2/src/transform/common/model/ng_deps_model.pb.dart'; |
| 6 | +import 'package:angular2/src/transform/common/model/source_module.dart'; |
5 | 7 |
|
6 | | -import 'uri.dart'; |
| 8 | +import 'ng_deps_code.dart'; |
7 | 9 |
|
8 | 10 | /// Writes the full Dart code for the provided [SourceModule]. |
9 | 11 | String writeSourceModule(SourceModule sourceModule, {String libraryName}) { |
10 | 12 | if (sourceModule == null) return null; |
11 | 13 | var buf = new StringBuffer(); |
| 14 | + final writer = new NgDepsWriter(buf); |
12 | 15 | var sourceWithImports = sourceModule.getSourceWithImports(); |
13 | 16 | libraryName = _sanitizeLibName( |
14 | 17 | libraryName != null ? libraryName : sourceModule.moduleUrl); |
15 | 18 | buf..writeln('library $libraryName;')..writeln(); |
16 | | - sourceWithImports.imports.forEach((import) { |
17 | | - // Format for importLine := [uri, prefix] |
18 | | - if (import.length != 2) { |
19 | | - throw new FormatException( |
20 | | - 'Unexpected import format! ' |
21 | | - 'Angular 2 compiler returned imports in an unexpected format. ' |
22 | | - 'Expected [<import_uri>, <prefix>].', |
23 | | - import.join(', ')); |
24 | | - } |
25 | | - buf.writeln(writeImportUri(import[0], |
26 | | - prefix: import[1], fromAbsolute: sourceModule.moduleUrl)); |
| 19 | + |
| 20 | + extractImports(sourceWithImports, sourceModule.moduleUrl).forEach((import) { |
| 21 | + writer.writeImportModel(import); |
27 | 22 | }); |
28 | 23 | buf..writeln()..writeln(sourceWithImports.source); |
29 | 24 |
|
30 | 25 | return buf.toString(); |
31 | 26 | } |
32 | 27 |
|
| 28 | +/// Uses `writer` to write a Dart library representing `model` and |
| 29 | +/// `sourceModule`. |
| 30 | +void writeTemplateFile( |
| 31 | + NgDepsWriterMixin writer, NgDepsModel model, SourceModule sourceModule) { |
| 32 | + if (model == null) return null; |
| 33 | + var sourceModuleCode = ''; |
| 34 | + if (sourceModule != null) { |
| 35 | + var sourceWithImports = sourceModule.getSourceWithImports(); |
| 36 | + sourceModuleCode = sourceWithImports.source; |
| 37 | + |
| 38 | + // Since we modify `imports`, make a copy to avoid changing the provided |
| 39 | + // value. |
| 40 | + var sourceModuleImports = |
| 41 | + extractImports(sourceWithImports, sourceModule.moduleUrl); |
| 42 | + model = model.clone(); |
| 43 | + model.imports.addAll(sourceModuleImports); |
| 44 | + } |
| 45 | + writer.writeNgDepsModel(model); |
| 46 | + writer.buffer..writeln()..writeln(sourceModuleCode); |
| 47 | +} |
| 48 | + |
33 | 49 | final _unsafeCharsPattern = new RegExp(r'[^a-zA-Z0-9_\.]'); |
34 | 50 | String _sanitizeLibName(String moduleUrl) { |
35 | 51 | var sanitized = |
|
0 commit comments