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

Side by Side 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, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/command/build.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 18 matching lines...) Expand all
48 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue; 52 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue;
49 53
50 numFiles++; 54 numFiles++;
51 var relative = p.relative(entry.path, from: sourceDir); 55 var relative = p.relative(entry.path, from: sourceDir);
52 56
53 var sourceFile = entry as File; 57 var sourceFile = entry as File;
54 var destPath = p.join(buildDir, relative); 58 var destPath = p.join(buildDir, relative);
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 source = _translateAsyncAwait(sourcePath, source);
74 source = source.replaceAll("ASYNC!", ""); 80 if (source != null) source = _fixDart2jsImports(sourcePath, source, destPath);
75
76 // 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
78 // to fix those to be valid relative imports from the build directory.
79 var compilerDir = p.url.join(sourceUrl, "../compiler");
80 var relative = p.url.relative(compilerDir, from: destDir.path);
81 source = source.replaceAll(_compilerPattern, "import '$relative");
82 81
83 try { 82 try {
84 new File(destPath).writeAsStringSync(source); 83 if (source == null) {
84 // If the async compile fails, delete the file so that we don't try to
85 // run the stale previous output and so that we try to recompile it later.
86 _deleteFile(destPath);
87 } else {
88 new File(destPath).writeAsStringSync(source);
89 }
85 } on IOException catch (ex) { 90 } on IOException catch (ex) {
86 // Do nothing. This may happen if two instances of the compiler are running 91 // 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 92 // concurrently and compile the same file. The second one may fail because
88 // the output may fail if the file is still open. Since they are producing 93 // the first is still working on it. Since they will end up producing the
89 // the same output anyway, just ignore it when the second one fails. 94 // same output anyway, just ignore the failure.
90 } 95 }
91 } 96 }
92 97
98 /// Runs the async/await compiler on [source].
99 ///
100 /// Returns the translated Dart code or `null` if the compiler failed.
101 String _translateAsyncAwait(String sourcePath, String source) {
102 if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) {
103 // Don't run the async compiler on the special "asset" source files. These
104 // have preprocessor comments that get discarded by the compiler.
105 return source;
106 }
107
108 try {
109 return async_await.compile(source);
110 } catch (ex) {
111 stderr.writeln("Async compile failed on $sourcePath:\n$ex");
112 hadFailure = true;
113 return null;
114 }
115 }
116
117 /// Fix relative imports to dart2js libraries.
118 ///
119 /// Pub imports dart2js using relative imports that reach outside of pub's
120 /// source tree. Since the build directory is in a different location, we need
121 /// to fix those to be valid relative imports from the build directory.
122 String _fixDart2jsImports(String sourcePath, String source, String destPath) {
123 var compilerDir = p.url.join(sourceUrl, "../compiler");
124 var relative = p.url.relative(compilerDir, from: p.dirname(destPath));
125 return source.replaceAll(_compilerPattern, "import '$relative");
126 }
127
93 /// Validates command-line argument usage and exits with [message] if [valid] 128 /// Validates command-line argument usage and exits with [message] if [valid]
94 /// is `false`. 129 /// is `false`.
95 void _validate(bool valid, String message) { 130 void _validate(bool valid, String message) {
96 if (valid) return; 131 if (valid) return;
97 132
98 stderr.writeln(message); 133 stderr.writeln(message);
99 stderr.writeln(); 134 stderr.writeln();
100 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); 135 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]");
101 exit(64); 136 exit(64);
102 } 137 }
OLDNEW
« 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