| 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:args/args.dart'; | 7 import 'package:args/args.dart'; |
| 8 import 'package:analyzer/src/services/formatter_impl.dart'; | 8 import 'package:analyzer/src/services/formatter_impl.dart'; |
| 9 import 'package:async_await/async_await.dart' as async_await; | 9 import 'package:async_await/async_await.dart' as async_await; |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 10 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 11 | 12 |
| 12 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. | 13 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. |
| 13 /// | 14 /// |
| 14 /// This assumes this script is itself being run from within the repo. | 15 /// This assumes this script is itself being run from within the repo. |
| 15 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); | 16 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); |
| 16 | 17 |
| 17 /// The [sourceDir] as a URL, for use in import strings. | 18 /// The [sourceDir] as a URL, for use in import strings. |
| 18 final sourceUrl = p.toUri(sourceDir).toString(); | 19 final sourceUrl = p.toUri(sourceDir).toString(); |
| 19 | 20 |
| 20 /// The directory that compiler output should be written to. | 21 /// The directory that compiler output should be written to. |
| 21 final generatedDir = p.join(p.dirname(sourceDir), 'pub_generated'); | 22 String generatedDir; |
| 22 | 23 |
| 23 /// `true` if any file failed to compile. | 24 /// `true` if any file failed to compile. |
| 24 bool hadFailure = false; | 25 bool hadFailure = false; |
| 25 | 26 |
| 26 bool verbose = false; | 27 bool verbose = false; |
| 27 | 28 |
| 28 /// Prefix for imports in pub that import dart2js libraries. | 29 /// Prefix for imports in pub that import dart2js libraries. |
| 29 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); | 30 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); |
| 30 | 31 |
| 31 /// Matches the Git commit hash of the compiler stored in the README.md file. | 32 /// Matches the Git commit hash of the compiler stored in the README.md file. |
| 32 /// | 33 /// |
| 33 /// This is used both to find the current commit and replace it with the new | 34 /// This is used both to find the current commit and replace it with the new |
| 34 /// one. | 35 /// one. |
| 35 final _commitPattern = new RegExp(r"[a-f0-9]{40}"); | 36 final _commitPattern = new RegExp(r"[a-f0-9]{40}"); |
| 36 | 37 |
| 38 /// The template for the README that's added to the generated source. |
| 39 /// |
| 40 /// This is used to store the current commit of the async_await compiler. |
| 41 const _README = """ |
| 42 Pub is currently dogfooding the new Dart async/await syntax. Since the Dart VM |
| 43 doesn't natively support it yet, we are using the [async-await][] compiler |
| 44 package. |
| 45 |
| 46 [async-await]: https://github.com/dart-lang/async_await |
| 47 |
| 48 We run that to compile pub-using-await from sdk/lib/_internal/pub down to |
| 49 vanilla Dart code which is what you see here. To interoperate more easily with |
| 50 the rest of the repositry, we check in that generated code. |
| 51 |
| 52 When bug #104 is fixed, we can remove this entirely. |
| 53 |
| 54 The code here was compiled using the async-await compiler at commit: |
| 55 |
| 56 <<COMMIT>> |
| 57 |
| 58 (Note: this file is also parsed by a tool to update the above commit, so be |
| 59 careful not to reformat it.) |
| 60 """; |
| 61 |
| 37 /// This runs the async/await compiler on all of the pub source code. | 62 /// This runs the async/await compiler on all of the pub source code. |
| 38 /// | 63 /// |
| 39 /// It reads from the repo and writes the compiled output into the given build | 64 /// It reads from the repo and writes the compiled output into the given build |
| 40 /// directory (using the same file names and relative layout). Does not | 65 /// directory (using the same file names and relative layout). Does not |
| 41 /// compile files that haven't changed since the last time they were compiled. | 66 /// compile files that haven't changed since the last time they were compiled. |
| 42 // TODO(rnystrom): Remove this when #104 is fixed. | 67 // TODO(rnystrom): Remove this when #104 is fixed. |
| 43 void main(List<String> arguments) { | 68 void main(List<String> arguments) { |
| 44 var parser = new ArgParser(allowTrailingOptions: true); | 69 var parser = new ArgParser(allowTrailingOptions: true); |
| 45 | 70 |
| 46 parser.addFlag("verbose", callback: (value) => verbose = value); | 71 parser.addFlag("verbose", callback: (value) => verbose = value); |
| 47 | 72 |
| 48 var force = false; | 73 var force = false; |
| 49 parser.addFlag("force", callback: (value) => force = value); | 74 parser.addFlag("force", callback: (value) => force = value); |
| 50 | 75 |
| 76 var snapshot = true; |
| 77 parser.addFlag("snapshot", callback: (value) => snapshot = value); |
| 78 |
| 51 var buildDir; | 79 var buildDir; |
| 52 | 80 |
| 53 try { | 81 try { |
| 54 var rest = parser.parse(arguments).rest; | 82 var rest = parser.parse(arguments).rest; |
| 55 if (rest.isEmpty) { | 83 if (rest.isEmpty) { |
| 84 throw new FormatException('Missing generated and build directory.'); |
| 85 } else if (rest.length == 1) { |
| 56 throw new FormatException('Missing build directory.'); | 86 throw new FormatException('Missing build directory.'); |
| 57 } else if (rest.length > 1) { | 87 } else if (rest.length > 2) { |
| 58 throw new FormatException( | 88 throw new FormatException( |
| 59 'Unexpected arguments: ${rest.skip(1).join(" ")}.'); | 89 'Unexpected arguments: ${rest.skip(2).join(" ")}.'); |
| 60 } | 90 } |
| 61 | 91 |
| 62 buildDir = rest.first; | 92 generatedDir = rest[0]; |
| 93 buildDir = rest[1]; |
| 63 } on FormatException catch (ex) { | 94 } on FormatException catch (ex) { |
| 64 print(ex); | 95 stderr.writeln(ex); |
| 65 print(); | 96 stderr.writeln(); |
| 66 print("Usage: dart async_compile.dart [--verbose] [--force] <build dir>"); | 97 stderr.writeln( |
| 98 "Usage: dart async_compile.dart [--verbose] [--force] [--no-snapshot] " |
| 99 "<generated dir> <build dir>"); |
| 67 exit(64); | 100 exit(64); |
| 68 } | 101 } |
| 69 | 102 |
| 70 // See what version (i.e. Git commit) of the async-await compiler we | 103 // See what version (i.e. Git commit) of the async-await compiler we |
| 71 // currently have. If this is different from the version that was used to | 104 // currently have. If this is different from the version that was used to |
| 72 // compile the sources, recompile everything. | 105 // compile the sources, recompile everything. |
| 73 var currentCommit = _getCurrentCommit(); | 106 var currentCommit = _getCurrentCommit(); |
| 74 | 107 |
| 75 var readmePath = p.join(generatedDir, "README.md"); | 108 var readmePath = p.join(generatedDir, "README.md"); |
| 76 var lastCommit; | 109 var lastCommit; |
| 77 var readme = new File(readmePath).readAsStringSync(); | 110 try { |
| 78 var match = _commitPattern.firstMatch(readme); | 111 var readme = new File(readmePath).readAsStringSync(); |
| 79 if (match == null) { | 112 var match = _commitPattern.firstMatch(readme); |
| 80 print("Could not find compiler commit hash in README.md."); | 113 if (match == null) { |
| 81 exit(1); | 114 stderr.writeln("Could not find compiler commit hash in README.md."); |
| 115 exit(1); |
| 116 } |
| 117 |
| 118 lastCommit = match[0]; |
| 119 } on IOException catch (error, stackTrace) { |
| 120 if (verbose) { |
| 121 stderr.writeln( |
| 122 "Failed to load $readmePath: $error\n" "${new Trace.from(stackTrace)}"
); |
| 123 } |
| 82 } | 124 } |
| 83 | 125 |
| 84 lastCommit = match[0]; | |
| 85 | |
| 86 var numFiles = 0; | 126 var numFiles = 0; |
| 87 var numCompiled = 0; | 127 var numCompiled = 0; |
| 88 | 128 |
| 89 // Compile any modified or missing files. | 129 // Compile any modified or missing files. |
| 90 var sources = new Set(); | 130 var sources = new Set(); |
| 91 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { | 131 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { |
| 92 if (p.extension(entry.path) != ".dart") continue; | 132 if (p.extension(entry.path) != ".dart") continue; |
| 93 | 133 |
| 94 numFiles++; | 134 numFiles++; |
| 95 var relative = p.relative(entry.path, from: sourceDir); | 135 var relative = p.relative(entry.path, from: sourceDir); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 115 var relative = p.relative(entry.path, from: generatedDir); | 155 var relative = p.relative(entry.path, from: generatedDir); |
| 116 | 156 |
| 117 if (!sources.contains(relative)) { | 157 if (!sources.contains(relative)) { |
| 118 _deleteFile(entry.path); | 158 _deleteFile(entry.path); |
| 119 if (verbose) print("Deleted $relative"); | 159 if (verbose) print("Deleted $relative"); |
| 120 } | 160 } |
| 121 } | 161 } |
| 122 | 162 |
| 123 // Update the README. | 163 // Update the README. |
| 124 if (currentCommit != lastCommit) { | 164 if (currentCommit != lastCommit) { |
| 125 readme = readme.replaceAll(_commitPattern, currentCommit); | 165 _writeFile(readmePath, _README.replaceAll("<<COMMIT>>", currentCommit)); |
| 126 _writeFile(readmePath, readme); | |
| 127 if (verbose) print("Updated README.md"); | 166 if (verbose) print("Updated README.md"); |
| 128 } | 167 } |
| 129 | 168 |
| 130 if (numCompiled > 0) _generateSnapshot(buildDir); | 169 if (numCompiled > 0 && snapshot) _generateSnapshot(buildDir); |
| 131 | 170 |
| 132 if (verbose) print("Compiled $numCompiled out of $numFiles files"); | 171 if (verbose) print("Compiled $numCompiled out of $numFiles files"); |
| 133 | 172 |
| 134 if (hadFailure) exit(1); | 173 if (hadFailure) exit(1); |
| 135 } | 174 } |
| 136 | 175 |
| 137 String _getCurrentCommit() { | 176 String _getCurrentCommit() { |
| 138 var command = "git"; | 177 var command = "git"; |
| 139 var args = ["rev-parse", "HEAD"]; | 178 var args = ["rev-parse", "HEAD"]; |
| 140 | 179 |
| 141 // Spawning a process on Windows will not look for the executable in the | 180 // Spawning a process on Windows will not look for the executable in the |
| 142 // system path so spawn git through a shell to find it. | 181 // system path so spawn git through a shell to find it. |
| 143 if (Platform.operatingSystem == "windows") { | 182 if (Platform.operatingSystem == "windows") { |
| 144 command = "cmd"; | 183 command = "cmd"; |
| 145 args = ["/c", "git"]..addAll(args); | 184 args = ["/c", "git"]..addAll(args); |
| 146 } | 185 } |
| 147 | 186 |
| 148 var result = Process.runSync( | 187 var result = Process.runSync( |
| 149 command, | 188 command, |
| 150 args, | 189 args, |
| 151 workingDirectory: p.join(sourceDir, "../../../../third_party/pkg/async_awa
it")); | 190 workingDirectory: p.join(sourceDir, "../../../../third_party/pkg/async_awa
it")); |
| 152 if (result.exitCode != 0) { | 191 if (result.exitCode != 0) { |
| 153 print("Could not get Git revision of async_await compiler."); | 192 stderr.writeln("Could not get Git revision of async_await compiler."); |
| 154 exit(1); | 193 exit(1); |
| 155 } | 194 } |
| 156 | 195 |
| 157 return result.stdout.trim(); | 196 return result.stdout.trim(); |
| 158 } | 197 } |
| 159 | 198 |
| 160 void _compile(String sourcePath, String source, String destPath) { | 199 void _compile(String sourcePath, String source, String destPath) { |
| 161 var destDir = new Directory(p.dirname(destPath)); | 200 var destDir = new Directory(p.dirname(destPath)); |
| 162 destDir.createSync(recursive: true); | 201 destDir.createSync(recursive: true); |
| 163 | 202 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 185 | 224 |
| 186 try { | 225 try { |
| 187 source = async_await.compile(source); | 226 source = async_await.compile(source); |
| 188 | 227 |
| 189 // Reformat the result since the compiler ditches all whitespace. | 228 // Reformat the result since the compiler ditches all whitespace. |
| 190 // TODO(rnystrom): Remove when this is fixed: | 229 // TODO(rnystrom): Remove when this is fixed: |
| 191 // https://github.com/dart-lang/async_await/issues/12 | 230 // https://github.com/dart-lang/async_await/issues/12 |
| 192 var result = new CodeFormatter().format(CodeKind.COMPILATION_UNIT, source); | 231 var result = new CodeFormatter().format(CodeKind.COMPILATION_UNIT, source); |
| 193 return result.source; | 232 return result.source; |
| 194 } catch (ex) { | 233 } catch (ex) { |
| 195 print("Async compile failed on $sourcePath:\n$ex"); | 234 stderr.writeln("Async compile failed on $sourcePath:\n$ex"); |
| 196 hadFailure = true; | 235 hadFailure = true; |
| 197 return null; | 236 return null; |
| 198 } | 237 } |
| 199 } | 238 } |
| 200 | 239 |
| 201 /// Fix relative imports to dart2js libraries. | 240 /// Fix relative imports to dart2js libraries. |
| 202 /// | 241 /// |
| 203 /// Pub imports dart2js using relative imports that reach outside of pub's | 242 /// Pub imports dart2js using relative imports that reach outside of pub's |
| 204 /// source tree. Since the build directory is in a different location, we need | 243 /// source tree. Since the build directory is in a different location, we need |
| 205 /// to fix those to be valid relative imports from the build directory. | 244 /// to fix those to be valid relative imports from the build directory. |
| 206 String _fixDart2jsImports(String sourcePath, String source, String destPath) { | 245 String _fixDart2jsImports(String sourcePath, String source, String destPath) { |
| 207 var compilerDir = p.url.join(sourceUrl, "../compiler"); | 246 var compilerDir = p.url.join(sourceUrl, "../compiler"); |
| 208 var relative = | 247 var relative = |
| 209 p.url.relative(compilerDir, from: p.url.dirname(p.toUri(destPath).toString
())); | 248 p.url.relative(compilerDir, from: p.url.dirname(p.toUri(destPath).toString
())); |
| 210 return source.replaceAll(_compilerPattern, "import '$relative"); | 249 return source.replaceAll(_compilerPattern, "import '$relative"); |
| 211 } | 250 } |
| 212 | 251 |
| 213 /// Regenerate the pub snapshot from the async/await-compiled output. We do | 252 /// Regenerate the pub snapshot from the async/await-compiled output. We do |
| 214 /// this here since the tests need it and it's faster than doing a full SDK | 253 /// this here since the tests need it and it's faster than doing a full SDK |
| 215 /// build. | 254 /// build. |
| 216 void _generateSnapshot(String buildDir) { | 255 void _generateSnapshot(String buildDir) { |
| 217 buildDir = p.normalize(buildDir); | 256 buildDir = p.normalize(buildDir); |
| 257 new Directory(dir).createSync(recursive: true); |
| 218 | 258 |
| 219 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); | 259 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); |
| 220 var packageRoot = p.join(buildDir, 'packages'); | 260 var packageRoot = p.join(buildDir, 'packages'); |
| 221 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); | 261 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); |
| 222 | 262 |
| 223 var result = Process.runSync( | 263 var result = Process.runSync( |
| 224 Platform.executable, | 264 Platform.executable, |
| 225 ["--package-root=$packageRoot", "--snapshot=$snapshot", entrypoint]); | 265 ["--package-root=$packageRoot", "--snapshot=$snapshot", entrypoint]); |
| 226 | 266 |
| 227 if (result.exitCode != 0) { | 267 if (result.exitCode != 0) { |
| 228 print("Failed to generate snapshot:"); | 268 stderr.writeln("Failed to generate snapshot:"); |
| 229 if (result.stderr.trim().isNotEmpty) print(result.stderr); | 269 if (result.stderr.trim().isNotEmpty) stderr.writeln(result.stderr); |
| 230 if (result.stdout.trim().isNotEmpty) print(result.stdout); | 270 if (result.stdout.trim().isNotEmpty) stderr.writeln(result.stdout); |
| 231 exit(result.exitCode); | 271 exit(result.exitCode); |
| 232 } | 272 } |
| 233 | 273 |
| 234 if (verbose) print("Created pub snapshot"); | 274 if (verbose) print("Created pub snapshot"); |
| 235 } | 275 } |
| 236 | 276 |
| 237 /// Deletes the file at [path], ignoring any IO errors that occur. | 277 /// Deletes the file at [path], ignoring any IO errors that occur. |
| 238 /// | 278 /// |
| 239 /// This swallows errors to accommodate multiple compilers running concurrently. | 279 /// This swallows errors to accommodate multiple compilers running concurrently. |
| 240 /// Since they will produce the same output anyway, a failure of one is fine. | 280 /// Since they will produce the same output anyway, a failure of one is fine. |
| 241 void _deleteFile(String path) { | 281 void _deleteFile(String path) { |
| 242 try { | 282 try { |
| 243 new File(path).deleteSync(); | 283 new File(path).deleteSync(); |
| 244 } on IOException catch (ex) { | 284 } on IOException catch (ex) { |
| 245 // Do nothing. | 285 // Do nothing. |
| 246 } | 286 } |
| 247 } | 287 } |
| 248 | 288 |
| 249 /// Writes [contents] to [path], ignoring any IO errors that occur. | 289 /// Writes [contents] to [path], ignoring any IO errors that occur. |
| 250 /// | 290 /// |
| 251 /// This swallows errors to accommodate multiple compilers running concurrently. | 291 /// This swallows errors to accommodate multiple compilers running concurrently. |
| 252 /// Since they will produce the same output anyway, a failure of one is fine. | 292 /// Since they will produce the same output anyway, a failure of one is fine. |
| 253 void _writeFile(String path, String contents) { | 293 void _writeFile(String path, String contents) { |
| 254 try { | 294 try { |
| 255 new File(path).writeAsStringSync(contents); | 295 new File(path).writeAsStringSync(contents); |
| 256 } on IOException catch (ex) { | 296 } on IOException catch (ex) { |
| 257 // Do nothing. | 297 // Do nothing. |
| 258 } | 298 } |
| 259 } | 299 } |
| OLD | NEW |