Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: sdk/lib/_internal/pub_generated/bin/async_compile.dart

Issue 557563002: Store the async-await compiled pub code directly in the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 import 'dart:io';
2 import 'package:args/args.dart';
3 import 'package:analyzer/src/services/formatter_impl.dart';
4 import 'package:async_await/async_await.dart' as async_await;
5 import 'package:path/path.dart' as p;
6 final sourceDir = p.dirname(p.dirname(p.fromUri(Platform.script)));
7 final sourceUrl = p.toUri(sourceDir).toString();
8 final generatedDir = p.join(p.dirname(sourceDir), 'pub_generated');
9 bool hadFailure = false;
10 bool verbose = false;
11 final _compilerPattern = new RegExp(r"import '(\.\./)+compiler");
12 final _commitPattern = new RegExp(r"[a-f0-9]{40,40}");
13 void main(List<String> arguments) {
14 var parser = new ArgParser(allowTrailingOptions: true);
15 parser.addFlag("verbose", callback: (value) => verbose = value);
16 var force = false;
17 parser.addFlag("force", callback: (value) => force = value);
18 var buildDir;
19 try {
20 var rest = parser.parse(arguments).rest;
21 if (rest.isEmpty) {
22 throw new FormatException('Missing build directory.');
23 } else if (rest.length > 1) {
24 throw new FormatException(
25 'Unexpected arguments: ${rest.skip(1).join(" ")}.');
26 }
27 buildDir = rest.first;
28 } on FormatException catch (ex) {
29 stderr.writeln(ex);
30 stderr.writeln();
31 stderr.writeln(
32 "Usage: dart async_compile.dart [--verbose] [--force] <build dir>");
33 exit(64);
34 }
35 var result = Process.runSync(
36 "git",
37 ["rev-parse", "HEAD"],
38 workingDirectory: p.join(sourceDir, "../../../../third_party/pkg/async_awa it"));
39 if (result.exitCode != 0) {
40 stderr.writeln("Could not get Git revision of async_await compiler.");
41 exit(1);
42 }
43 var currentCommit = result.stdout.trim();
44 var readmePath = p.join(generatedDir, "README.md");
45 var lastCommit;
46 var readme = new File(readmePath).readAsStringSync();
47 var match = _commitPattern.firstMatch(readme);
48 if (match == null) {
49 stderr.writeln("Could not find compiler commit hash in README.md.");
50 exit(1);
51 }
52 lastCommit = match[0];
53 var numFiles = 0;
54 var numCompiled = 0;
55 for (var entry in new Directory(sourceDir).listSync(recursive: true)) {
56 if (p.extension(entry.path) != ".dart") continue;
57 numFiles++;
58 var relative = p.relative(entry.path, from: sourceDir);
59 var sourceFile = entry as File;
60 var destPath = p.join(generatedDir, relative);
61 var destFile = new File(destPath);
62 if (force ||
63 currentCommit != lastCommit ||
64 !destFile.existsSync() ||
65 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) {
66 _compile(sourceFile.path, sourceFile.readAsStringSync(), destPath);
67 numCompiled++;
68 if (verbose) print("Compiled $relative");
69 }
70 }
71 if (currentCommit != lastCommit) {
72 readme = readme.replaceAll(_commitPattern, currentCommit);
73 _writeFile(readmePath, readme);
74 }
75 if (verbose) print("Compiled $numCompiled out of $numFiles files");
76 if (numCompiled > 0) _generateSnapshot(buildDir);
77 if (hadFailure) exit(1);
78 }
79 void _compile(String sourcePath, String source, String destPath) {
80 var destDir = new Directory(p.dirname(destPath));
81 destDir.createSync(recursive: true);
82 source = _translateAsyncAwait(sourcePath, source);
83 if (source != null) source = _fixDart2jsImports(sourcePath, source, destPath);
84 if (source == null) {
85 _deleteFile(destPath);
86 } else {
87 _writeFile(destPath, source);
88 }
89 }
90 String _translateAsyncAwait(String sourcePath, String source) {
91 if (p.isWithin(p.join(sourceDir, "asset"), sourcePath)) {
92 return source;
93 }
94 try {
95 source = async_await.compile(source);
96 var result = new CodeFormatter().format(CodeKind.COMPILATION_UNIT, source);
97 return result.source;
98 } catch (ex) {
99 stderr.writeln("Async compile failed on $sourcePath:\n$ex");
100 hadFailure = true;
101 return null;
102 }
103 }
104 String _fixDart2jsImports(String sourcePath, String source, String destPath) {
105 var compilerDir = p.url.join(sourceUrl, "../compiler");
106 var relative = p.url.relative(compilerDir, from: p.dirname(destPath));
107 return source.replaceAll(_compilerPattern, "import '$relative");
108 }
109 void _generateSnapshot(String buildDir) {
110 buildDir = p.normalize(buildDir);
111 var entrypoint = p.join(generatedDir, 'bin/pub.dart');
112 var packageRoot = p.join(buildDir, 'packages');
113 var snapshot = p.join(buildDir, 'dart-sdk/bin/snapshots/pub.dart.snapshot');
114 var result = Process.runSync(
115 Platform.executable,
116 ["--package-root=$packageRoot", "--snapshot=$snapshot", entrypoint]);
117 if (result.exitCode != 0) {
118 stderr.writeln("Failed to generate snapshot:");
119 if (result.stderr.trim().isNotEmpty) stderr.writeln(result.stderr);
120 if (result.stdout.trim().isNotEmpty) stderr.writeln(result.stdout);
121 exit(result.exitCode);
122 }
123 if (verbose) print("Created pub snapshot");
124 }
125 void _deleteFile(String path) {
126 try {
127 new File(path).deleteSync();
128 } on IOException catch (ex) {}
129 }
130 void _writeFile(String path, String contents) {
131 try {
132 new File(path).writeAsStringSync(contents);
133 } on IOException catch (ex) {}
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698