Chromium Code Reviews| 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:async_await/async_await.dart' as async_await; |
| 8 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
| 9 | 9 |
| 10 /// A changing string that indicates the "version" or timestamp of the compiler | |
| 11 /// that the current sources were compiled against. | |
| 12 /// | |
| 13 /// Increment this whenever a meaningful change in the async/await compiler | |
| 14 /// itself is landed. Bumping this will force all previously compiled files | |
| 15 /// that were compiled against an older compiler to be recompiled. | |
| 16 const COMPILER_VERSION = "1"; | |
|
nweiz
2014/08/28 17:55:29
Can we scrape the DEPS file for this instead? Havi
Bob Nystrom
2014/08/28 18:24:26
No, because the DEPS file isn't in most people's c
nweiz
2014/08/28 18:25:51
In that case, at least add a comment to the DEPS l
| |
| 17 | |
| 10 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. | 18 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. |
| 11 /// | 19 /// |
| 12 /// This assumes this script is itself being run from within the repo. | 20 /// This assumes this script is itself being run from within the repo. |
| 13 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); | 21 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); |
| 14 | 22 |
| 15 /// The [sourceDir] as a URL, for use in import strings. | 23 /// The [sourceDir] as a URL, for use in import strings. |
| 16 final sourceUrl = p.toUri(sourceDir).toString(); | 24 final sourceUrl = p.toUri(sourceDir).toString(); |
| 17 | 25 |
| 18 /// The directory that compiler output should be written to. | 26 /// The directory that compiler output should be written to. |
| 19 String buildDir; | 27 String buildDir; |
| 20 | 28 |
| 21 /// `true` if any file failed to compile. | 29 /// `true` if any file failed to compile. |
| 22 bool hadFailure = false; | 30 bool hadFailure = false; |
| 23 | 31 |
| 32 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); | |
| 33 | |
| 24 /// This runs the async/await compiler on all of the pub source code. | 34 /// This runs the async/await compiler on all of the pub source code. |
| 25 /// | 35 /// |
| 26 /// It reads from the repo and writes the compiled output into the given build | 36 /// It reads from the repo and writes the compiled output into the given build |
| 27 /// directory (using the same file names and relative layout). Does not | 37 /// directory (using the same file names and relative layout). Does not |
| 28 /// compile files that haven't changed since the last time they were compiled. | 38 /// compile files that haven't changed since the last time they were compiled. |
| 29 // TODO(rnystrom): Remove this when #104 is fixed. | 39 // TODO(rnystrom): Remove this when #104 is fixed. |
| 30 void main(List<String> arguments) { | 40 void main(List<String> arguments) { |
| 31 _validate(arguments.isNotEmpty, "Missing build directory."); | 41 _validate(arguments.isNotEmpty, "Missing build directory."); |
| 32 _validate(arguments.length <= 2, "Unexpected arguments."); | 42 _validate(arguments.length <= 2, "Unexpected arguments."); |
| 33 if (arguments.length == 2) { | 43 if (arguments.length == 2) { |
| 34 _validate(arguments[1] == "--silent", | 44 _validate(arguments[1] == "--silent", |
| 35 "Invalid argument '${arguments[1]}"); | 45 "Invalid argument '${arguments[1]}"); |
| 36 } | 46 } |
| 37 | 47 |
| 38 // Create the build output directory if it's not already there. | 48 // Create the build output directory if it's not already there. |
| 39 buildDir = p.join(p.normalize(arguments[0]), "pub_async"); | 49 buildDir = p.join(p.normalize(arguments[0]), "pub_async"); |
| 40 new Directory(buildDir).createSync(recursive: true); | 50 new Directory(buildDir).createSync(recursive: true); |
| 41 | 51 |
| 52 // See if the current sources were compiled against a different version of the | |
| 53 // compiler. | |
| 54 var versionPath = p.join(buildDir, "compiler.version"); | |
| 55 var version = "none"; | |
| 56 try { | |
| 57 version = new File(versionPath).readAsStringSync(); | |
| 58 } on IOException catch (ex) { | |
| 59 // Do nothing. The version file didn't exist. | |
| 60 } | |
| 61 | |
| 42 var silent = arguments.length == 2 && arguments[1] == "--silent"; | 62 var silent = arguments.length == 2 && arguments[1] == "--silent"; |
| 43 var numFiles = 0; | 63 var numFiles = 0; |
| 44 var numCompiled = 0; | 64 var numCompiled = 0; |
| 45 | 65 |
| 46 // Compile any modified or missing files. | 66 // Compile any modified or missing files. |
| 47 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { | 67 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { |
| 48 if (p.extension(entry.path) != ".dart") continue; | 68 if (p.extension(entry.path) != ".dart") continue; |
| 49 | 69 |
| 50 // Skip tests. | 70 // Skip tests. |
| 51 // TODO(rnystrom): Do we want to use this for tests too? | 71 // TODO(rnystrom): Do we want to use this for tests too? |
| 52 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue; | 72 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue; |
| 53 | 73 |
| 54 numFiles++; | 74 numFiles++; |
| 55 var relative = p.relative(entry.path, from: sourceDir); | 75 var relative = p.relative(entry.path, from: sourceDir); |
| 56 | 76 |
| 57 var sourceFile = entry as File; | 77 var sourceFile = entry as File; |
| 58 var destPath = p.join(buildDir, relative); | 78 var destPath = p.join(buildDir, relative); |
| 59 var destFile = new File(destPath); | 79 var destFile = new File(destPath); |
| 60 if (!destFile.existsSync() || | 80 if (version != COMPILER_VERSION || |
| 81 !destFile.existsSync() || | |
| 61 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) { | 82 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) { |
| 62 _compile(sourceFile.path, sourceFile.readAsStringSync(), destPath); | 83 _compile(sourceFile.path, sourceFile.readAsStringSync(), destPath); |
| 63 numCompiled++; | 84 numCompiled++; |
| 64 if (!silent) print("Compiled ${sourceFile.path}."); | 85 if (!silent) print("Compiled ${sourceFile.path}."); |
| 65 } | 86 } |
| 66 } | 87 } |
| 67 | 88 |
| 89 _writeFile(versionPath, COMPILER_VERSION); | |
| 90 | |
| 68 if (!silent) print("Compiled $numCompiled out of $numFiles files."); | 91 if (!silent) print("Compiled $numCompiled out of $numFiles files."); |
| 69 | 92 |
| 70 if (hadFailure) exit(1); | 93 if (hadFailure) exit(1); |
| 71 } | 94 } |
| 72 | 95 |
| 73 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); | |
| 74 | |
| 75 void _compile(String sourcePath, String source, String destPath) { | 96 void _compile(String sourcePath, String source, String destPath) { |
| 76 var destDir = new Directory(p.dirname(destPath)); | 97 var destDir = new Directory(p.dirname(destPath)); |
| 77 destDir.createSync(recursive: true); | 98 destDir.createSync(recursive: true); |
| 78 | 99 |
| 79 source = _translateAsyncAwait(sourcePath, source); | 100 source = _translateAsyncAwait(sourcePath, source); |
| 80 if (source != null) source = _fixDart2jsImports(sourcePath, source, destPath); | 101 if (source != null) source = _fixDart2jsImports(sourcePath, source, destPath); |
| 81 | 102 |
| 82 try { | 103 if (source == null) { |
| 83 if (source == null) { | 104 // If the async compile fails, delete the file so that we don't try to |
| 84 // If the async compile fails, delete the file so that we don't try to | 105 // run the stale previous output and so that we try to recompile it later. |
| 85 // run the stale previous output and so that we try to recompile it later. | 106 _deleteFile(destPath); |
| 86 _deleteFile(destPath); | 107 } else { |
| 87 } else { | 108 _writeFile(destPath, source); |
| 88 new File(destPath).writeAsStringSync(source); | |
| 89 } | |
| 90 } on IOException catch (ex) { | |
| 91 // Do nothing. This may happen if two instances of the compiler are running | |
| 92 // concurrently and compile the same file. The second one may fail because | |
| 93 // the first is still working on it. Since they will end up producing the | |
| 94 // same output anyway, just ignore the failure. | |
| 95 } | 109 } |
| 96 } | 110 } |
| 97 | 111 |
| 98 /// Runs the async/await compiler on [source]. | 112 /// Runs the async/await compiler on [source]. |
| 99 /// | 113 /// |
| 100 /// Returns the translated Dart code or `null` if the compiler failed. | 114 /// Returns the translated Dart code or `null` if the compiler failed. |
| 101 String _translateAsyncAwait(String sourcePath, String source) { | 115 String _translateAsyncAwait(String sourcePath, String source) { |
| 102 if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) { | 116 if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) { |
| 103 // Don't run the async compiler on the special "asset" source files. These | 117 // Don't run the async compiler on the special "asset" source files. These |
| 104 // have preprocessor comments that get discarded by the compiler. | 118 // have preprocessor comments that get discarded by the compiler. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 128 /// Validates command-line argument usage and exits with [message] if [valid] | 142 /// Validates command-line argument usage and exits with [message] if [valid] |
| 129 /// is `false`. | 143 /// is `false`. |
| 130 void _validate(bool valid, String message) { | 144 void _validate(bool valid, String message) { |
| 131 if (valid) return; | 145 if (valid) return; |
| 132 | 146 |
| 133 stderr.writeln(message); | 147 stderr.writeln(message); |
| 134 stderr.writeln(); | 148 stderr.writeln(); |
| 135 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); | 149 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); |
| 136 exit(64); | 150 exit(64); |
| 137 } | 151 } |
| 152 | |
| 153 /// Deletes the file at [path], ignoring any IO errors that occur. | |
| 154 /// | |
| 155 /// This swallows errors to accommodate multiple compilers running concurrently. | |
| 156 /// Since they will produce the same output anyway, a failure of one is fine. | |
| 157 void _deleteFile(String path) { | |
| 158 try { | |
| 159 new File(path).deleteSync(); | |
| 160 } on IOException catch (ex) { | |
| 161 // Do nothing. | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 /// Writes [contents] to [path], ignoring any IO errors that occur. | |
| 166 /// | |
| 167 /// This swallows errors to accommodate multiple compilers running concurrently. | |
| 168 /// Since they will produce the same output anyway, a failure of one is fine. | |
| 169 void _writeFile(String path, String contents) { | |
| 170 try { | |
| 171 new File(path).writeAsStringSync(contents); | |
| 172 } on IOException catch (ex) { | |
| 173 // Do nothing. | |
| 174 } | |
| 175 } | |
| OLD | NEW |