OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub_tests; | 5 library pub_tests; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 /// rewrites Dart files. | 91 /// rewrites Dart files. |
92 /// | 92 /// |
93 /// The transformer defines a constant named TOKEN whose value is [id]. When the | 93 /// The transformer defines a constant named TOKEN whose value is [id]. When the |
94 /// transformer transforms another Dart file, it will look for a "TOKEN" | 94 /// transformer transforms another Dart file, it will look for a "TOKEN" |
95 /// constant definition there and modify it to include *this* transformer's | 95 /// constant definition there and modify it to include *this* transformer's |
96 /// TOKEN value as well. | 96 /// TOKEN value as well. |
97 /// | 97 /// |
98 /// If [import] is passed, it should be the name of a package that defines its | 98 /// If [import] is passed, it should be the name of a package that defines its |
99 /// own TOKEN constant. The primary library of that package will be imported | 99 /// own TOKEN constant. The primary library of that package will be imported |
100 /// here and its TOKEN value will be added to this library's. | 100 /// here and its TOKEN value will be added to this library's. |
| 101 /// |
| 102 /// This transformer takes one configuration field: "addition". This is |
| 103 /// concatenated to its TOKEN value before adding it to the output library. |
101 String dartTransformer(String id, {String import}) { | 104 String dartTransformer(String id, {String import}) { |
102 if (import != null) { | 105 if (import != null) { |
103 id = '$id imports \${$import.TOKEN}'; | 106 id = '$id imports \${$import.TOKEN}'; |
104 import = 'import "package:$import/$import.dart" as $import;'; | 107 import = 'import "package:$import/$import.dart" as $import;'; |
105 } else { | 108 } else { |
106 import = ''; | 109 import = ''; |
107 } | 110 } |
108 | 111 |
109 return """ | 112 return """ |
110 import 'dart:async'; | 113 import 'dart:async'; |
111 | 114 |
112 import 'package:barback/barback.dart'; | 115 import 'package:barback/barback.dart'; |
113 $import | 116 $import |
114 | 117 |
| 118 import 'dart:io'; |
| 119 |
115 const TOKEN = "$id"; | 120 const TOKEN = "$id"; |
116 | 121 |
117 final _tokenRegExp = new RegExp(r'^const TOKEN = "(.*?)";\$', multiLine: true); | 122 final _tokenRegExp = new RegExp(r'^const TOKEN = "(.*?)";\$', multiLine: true); |
118 | 123 |
119 class DartTransformer extends Transformer { | 124 class DartTransformer extends Transformer { |
120 DartTransformer.asPlugin(); | 125 final BarbackSettings _settings; |
| 126 |
| 127 DartTransformer.asPlugin(this._settings); |
121 | 128 |
122 String get allowedExtensions => '.dart'; | 129 String get allowedExtensions => '.dart'; |
123 | 130 |
124 Future apply(Transform transform) { | 131 Future apply(Transform transform) { |
125 return transform.primaryInput.readAsString().then((contents) { | 132 return transform.primaryInput.readAsString().then((contents) { |
126 transform.addOutput(new Asset.fromString(transform.primaryInput.id, | 133 transform.addOutput(new Asset.fromString(transform.primaryInput.id, |
127 contents.replaceAllMapped(_tokenRegExp, (match) { | 134 contents.replaceAllMapped(_tokenRegExp, (match) { |
128 return 'const TOKEN = "(\${match[1]}, \$TOKEN)";'; | 135 var token = TOKEN; |
| 136 var addition = _settings.configuration["addition"]; |
| 137 if (addition != null) token += addition; |
| 138 return 'const TOKEN = "(\${match[1]}, \$token)";'; |
129 }))); | 139 }))); |
130 }); | 140 }); |
131 } | 141 } |
132 } | 142 } |
133 """; | 143 """; |
134 } | 144 } |
135 | 145 |
136 /// Schedules starting the `pub serve` process. | 146 /// Schedules starting the `pub serve` process. |
137 /// | 147 /// |
138 /// Unlike [pubServe], this doesn't determine the port number of the server, and | 148 /// Unlike [pubServe], this doesn't determine the port number of the server, and |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 /// included. Unlike [getServerUrl], this should only be called after the ports | 487 /// included. Unlike [getServerUrl], this should only be called after the ports |
478 /// are known. | 488 /// are known. |
479 String _getServerUrlSync([String root, String path]) { | 489 String _getServerUrlSync([String root, String path]) { |
480 if (root == null) root = 'web'; | 490 if (root == null) root = 'web'; |
481 expect(_ports, contains(root)); | 491 expect(_ports, contains(root)); |
482 var url = "http://localhost:${_ports[root]}"; | 492 var url = "http://localhost:${_ports[root]}"; |
483 if (path != null) url = "$url/$path"; | 493 if (path != null) url = "$url/$path"; |
484 return url; | 494 return url; |
485 } | 495 } |
486 | 496 |
OLD | NEW |