| 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 |
| 51 var buildDir; | 76 var buildDir; |
| 77 parser.addOption("snapshot-build-dir", callback: (value) => buildDir = value); |
| 52 | 78 |
| 53 try { | 79 try { |
| 54 var rest = parser.parse(arguments).rest; | 80 var rest = parser.parse(arguments).rest; |
| 55 if (rest.isEmpty) { | 81 if (rest.isEmpty) { |
| 56 throw new FormatException('Missing build directory.'); | 82 throw new FormatException('Missing generated directory.'); |
| 57 } else if (rest.length > 1) { | 83 } else if (rest.length > 1) { |
| 58 throw new FormatException( | 84 throw new FormatException( |
| 59 'Unexpected arguments: ${rest.skip(1).join(" ")}.'); | 85 'Unexpected arguments: ${rest.skip(1).join(" ")}.'); |
| 60 } | 86 } |
| 61 | 87 |
| 62 buildDir = rest.first; | 88 generatedDir = rest.first; |
| 63 } on FormatException catch(ex) { | 89 } on FormatException catch(ex) { |
| 64 stderr.writeln(ex); | 90 stderr.writeln(ex); |
| 65 stderr.writeln(); | 91 stderr.writeln(); |
| 66 stderr.writeln( | 92 stderr.writeln( |
| 67 "Usage: dart async_compile.dart [--verbose] [--force] <build dir>"); | 93 "Usage: dart async_compile.dart [--verbose] [--force] " |
| 94 "[--snapshot-build-dir <dir>] <generated dir>"); |
| 68 exit(64); | 95 exit(64); |
| 69 } | 96 } |
| 70 | 97 |
| 71 // See what version (i.e. Git commit) of the async-await compiler we | 98 // See what version (i.e. Git commit) of the async-await compiler we |
| 72 // currently have. If this is different from the version that was used to | 99 // currently have. If this is different from the version that was used to |
| 73 // compile the sources, recompile everything. | 100 // compile the sources, recompile everything. |
| 74 var currentCommit = _getCurrentCommit(); | 101 var currentCommit = _getCurrentCommit(); |
| 75 | 102 |
| 76 var readmePath = p.join(generatedDir, "README.md"); | 103 var readmePath = p.join(generatedDir, "README.md"); |
| 77 var lastCommit; | 104 var lastCommit; |
| 78 var readme = new File(readmePath).readAsStringSync(); | 105 try { |
| 79 var match = _commitPattern.firstMatch(readme); | 106 var readme = new File(readmePath).readAsStringSync(); |
| 80 if (match == null) { | 107 var match = _commitPattern.firstMatch(readme); |
| 81 stderr.writeln("Could not find compiler commit hash in README.md."); | 108 if (match == null) { |
| 82 exit(1); | 109 stderr.writeln("Could not find compiler commit hash in README.md."); |
| 110 exit(1); |
| 111 } |
| 112 |
| 113 lastCommit = match[0]; |
| 114 } on IOException catch (error, stackTrace) { |
| 115 if (verbose) { |
| 116 stderr.writeln("Failed to load $readmePath: $error\n" |
| 117 "${new Trace.from(stackTrace)}"); |
| 118 } |
| 83 } | 119 } |
| 84 | 120 |
| 85 lastCommit = match[0]; | |
| 86 | |
| 87 var numFiles = 0; | 121 var numFiles = 0; |
| 88 var numCompiled = 0; | 122 var numCompiled = 0; |
| 89 | 123 |
| 90 // Compile any modified or missing files. | 124 // Compile any modified or missing files. |
| 91 var sources = new Set(); | 125 var sources = new Set(); |
| 92 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { | 126 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { |
| 93 if (p.extension(entry.path) != ".dart") continue; | 127 if (p.extension(entry.path) != ".dart") continue; |
| 94 | 128 |
| 95 numFiles++; | 129 numFiles++; |
| 96 var relative = p.relative(entry.path, from: sourceDir); | 130 var relative = p.relative(entry.path, from: sourceDir); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 116 var relative = p.relative(entry.path, from: generatedDir); | 150 var relative = p.relative(entry.path, from: generatedDir); |
| 117 | 151 |
| 118 if (!sources.contains(relative)) { | 152 if (!sources.contains(relative)) { |
| 119 _deleteFile(entry.path); | 153 _deleteFile(entry.path); |
| 120 if (verbose) print("Deleted $relative"); | 154 if (verbose) print("Deleted $relative"); |
| 121 } | 155 } |
| 122 } | 156 } |
| 123 | 157 |
| 124 // Update the README. | 158 // Update the README. |
| 125 if (currentCommit != lastCommit) { | 159 if (currentCommit != lastCommit) { |
| 126 readme = readme.replaceAll(_commitPattern, currentCommit); | 160 _writeFile(readmePath, _README.replaceAll("<<COMMIT>>", currentCommit)); |
| 127 _writeFile(readmePath, readme); | |
| 128 if (verbose) print("Updated README.md"); | 161 if (verbose) print("Updated README.md"); |
| 129 } | 162 } |
| 130 | 163 |
| 131 if (numCompiled > 0) _generateSnapshot(buildDir); | 164 if (numCompiled > 0 && buildDir != null) _generateSnapshot(buildDir); |
| 132 | 165 |
| 133 if (verbose) print("Compiled $numCompiled out of $numFiles files"); | 166 if (verbose) print("Compiled $numCompiled out of $numFiles files"); |
| 134 | 167 |
| 135 if (hadFailure) exit(1); | 168 if (hadFailure) exit(1); |
| 136 } | 169 } |
| 137 | 170 |
| 138 String _getCurrentCommit() { | 171 String _getCurrentCommit() { |
| 139 var command = "git"; | 172 var command = "git"; |
| 140 var args = ["rev-parse", "HEAD"]; | 173 var args = ["rev-parse", "HEAD"]; |
| 141 | 174 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 var relative = p.url.relative(compilerDir, | 240 var relative = p.url.relative(compilerDir, |
| 208 from: p.url.dirname(p.toUri(destPath).toString())); | 241 from: p.url.dirname(p.toUri(destPath).toString())); |
| 209 return source.replaceAll(_compilerPattern, "import '$relative"); | 242 return source.replaceAll(_compilerPattern, "import '$relative"); |
| 210 } | 243 } |
| 211 | 244 |
| 212 /// Regenerate the pub snapshot from the async/await-compiled output. We do | 245 /// Regenerate the pub snapshot from the async/await-compiled output. We do |
| 213 /// this here since the tests need it and it's faster than doing a full SDK | 246 /// this here since the tests need it and it's faster than doing a full SDK |
| 214 /// build. | 247 /// build. |
| 215 void _generateSnapshot(String buildDir) { | 248 void _generateSnapshot(String buildDir) { |
| 216 buildDir = p.normalize(buildDir); | 249 buildDir = p.normalize(buildDir); |
| 250 new Directory(dir).createSync(recursive: true); |
| 217 | 251 |
| 218 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); | 252 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); |
| 219 var packageRoot = p.join(buildDir, 'packages'); | 253 var packageRoot = p.join(buildDir, 'packages'); |
| 220 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); | 254 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); |
| 221 | 255 |
| 222 var result = Process.runSync(Platform.executable, [ | 256 var result = Process.runSync(Platform.executable, [ |
| 223 "--package-root=$packageRoot", | 257 "--package-root=$packageRoot", |
| 224 "--snapshot=$snapshot", | 258 "--snapshot=$snapshot", |
| 225 entrypoint | 259 entrypoint |
| 226 ]); | 260 ]); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 251 /// | 285 /// |
| 252 /// This swallows errors to accommodate multiple compilers running concurrently. | 286 /// This swallows errors to accommodate multiple compilers running concurrently. |
| 253 /// Since they will produce the same output anyway, a failure of one is fine. | 287 /// Since they will produce the same output anyway, a failure of one is fine. |
| 254 void _writeFile(String path, String contents) { | 288 void _writeFile(String path, String contents) { |
| 255 try { | 289 try { |
| 256 new File(path).writeAsStringSync(contents); | 290 new File(path).writeAsStringSync(contents); |
| 257 } on IOException catch (ex) { | 291 } on IOException catch (ex) { |
| 258 // Do nothing. | 292 // Do nothing. |
| 259 } | 293 } |
| 260 } | 294 } |
| OLD | NEW |