OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 |
| 9 /// The path to pub's root directory (sdk/lib/_internal/pub) in the Dart repo. |
| 10 /// |
| 11 /// This assumes this script is itself being run from within the repo. |
| 12 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script))); |
| 13 |
| 14 /// The [sourceDir] as a URL, for use in import strings. |
| 15 final sourceUrl = p.toUri(sourceDir).toString(); |
| 16 |
| 17 /// The directory that compiler output should be written to. |
| 18 String buildDir; |
| 19 |
| 20 /// This runs the async/await compiler on all of the pub source code. |
| 21 /// |
| 22 /// 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 |
| 24 /// compile files that haven't changed since the last time they were compiled. |
| 25 // TODO(rnystrom): Remove this when #104 is fixed. |
| 26 void main(List<String> arguments) { |
| 27 _validate(arguments.isNotEmpty, "Missing build directory."); |
| 28 _validate(arguments.length <= 2, "Unexpected arguments."); |
| 29 if (arguments.length == 2) { |
| 30 _validate(arguments[1] == "--silent", |
| 31 "Invalid argument '${arguments[1]}"); |
| 32 } |
| 33 |
| 34 // Create the build output directory if it's not already there. |
| 35 buildDir = p.join(p.normalize(arguments[0]), "pub_async"); |
| 36 new Directory(buildDir).createSync(recursive: true); |
| 37 |
| 38 var silent = arguments.length == 2 && arguments[1] == "--silent"; |
| 39 var numFiles = 0; |
| 40 var numCompiled = 0; |
| 41 |
| 42 // Compile any modified or missing files. |
| 43 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { |
| 44 if (p.extension(entry.path) != ".dart") continue; |
| 45 |
| 46 // Skip tests. |
| 47 // TODO(rnystrom): Do we want to use this for tests too? |
| 48 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue; |
| 49 |
| 50 numFiles++; |
| 51 var relative = p.relative(entry.path, from: sourceDir); |
| 52 |
| 53 var sourceFile = entry as File; |
| 54 var destPath = p.join(buildDir, relative); |
| 55 var destFile = new File(destPath); |
| 56 if (!destFile.existsSync() || |
| 57 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) { |
| 58 compile(sourceFile.path, sourceFile.readAsStringSync(), destPath); |
| 59 numCompiled++; |
| 60 if (!silent) print("Compiled ${sourceFile.path}."); |
| 61 } |
| 62 } |
| 63 |
| 64 if (!silent) print("Compiled $numCompiled out of $numFiles files."); |
| 65 } |
| 66 |
| 67 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler"); |
| 68 |
| 69 void compile(String sourcePath, String source, String destPath) { |
| 70 var destDir = new Directory(p.dirname(destPath)); |
| 71 destDir.createSync(recursive: true); |
| 72 |
| 73 // TODO(rnystrom): Do real async/await transformation here! |
| 74 source = source.replaceAll("ASYNC!", ""); |
| 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 |
| 83 try { |
| 84 new File(destPath).writeAsStringSync(source); |
| 85 } on IOException catch (ex) { |
| 86 // 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 |
| 88 // the output may fail if the file is still open. Since they are producing |
| 89 // the same output anyway, just ignore it when the second one fails. |
| 90 } |
| 91 } |
| 92 |
| 93 /// Validates command-line argument usage and exits with [message] if [valid] |
| 94 /// is `false`. |
| 95 void _validate(bool valid, String message) { |
| 96 if (valid) return; |
| 97 |
| 98 stderr.writeln(message); |
| 99 stderr.writeln(); |
| 100 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]"); |
| 101 exit(64); |
| 102 } |
OLD | NEW |