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 stderr.writeln(ex); | 95 stderr.writeln(ex); |
65 stderr.writeln(); | 96 stderr.writeln(); |
66 stderr.writeln( | 97 stderr.writeln( |
67 "Usage: dart async_compile.dart [--verbose] [--force] <build dir>"); | 98 "Usage: dart async_compile.dart [--verbose] [--force] [--no-snapshot] " |
99 "<generated dir> <build dir>"); | |
Bob Nystrom
2014/12/02 19:15:50
Is the build dir only used for the snapshot now? I
nweiz
2014/12/02 21:14:13
Done.
| |
68 exit(64); | 100 exit(64); |
69 } | 101 } |
70 | 102 |
71 // 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 |
72 // 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 |
73 // compile the sources, recompile everything. | 105 // compile the sources, recompile everything. |
74 var currentCommit = _getCurrentCommit(); | 106 var currentCommit = _getCurrentCommit(); |
75 | 107 |
76 var readmePath = p.join(generatedDir, "README.md"); | 108 var readmePath = p.join(generatedDir, "README.md"); |
77 var lastCommit; | 109 var lastCommit; |
78 var readme = new File(readmePath).readAsStringSync(); | 110 try { |
79 var match = _commitPattern.firstMatch(readme); | 111 var readme = new File(readmePath).readAsStringSync(); |
80 if (match == null) { | 112 var match = _commitPattern.firstMatch(readme); |
81 stderr.writeln("Could not find compiler commit hash in README.md."); | 113 if (match == null) { |
82 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("Failed to load $readmePath: $error\n" | |
122 "${new Trace.from(stackTrace)}"); | |
123 } | |
83 } | 124 } |
84 | 125 |
85 lastCommit = match[0]; | |
86 | |
87 var numFiles = 0; | 126 var numFiles = 0; |
88 var numCompiled = 0; | 127 var numCompiled = 0; |
89 | 128 |
90 // Compile any modified or missing files. | 129 // Compile any modified or missing files. |
91 var sources = new Set(); | 130 var sources = new Set(); |
92 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { | 131 for (var entry in new Directory(sourceDir).listSync(recursive: true)) { |
93 if (p.extension(entry.path) != ".dart") continue; | 132 if (p.extension(entry.path) != ".dart") continue; |
94 | 133 |
95 numFiles++; | 134 numFiles++; |
96 var relative = p.relative(entry.path, from: sourceDir); | 135 var relative = p.relative(entry.path, from: sourceDir); |
(...skipping 19 matching lines...) Expand all Loading... | |
116 var relative = p.relative(entry.path, from: generatedDir); | 155 var relative = p.relative(entry.path, from: generatedDir); |
117 | 156 |
118 if (!sources.contains(relative)) { | 157 if (!sources.contains(relative)) { |
119 _deleteFile(entry.path); | 158 _deleteFile(entry.path); |
120 if (verbose) print("Deleted $relative"); | 159 if (verbose) print("Deleted $relative"); |
121 } | 160 } |
122 } | 161 } |
123 | 162 |
124 // Update the README. | 163 // Update the README. |
125 if (currentCommit != lastCommit) { | 164 if (currentCommit != lastCommit) { |
126 readme = readme.replaceAll(_commitPattern, currentCommit); | 165 _writeFile(readmePath, _README.replaceAll("<<COMMIT>>", currentCommit)); |
127 _writeFile(readmePath, readme); | |
128 if (verbose) print("Updated README.md"); | 166 if (verbose) print("Updated README.md"); |
129 } | 167 } |
130 | 168 |
131 if (numCompiled > 0) _generateSnapshot(buildDir); | 169 if (numCompiled > 0 && snapshot) _generateSnapshot(buildDir); |
132 | 170 |
133 if (verbose) print("Compiled $numCompiled out of $numFiles files"); | 171 if (verbose) print("Compiled $numCompiled out of $numFiles files"); |
134 | 172 |
135 if (hadFailure) exit(1); | 173 if (hadFailure) exit(1); |
136 } | 174 } |
137 | 175 |
138 String _getCurrentCommit() { | 176 String _getCurrentCommit() { |
139 var command = "git"; | 177 var command = "git"; |
140 var args = ["rev-parse", "HEAD"]; | 178 var args = ["rev-parse", "HEAD"]; |
141 | 179 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 var relative = p.url.relative(compilerDir, | 245 var relative = p.url.relative(compilerDir, |
208 from: p.url.dirname(p.toUri(destPath).toString())); | 246 from: p.url.dirname(p.toUri(destPath).toString())); |
209 return source.replaceAll(_compilerPattern, "import '$relative"); | 247 return source.replaceAll(_compilerPattern, "import '$relative"); |
210 } | 248 } |
211 | 249 |
212 /// Regenerate the pub snapshot from the async/await-compiled output. We do | 250 /// 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 | 251 /// this here since the tests need it and it's faster than doing a full SDK |
214 /// build. | 252 /// build. |
215 void _generateSnapshot(String buildDir) { | 253 void _generateSnapshot(String buildDir) { |
216 buildDir = p.normalize(buildDir); | 254 buildDir = p.normalize(buildDir); |
255 new Directory(dir).createSync(recursive: true); | |
217 | 256 |
218 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); | 257 var entrypoint = p.join(generatedDir, 'bin/pub.dart'); |
219 var packageRoot = p.join(buildDir, 'packages'); | 258 var packageRoot = p.join(buildDir, 'packages'); |
220 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); | 259 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot'); |
221 | 260 |
222 var result = Process.runSync(Platform.executable, [ | 261 var result = Process.runSync(Platform.executable, [ |
223 "--package-root=$packageRoot", | 262 "--package-root=$packageRoot", |
224 "--snapshot=$snapshot", | 263 "--snapshot=$snapshot", |
225 entrypoint | 264 entrypoint |
226 ]); | 265 ]); |
(...skipping 24 matching lines...) Expand all Loading... | |
251 /// | 290 /// |
252 /// This swallows errors to accommodate multiple compilers running concurrently. | 291 /// This swallows errors to accommodate multiple compilers running concurrently. |
253 /// 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. |
254 void _writeFile(String path, String contents) { | 293 void _writeFile(String path, String contents) { |
255 try { | 294 try { |
256 new File(path).writeAsStringSync(contents); | 295 new File(path).writeAsStringSync(contents); |
257 } on IOException catch (ex) { | 296 } on IOException catch (ex) { |
258 // Do nothing. | 297 // Do nothing. |
259 } | 298 } |
260 } | 299 } |
OLD | NEW |