OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:async_await/async_await.dart' as async_await; | |
7 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
8 | 9 |
9 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. | 10 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. |
10 /// | 11 /// |
11 /// This assumes this script is itself being run from within the repo. | 12 /// This assumes this script is itself being run from within the repo. |
12 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); | 13 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); |
13 | 14 |
14 /// The [sourceDir] as a URL, for use in import strings. | 15 /// The [sourceDir] as a URL, for use in import strings. |
15 final sourceUrl = p.toUri(sourceDir).toString(); | 16 final sourceUrl = p.toUri(sourceDir).toString(); |
16 | 17 |
17 /// The directory that compiler output should be written to. | 18 /// The directory that compiler output should be written to. |
18 String buildDir; | 19 String buildDir; |
19 | 20 |
21 /// `true` if any file failed to compile. | |
22 bool hadFailure = false; | |
23 | |
20 /// This runs the async/await compiler on all of the pub source code. | 24 /// This runs the async/await compiler on all of the pub source code. |
21 /// | 25 /// |
22 /// It reads from the repo and writes the compiled output into the given build | 26 /// It reads from the repo and writes the compiled output into the given build |
23 /// directory (using the same file names and relative layout). Does not | 27 /// directory (using the same file names and relative layout). Does not |
24 /// compile files that haven't changed since the last time they were compiled. | 28 /// compile files that haven't changed since the last time they were compiled. |
25 // TODO(rnystrom): Remove this when #104 is fixed. | 29 // TODO(rnystrom): Remove this when #104 is fixed. |
26 void main(List<String> arguments) { | 30 void main(List<String> arguments) { |
27 _validate(arguments.isNotEmpty, "Missing build directory."); | 31 _validate(arguments.isNotEmpty, "Missing build directory."); |
28 _validate(arguments.length <= 2, "Unexpected arguments."); | 32 _validate(arguments.length <= 2, "Unexpected arguments."); |
29 if (arguments.length == 2) { | 33 if (arguments.length == 2) { |
(...skipping 25 matching lines...) Expand all Loading... | |
55 var destFile = new File(destPath); | 59 var destFile = new File(destPath); |
56 if (!destFile.existsSync() || | 60 if (!destFile.existsSync() || |
57 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) { | 61 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) { |
58 compile(sourceFile.path, sourceFile.readAsStringSync(), destPath); | 62 compile(sourceFile.path, sourceFile.readAsStringSync(), destPath); |
59 numCompiled++; | 63 numCompiled++; |
60 if (!silent) print("Compiled ${sourceFile.path}."); | 64 if (!silent) print("Compiled ${sourceFile.path}."); |
61 } | 65 } |
62 } | 66 } |
63 | 67 |
64 if (!silent) print("Compiled $numCompiled out of $numFiles files."); | 68 if (!silent) print("Compiled $numCompiled out of $numFiles files."); |
69 | |
70 if (hadFailure) exit(1); | |
65 } | 71 } |
66 | 72 |
67 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); | 73 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); |
68 | 74 |
69 void compile(String sourcePath, String source, String destPath) { | 75 void compile(String sourcePath, String source, String destPath) { |
70 var destDir = new Directory(p.dirname(destPath)); | 76 var destDir = new Directory(p.dirname(destPath)); |
71 destDir.createSync(recursive: true); | 77 destDir.createSync(recursive: true); |
72 | 78 |
73 // TODO(rnystrom): Do real async/await transformation here! | 79 if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) { |
74 source = source.replaceAll("ASYNC!", ""); | 80 // Don't run the async compiler on the special "asset" source files. These |
81 // have preprocessor comments that get discarded by the compiler. | |
nweiz
2014/08/27 20:10:07
Maybe factor this into a function so you can short
Bob Nystrom
2014/08/27 21:35:31
Done.
| |
82 } else { | |
83 try { | |
84 source = async_await.compile(source); | |
85 } catch (ex) { | |
86 stderr.writeln("Async compile failed on $sourcePath:\n$ex"); | |
87 hadFailure = true; | |
88 | |
89 // If the async compile fails, delete the file so that we don't try to | |
90 // run the stale previous output and so that we try to recompile it later. | |
91 try { | |
92 new File(destPath).deleteSync(); | |
93 } on IOException catch (ex) { | |
94 // Do nothing. This may happen if two instances of the compiler are runn ing | |
nweiz
2014/08/27 20:10:07
Long lines.
Bob Nystrom
2014/08/27 21:35:32
Done.
| |
95 // concurrently and compile the same file. The second one to try to writ e | |
96 // the output may fail if the file is still open. Since they are produci ng | |
nweiz
2014/08/27 20:10:07
"write the output" -> "delete the file"
Bob Nystrom
2014/08/27 21:35:32
Done.
| |
97 // the same output anyway, just ignore it when the second one fails. | |
98 } | |
99 | |
100 return; | |
101 } | |
102 } | |
75 | 103 |
76 // Pub imports dart2js using relative imports that reach outside of pub's | 104 // Pub imports dart2js using relative imports that reach outside of pub's |
77 // source tree. Since the build directory is in a different location, we need | 105 // source tree. Since the build directory is in a different location, we need |
78 // to fix those to be valid relative imports from the build directory. | 106 // to fix those to be valid relative imports from the build directory. |
79 var compilerDir = p.url.join(sourceUrl, "../compiler"); | 107 var compilerDir = p.url.join(sourceUrl, "../compiler"); |
80 var relative = p.url.relative(compilerDir, from: destDir.path); | 108 var relative = p.url.relative(compilerDir, from: destDir.path); |
81 source = source.replaceAll(_compilerPattern, "import '$relative"); | 109 source = source.replaceAll(_compilerPattern, "import '$relative"); |
82 | 110 |
83 try { | 111 try { |
84 new File(destPath).writeAsStringSync(source); | 112 new File(destPath).writeAsStringSync(source); |
85 } on IOException catch (ex) { | 113 } on IOException catch (ex) { |
86 // Do nothing. This may happen if two instances of the compiler are running | 114 // Do nothing. This may happen if two instances of the compiler are running |
87 // concurrently and compile the same file. The second one to try to write | 115 // concurrently and compile the same file. The second one to try to write |
88 // the output may fail if the file is still open. Since they are producing | 116 // the output may fail if the file is still open. Since they are producing |
89 // the same output anyway, just ignore it when the second one fails. | 117 // the same output anyway, just ignore it when the second one fails. |
90 } | 118 } |
91 } | 119 } |
92 | 120 |
93 /// Validates command-line argument usage and exits with [message] if [valid] | 121 /// Validates command-line argument usage and exits with [message] if [valid] |
94 /// is `false`. | 122 /// is `false`. |
95 void _validate(bool valid, String message) { | 123 void _validate(bool valid, String message) { |
96 if (valid) return; | 124 if (valid) return; |
97 | 125 |
98 stderr.writeln(message); | 126 stderr.writeln(message); |
99 stderr.writeln(); | 127 stderr.writeln(); |
100 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); | 128 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); |
101 exit(64); | 129 exit(64); |
102 } | 130 } |
OLD | NEW |