OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #library('frog_leg'); | 5 #library('dart2js'); |
| 6 |
| 7 #import('dart:io'); |
| 8 #import('dart:utf'); |
6 | 9 |
7 #import('../../lib/uri/uri.dart'); | 10 #import('../../lib/uri/uri.dart'); |
8 #import('../lang.dart', prefix: 'frog'); | 11 #import('../../frog/leg/api.dart', prefix: 'api'); |
9 #import('api.dart', prefix: 'api'); | 12 #import('../../frog/leg/io/io.dart', prefix: 'io'); |
10 #import('io/io.dart', prefix: 'io'); | 13 #import('../../frog/leg/colors.dart'); |
| 14 #import('source_file.dart'); |
11 | 15 |
12 String relativize(Uri base, Uri uri) { | 16 String relativize(Uri base, Uri uri) { |
13 if (base.scheme == 'file' && | 17 if (base.scheme == 'file' && |
14 base.scheme == uri.scheme && | 18 base.scheme == uri.scheme && |
15 base.userInfo == uri.userInfo && | 19 base.userInfo == uri.userInfo && |
16 base.domain == uri.domain && | 20 base.domain == uri.domain && |
17 base.port == uri.port && | 21 base.port == uri.port && |
18 uri.query == "" && uri.fragment == "") { | 22 uri.query == "" && uri.fragment == "") { |
19 if (uri.path.startsWith(base.path)) { | 23 if (uri.path.startsWith(base.path)) { |
20 return uri.path.substring(base.path.length); | 24 return uri.path.substring(base.path.length); |
(...skipping 11 matching lines...) Expand all Loading... |
32 } | 36 } |
33 for (int i = common; i < uriParts.length - 1; i++) { | 37 for (int i = common; i < uriParts.length - 1; i++) { |
34 sb.add('${uriParts[i]}/'); | 38 sb.add('${uriParts[i]}/'); |
35 } | 39 } |
36 sb.add('${uriParts.last()}'); | 40 sb.add('${uriParts.last()}'); |
37 return sb.toString(); | 41 return sb.toString(); |
38 } | 42 } |
39 return uri.toString(); | 43 return uri.toString(); |
40 } | 44 } |
41 | 45 |
42 bool compile(frog.World world) { | 46 void compile(List<String> argv) { |
43 final throwOnError = frog.options.throwOnErrors; | |
44 final showWarnings = frog.options.showWarnings; | |
45 // final compiler = new WorldCompiler(world, throwOnError); | |
46 Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory()); | 47 Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory()); |
47 Uri uri = cwd.resolve(frog.options.dartScript); | 48 bool throwOnError = false; |
48 String frogLibDir = frog.options.libDir; | 49 bool showWarnings = true; |
49 if (!frogLibDir.endsWith("/")) frogLibDir = "$frogLibDir/"; | 50 bool verbose = false; |
50 Uri frogLib = new Uri(scheme: 'file', path: frogLibDir); | 51 Uri libraryRoot = cwd; |
51 Uri libraryRoot = frogLib.resolve('../leg/lib/'); | 52 Uri out = cwd.resolve('out.js'); |
52 Map<String, frog.SourceFile> sourceFiles = <frog.SourceFile>{}; | 53 |
| 54 List<String> arguments = <String>[]; |
| 55 for (String argument in argv) { |
| 56 if ('--throw-on-error' == argument) { |
| 57 throwOnError = true; |
| 58 } else if ('--suppress-warnings' == argument) { |
| 59 showWarnings = false; |
| 60 } else if ('--verbose' == argument) { |
| 61 verbose = true; |
| 62 } else if (argument.startsWith('--library-root=')) { |
| 63 String path = argument.substring(argument.indexOf('=') + 1); |
| 64 if (!path.endsWith("/")) path = "$path/"; |
| 65 libraryRoot = cwd.resolve(path); |
| 66 } else if (argument.startsWith('--out=')) { |
| 67 String path = argument.substring(argument.indexOf('=') + 1); |
| 68 out = cwd.resolve(path); |
| 69 } else if (argument.startsWith('-')) { |
| 70 throw new AbortLeg('unknown option $argument'); |
| 71 } else { |
| 72 arguments.add(argument); |
| 73 } |
| 74 } |
| 75 if (arguments.isEmpty()) { |
| 76 throw new AbortLeg('no files to compile'); |
| 77 } |
| 78 if (arguments.length > 1) { |
| 79 var extra = arguments.getRange(1, arguments.length - 1); |
| 80 throw new AbortLeg('extra arguments: $extra'); |
| 81 } |
| 82 |
| 83 Map<String, SourceFile> sourceFiles = <SourceFile>{}; |
| 84 int dartBytesRead = 0; |
53 | 85 |
54 Future<String> provider(Uri uri) { | 86 Future<String> provider(Uri uri) { |
55 if (uri.scheme != 'file') { | 87 if (uri.scheme != 'file') { |
56 throw new IllegalArgumentException(uri); | 88 throw new IllegalArgumentException(uri); |
57 } | 89 } |
58 String source = world.files.readAll(uri.path); | 90 String source = readAll(uri.path); |
59 world.dartBytesRead += source.length; | 91 dartBytesRead += source.length; |
60 sourceFiles[uri.toString()] = | 92 sourceFiles[uri.toString()] = |
61 new frog.SourceFile(relativize(cwd, uri), source); | 93 new SourceFile(relativize(cwd, uri), source); |
62 Completer<String> completer = new Completer<String>(); | 94 Completer<String> completer = new Completer<String>(); |
63 completer.complete(source); | 95 completer.complete(source); |
64 return completer.future; | 96 return completer.future; |
65 } | 97 } |
66 | 98 |
| 99 void info(var message) { |
| 100 if (verbose) print('${green("info:")} $message'); |
| 101 } |
| 102 |
67 void handler(Uri uri, int begin, int end, String message, bool fatal) { | 103 void handler(Uri uri, int begin, int end, String message, bool fatal) { |
68 if (uri === null && !fatal) { | 104 if (uri === null && !fatal) { |
69 world.info('[leg] $message'); | 105 info(message); |
70 return; | 106 return; |
71 } | 107 } |
72 if (uri === null) { | 108 if (uri === null) { |
73 assert(fatal); | 109 assert(fatal); |
74 print(message); | 110 print(message); |
75 } else if (fatal || showWarnings) { | 111 } else if (fatal || showWarnings) { |
76 frog.SourceFile file = sourceFiles[uri.toString()]; | 112 SourceFile file = sourceFiles[uri.toString()]; |
77 print(file.getLocationMessage(message, begin, end, true)); | 113 print(file.getLocationMessage(message, begin, end, true)); |
78 } | 114 } |
79 if (fatal && throwOnError) throw new AbortLeg(message); | 115 if (fatal && throwOnError) throw new AbortLeg(message); |
80 } | 116 } |
81 | 117 |
| 118 Uri uri = cwd.resolve(arguments[0]); |
| 119 info('compiling $uri'); |
| 120 |
82 // TODO(ahe): We expect the future to be complete and call value | 121 // TODO(ahe): We expect the future to be complete and call value |
83 // directly. In effect, we don't support truly asynchronous API. | 122 // directly. In effect, we don't support truly asynchronous API. |
84 String code = api.compile(uri, libraryRoot, provider, handler).value; | 123 String code = api.compile(uri, libraryRoot, provider, handler).value; |
85 if (code === null) return false; | 124 if (code === null) throw new AbortLeg('compilation failed'); |
86 world.legCode = code; | 125 writeString(out, code); |
87 world.jsBytesWritten = code.length; | 126 int jsBytesWritten = code.length; |
88 return true; | 127 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' |
| 128 + 'in ${relativize(cwd, out)}'); |
89 } | 129 } |
90 | 130 |
91 class AbortLeg { | 131 class AbortLeg { |
92 final message; | 132 final message; |
93 AbortLeg(this.message); | 133 AbortLeg(this.message); |
94 toString() => 'Aborted due to --throw-on-error: $message'; | 134 toString() => 'Aborted due to --throw-on-error: $message'; |
95 } | 135 } |
| 136 |
| 137 void writeString(Uri uri, String text) { |
| 138 if (uri.scheme != 'file') { |
| 139 throw new AbortLeg('unhandled scheme ${uri.scheme}'); |
| 140 } |
| 141 var file = new File(uri.path).openSync(FileMode.WRITE); |
| 142 file.writeStringSync(text); |
| 143 file.closeSync(); |
| 144 } |
| 145 |
| 146 String readAll(String filename) { |
| 147 var file = (new File(filename)).openSync(); |
| 148 var length = file.lengthSync(); |
| 149 var buffer = new List<int>(length); |
| 150 var bytes = file.readListSync(buffer, 0, length); |
| 151 file.closeSync(); |
| 152 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); |
| 153 } |
OLD | NEW |