OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:async/async.dart'; | 9 import 'package:async/async.dart'; |
| 10 import 'package:package_resolver/package_resolver.dart'; |
10 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
11 import 'package:pool/pool.dart'; | 12 import 'package:pool/pool.dart'; |
12 | 13 |
13 import '../../util/io.dart'; | 14 import '../../util/io.dart'; |
14 import '../configuration.dart'; | 15 import '../configuration.dart'; |
15 import '../load_exception.dart'; | 16 import '../load_exception.dart'; |
16 | 17 |
17 /// A regular expression matching the first status line printed by dart2js. | 18 /// A regular expression matching the first status line printed by dart2js. |
18 final _dart2jsStatus = | 19 final _dart2jsStatus = |
19 new RegExp(r"^Dart file \(.*\) compiled to JavaScript: .*\n?"); | 20 new RegExp(r"^Dart file \(.*\) compiled to JavaScript: .*\n?"); |
(...skipping 17 matching lines...) Expand all Loading... |
37 /// The memoizer for running [close] exactly once. | 38 /// The memoizer for running [close] exactly once. |
38 final _closeMemo = new AsyncMemoizer(); | 39 final _closeMemo = new AsyncMemoizer(); |
39 | 40 |
40 /// Creates a compiler pool that multiple instances of `dart2js` at once. | 41 /// Creates a compiler pool that multiple instances of `dart2js` at once. |
41 CompilerPool(Configuration config) | 42 CompilerPool(Configuration config) |
42 : _pool = new Pool(config.concurrency), | 43 : _pool = new Pool(config.concurrency), |
43 _config = config; | 44 _config = config; |
44 | 45 |
45 /// Compile the Dart code at [dartPath] to [jsPath]. | 46 /// Compile the Dart code at [dartPath] to [jsPath]. |
46 /// | 47 /// |
47 /// This wraps the Dart code in the standard browser-testing wrapper. If | 48 /// This wraps the Dart code in the standard browser-testing wrapper. |
48 /// [packageRoot] is provided, it's used as the package root for the | |
49 /// compilation. | |
50 /// | 49 /// |
51 /// The returned [Future] will complete once the `dart2js` process completes | 50 /// The returned [Future] will complete once the `dart2js` process completes |
52 /// *and* all its output has been printed to the command line. | 51 /// *and* all its output has been printed to the command line. |
53 Future compile(String dartPath, String jsPath, {String packageRoot}) { | 52 Future compile(String dartPath, String jsPath) { |
54 return _pool.withResource(() { | 53 return _pool.withResource(() { |
55 if (_closed) return null; | 54 if (_closed) return null; |
56 | 55 |
57 return withTempDir((dir) async { | 56 return withTempDir((dir) async { |
58 var wrapperPath = p.join(dir, "runInBrowser.dart"); | 57 var wrapperPath = p.join(dir, "runInBrowser.dart"); |
59 new File(wrapperPath).writeAsStringSync(''' | 58 new File(wrapperPath).writeAsStringSync(''' |
60 import "package:stream_channel/stream_channel.dart"; | 59 import "package:stream_channel/stream_channel.dart"; |
61 | 60 |
62 import "package:test/src/runner/plugin/remote_platform_helpers.dart"; | 61 import "package:test/src/runner/plugin/remote_platform_helpers.dart"; |
63 import "package:test/src/runner/browser/post_message_channel.dart"; | 62 import "package:test/src/runner/browser/post_message_channel.dart"; |
64 | 63 |
65 import "${p.toUri(p.absolute(dartPath))}" as test; | 64 import "${p.toUri(p.absolute(dartPath))}" as test; |
66 | 65 |
67 main(_) async { | 66 main(_) async { |
68 var channel = serializeSuite(() => test.main, hidePrints: false); | 67 var channel = serializeSuite(() => test.main, hidePrints: false); |
69 postMessageChannel().pipe(channel); | 68 postMessageChannel().pipe(channel); |
70 } | 69 } |
71 '''); | 70 '''); |
72 | 71 |
73 var dart2jsPath = _config.dart2jsPath; | 72 var dart2jsPath = _config.dart2jsPath; |
74 if (Platform.isWindows) dart2jsPath += '.bat'; | 73 if (Platform.isWindows) dart2jsPath += '.bat'; |
75 | 74 |
76 var args = ["--checked", wrapperPath, "--out=$jsPath"] | 75 var args = [ |
77 ..addAll(_config.dart2jsArgs); | 76 "--checked", |
78 | 77 wrapperPath, |
79 if (packageRoot != null) { | 78 "--out=$jsPath", |
80 args.add("--package-root=${p.toUri(p.absolute(packageRoot))}"); | 79 await PackageResolver.current.processArgument |
81 } | 80 ]..addAll(_config.dart2jsArgs); |
82 | 81 |
83 if (_config.color) args.add("--enable-diagnostic-colors"); | 82 if (_config.color) args.add("--enable-diagnostic-colors"); |
84 | 83 |
85 var process = await Process.start(dart2jsPath, args); | 84 var process = await Process.start(dart2jsPath, args); |
86 if (_closed) { | 85 if (_closed) { |
87 process.kill(); | 86 process.kill(); |
88 return; | 87 return; |
89 } | 88 } |
90 | 89 |
91 _processes.add(process); | 90 _processes.add(process); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 /// have been killed and all resources released. | 146 /// have been killed and all resources released. |
148 Future close() { | 147 Future close() { |
149 return _closeMemo.runOnce(() async { | 148 return _closeMemo.runOnce(() async { |
150 await Future.wait(_processes.map((process) async { | 149 await Future.wait(_processes.map((process) async { |
151 process.kill(); | 150 process.kill(); |
152 await process.exitCode; | 151 await process.exitCode; |
153 })); | 152 })); |
154 }); | 153 }); |
155 } | 154 } |
156 } | 155 } |
OLD | NEW |