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

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

Issue 472173004: Skeleton code for running the forthcoming async/await compiler on pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 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 // 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 directory that compiler output should be written to.
15 String buildDir;
16
17 /// This runs the async/await compiler on all of the pub source code.
18 ///
19 /// It reads from the repo and writes the compiled output into the given build
20 /// directory (using the same file names and relative layout). Does not
21 /// compile files that haven't changed since the last time they were compiled.
22 // TODO(rnystrom): Remove this when #104 is fixed.
23 void main(List<String> arguments) {
nweiz 2014/08/22 19:29:15 This needs to be more resilient to being run in pa
Bob Nystrom 2014/08/25 17:40:00 Done. It doesn't check the directory for existenc
nweiz 2014/08/25 19:40:56 This is probably sufficient to avoid crashes, but
24 _validate(arguments.isNotEmpty, "Missing build directory.");
25 _validate(arguments.length <= 2, "Unexpected arguments.");
26 if (arguments.length == 2) {
27 _validate(arguments[1] == "--silent",
28 "Invalid argument '${arguments[1]}");
nweiz 2014/08/22 19:29:15 Why not just use args? It would make this parsing
Bob Nystrom 2014/08/25 17:40:00 It felt like more trouble than it's worth. This sc
29 }
30
31 // Create the build output directory if not already there.
nweiz 2014/08/22 19:29:15 Nit: "if not" -> "if it's not"
Bob Nystrom 2014/08/25 17:40:00 Done.
32 buildDir = p.join(p.normalize(arguments[0]), "pub_async");
33 if (!new Directory(buildDir).existsSync()) {
34 new Directory(buildDir).createSync(recursive: true);
35 }
nweiz 2014/08/22 19:29:15 This script lives in pub; I'd just import pub's io
Bob Nystrom 2014/08/25 17:40:00 We'll probably want to use async syntax in io.dart
36
37 var silent = arguments.length == 2 && arguments[1] == "--silent";
38 var numFiles = 0;
39 var numCompiled = 0;
40
41 // Compile any modified or missing files.
42 for (var entry in new Directory(sourceDir).listSync(recursive: true)) {
43 if (p.extension(entry.path) != ".dart") continue;
44
45 // Skip tests.
46 // TODO(rnystrom): Do we want to use this for tests too?
47 if (p.isWithin(p.join(sourceDir, "test"), entry.path)) continue;
48
49 numFiles++;
50 var relative = p.relative(entry.path, from: sourceDir);
51
52 var sourceFile = entry as File;
53 var destPath = p.join(buildDir, relative);
54 var destFile = new File(destPath);
55 if (!destFile.existsSync() ||
56 entry.lastModifiedSync().isAfter(destFile.lastModifiedSync())) {
57 compile(sourceFile.path, sourceFile.readAsStringSync(), destPath);
58 numCompiled++;
59 if (!silent) print("Compiled ${sourceFile.path}");
nweiz 2014/08/22 19:29:15 Nit: add a period.
Bob Nystrom 2014/08/25 17:40:00 Done.
60 }
61 }
62
63 if (!silent) print("Compiled $numCompiled out of $numFiles files.");
64 }
65
66 final _compilerPattern = new RegExp(r'(\.\./)+compiler');
nweiz 2014/08/22 19:29:15 I'd feel a little better if this at least started
Bob Nystrom 2014/08/25 17:40:00 Done.
67
68 void compile(String sourcePath, String source, String destPath) {
69 var destDir = new Directory(p.dirname(destPath));
70 if (!destDir.existsSync()) destDir.createSync(recursive: true);
71
72 // TODO(rnystrom): Do real async/await transformation here!
73 source = source.replaceAll("ASYNC!", "");
74
75 // Pub imports dart2js using relative imports that reach outside of pub's
76 // source tree. Since the build directory is in a different location, we need
77 // to fix those to be valid relative imports from the build directory.
78 var compilerDir = p.join(sourceDir, "../compiler");
79 var relative = p.relative(compilerDir, from: destDir.path);
nweiz 2014/08/22 19:29:15 Use p.url for manipulating import paths.
Bob Nystrom 2014/08/25 17:40:00 Done.
80 source = source.replaceAll(_compilerPattern, relative);
81
82 new File(destPath).writeAsStringSync(source);
83 }
84
85 /// Validates command-line argument usage and exits with [message] if [valid]
86 /// is `false`.
87 void _validate(bool valid, String message) {
88 if (valid) return;
89
90 stderr.writeln(message);
91 stderr.writeln();
92 stderr.writeln("Usage: dart async_compile.dart <build dir> [--silent]");
93 exit(64);
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698