forked from justinfagnani/buildtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart2js_task.dart
71 lines (58 loc) · 2.35 KB
/
dart2js_task.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library dart2js;
import 'dart:io';
import 'dart:uri';
import 'package:buildtool/buildtool.dart';
import 'package:buildtool/task.dart';
import 'package:buildtool/src/utils.dart';
import 'package:logging/logging.dart';
Logger get _logger => new Logger('dart2js');
/** Adds a dart2js task to the build configuration. */
Dart2JSTask dart2js({String name: "dart2js", List<String> files}) =>
addTask(files, new Dart2JSTask(name));
Path get _dart2jsPath => new Path.fromNative(new Options().executable)
.directoryPath.append('dart2js');
/** Runs dart2js on the input files. */
class Dart2JSTask extends Task {
Path outDirectory;
Dart2JSTask(String name) : super(name);
Dart2JSTask.withOutDir(String name, this.outDirectory) : super(name);
Future<TaskResult> run(List<InputFile> files, Path outDir, Path genDir) {
if (outDirectory != null) {
outDir = outDirectory;
}
_logger.info("dart2js task starting. files: $files");
var futureGroup = new FutureGroup();
for (var file in files) {
var outPath = outDir.append('${file.path}.js');
var outFileDir = outPath.directoryPath;
new Directory.fromPath(outFileDir).createSync(recursive: true);
var options = new ProcessOptions()
..workingDirectory = new Directory.current().path;
var args = ['--out=$outPath', '--verbose', file.inputPath.toNativePath()];
_logger.fine("running $_dart2jsPath args: $args");
futureGroup.add(Process.run(_dart2jsPath.toNativePath(), args, options)
..transform((ProcessResult result) {
_logger.fine("dart2js exitCode: ${result.exitCode}");
return result;
})
..transformException((e) {
_logger.severe("error: $e");
throw e;
}));
}
return futureGroup.future.transform((_) {
_logger.info("dartjs tasks complete");
var messages = [];
var success = futureGroup.futures.every((f) => f.value.exitCode == 0);
for (var f in futureGroup.futures) {
ProcessResult r = f.value;
messages.add(r.stdout);
messages.add(r.stderr);
}
return new TaskResult(success, [], {}, messages);
});
}
}