Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: sdk/lib/_internal/pub/bin/async_compile.dart

Issue 514853003: Hook async/await compiler into pub and use it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise! Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/command/build.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/bin/async_compile.dart
diff --git a/sdk/lib/_internal/pub/bin/async_compile.dart b/sdk/lib/_internal/pub/bin/async_compile.dart
index 40280a674281cfce611cdd815cdfaa215ccb3827..c5c47de9fd925714f11f48f50ea9ae9fada9f53b 100644
--- a/sdk/lib/_internal/pub/bin/async_compile.dart
+++ b/sdk/lib/_internal/pub/bin/async_compile.dart
@@ -4,6 +4,7 @@
import 'dart:io';
+import 'package:async_await/async_await.dart' as async_await;
import 'package:path/path.dart' as p;
/// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo.
@@ -17,6 +18,9 @@ final sourceUrl = p.toUri(sourceDir).toString();
/// The directory that compiler output should be written to.
String buildDir;
+/// `true` if any file failed to compile.
+bool hadFailure = false;
+
/// This runs the async/await compiler on all of the pub source code.
///
/// It reads from the repo and writes the compiled output into the given build
@@ -55,41 +59,72 @@ void main(List<String> arguments) {
var destFile = new File(destPath);
if (!destFile.existsSync() ||
entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) {
- compile(sourceFile.path, sourceFile.readAsStringSync(), destPath);
+ _compile(sourceFile.path, sourceFile.readAsStringSync(), destPath);
numCompiled++;
if (!silent) print("Compiled ${sourceFile.path}.");
}
}
if (!silent) print("Compiled $numCompiled out of $numFiles files.");
+
+ if (hadFailure) exit(1);
}
final _compilerPattern = new RegExp(r"import '(\.\./)+compiler");
-void compile(String sourcePath, String source, String destPath) {
+void _compile(String sourcePath, String source, String destPath) {
var destDir = new Directory(p.dirname(destPath));
destDir.createSync(recursive: true);
- // TODO(rnystrom): Do real async/await transformation here!
- source = source.replaceAll("ASYNC!", "");
-
- // Pub imports dart2js using relative imports that reach outside of pub's
- // source tree. Since the build directory is in a different location, we need
- // to fix those to be valid relative imports from the build directory.
- var compilerDir = p.url.join(sourceUrl, "../compiler");
- var relative = p.url.relative(compilerDir, from: destDir.path);
- source = source.replaceAll(_compilerPattern, "import '$relative");
+ source = _translateAsyncAwait(sourcePath, source);
+ if (source != null) source = _fixDart2jsImports(sourcePath, source, destPath);
try {
- new File(destPath).writeAsStringSync(source);
+ if (source == null) {
+ // If the async compile fails, delete the file so that we don't try to
+ // run the stale previous output and so that we try to recompile it later.
+ _deleteFile(destPath);
+ } else {
+ new File(destPath).writeAsStringSync(source);
+ }
} on IOException catch (ex) {
// Do nothing. This may happen if two instances of the compiler are running
- // concurrently and compile the same file. The second one to try to write
- // the output may fail if the file is still open. Since they are producing
- // the same output anyway, just ignore it when the second one fails.
+ // concurrently and compile the same file. The second one may fail because
+ // the first is still working on it. Since they will end up producing the
+ // same output anyway, just ignore the failure.
+ }
+}
+
+/// Runs the async/await compiler on [source].
+///
+/// Returns the translated Dart code or `null` if the compiler failed.
+String _translateAsyncAwait(String sourcePath, String source) {
+ if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) {
+ // Don't run the async compiler on the special "asset" source files. These
+ // have preprocessor comments that get discarded by the compiler.
+ return source;
+ }
+
+ try {
+ return async_await.compile(source);
+ } catch (ex) {
+ stderr.writeln("Async compile failed on $sourcePath:\n$ex");
+ hadFailure = true;
+ return null;
}
}
+/// Fix relative imports to dart2js libraries.
+///
+/// Pub imports dart2js using relative imports that reach outside of pub's
+/// source tree. Since the build directory is in a different location, we need
+/// to fix those to be valid relative imports from the build directory.
+String _fixDart2jsImports(String sourcePath, String source, String destPath) {
+ var compilerDir = p.url.join(sourceUrl, "../compiler");
+ var relative = p.url.relative(compilerDir, from: p.dirname(destPath));
+ return source.replaceAll(_compilerPattern, "import '$relative");
+}
+
/// Validates command-line argument usage and exits with [message] if [valid]
/// is `false`.
void _validate(bool valid, String message) {
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/command/build.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698