Index: dart/utils/compiler/dart2js.dart |
diff --git a/dart/utils/compiler/dart2js.dart b/dart/utils/compiler/dart2js.dart |
index 100f2eb33f8d855978937ff58a07d3e46431acd9..2481feae790334dad8562d119cf708bd9af78c93 100644 |
--- a/dart/utils/compiler/dart2js.dart |
+++ b/dart/utils/compiler/dart2js.dart |
@@ -2,12 +2,16 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-#library('frog_leg'); |
+#library('dart2js'); |
+ |
+#import('dart:io'); |
+#import('dart:utf'); |
#import('../../lib/uri/uri.dart'); |
-#import('../lang.dart', prefix: 'frog'); |
-#import('api.dart', prefix: 'api'); |
-#import('io/io.dart', prefix: 'io'); |
+#import('../../frog/leg/api.dart', prefix: 'api'); |
+#import('../../frog/leg/io/io.dart', prefix: 'io'); |
+#import('../../frog/leg/colors.dart'); |
+#import('source_file.dart'); |
String relativize(Uri base, Uri uri) { |
if (base.scheme == 'file' && |
@@ -39,53 +43,89 @@ String relativize(Uri base, Uri uri) { |
return uri.toString(); |
} |
-bool compile(frog.World world) { |
- final throwOnError = frog.options.throwOnErrors; |
- final showWarnings = frog.options.showWarnings; |
- // final compiler = new WorldCompiler(world, throwOnError); |
+void compile(List<String> argv) { |
Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory()); |
- Uri uri = cwd.resolve(frog.options.dartScript); |
- String frogLibDir = frog.options.libDir; |
- if (!frogLibDir.endsWith("/")) frogLibDir = "$frogLibDir/"; |
- Uri frogLib = new Uri(scheme: 'file', path: frogLibDir); |
- Uri libraryRoot = frogLib.resolve('../leg/lib/'); |
- Map<String, frog.SourceFile> sourceFiles = <frog.SourceFile>{}; |
+ bool throwOnError = false; |
+ bool showWarnings = true; |
+ bool verbose = false; |
+ Uri libraryRoot = cwd; |
+ Uri out = cwd.resolve('out.js'); |
+ |
+ List<String> arguments = <String>[]; |
+ for (String argument in argv) { |
+ if ('--throw-on-error' == argument) { |
+ throwOnError = true; |
+ } else if ('--suppress-warnings' == argument) { |
+ showWarnings = false; |
+ } else if ('--verbose' == argument) { |
+ verbose = true; |
+ } else if (argument.startsWith('--library-root=')) { |
+ String path = argument.substring(argument.indexOf('=') + 1); |
+ if (!path.endsWith("/")) path = "$path/"; |
+ libraryRoot = cwd.resolve(path); |
+ } else if (argument.startsWith('--out=')) { |
+ String path = argument.substring(argument.indexOf('=') + 1); |
+ out = cwd.resolve(path); |
+ } else if (argument.startsWith('-')) { |
+ throw new AbortLeg('unknown option $argument'); |
+ } else { |
+ arguments.add(argument); |
+ } |
+ } |
+ if (arguments.isEmpty()) { |
+ throw new AbortLeg('no files to compile'); |
+ } |
+ if (arguments.length > 1) { |
+ var extra = arguments.getRange(1, arguments.length - 1); |
+ throw new AbortLeg('extra arguments: $extra'); |
+ } |
+ |
+ Map<String, SourceFile> sourceFiles = <SourceFile>{}; |
+ int dartBytesRead = 0; |
Future<String> provider(Uri uri) { |
if (uri.scheme != 'file') { |
throw new IllegalArgumentException(uri); |
} |
- String source = world.files.readAll(uri.path); |
- world.dartBytesRead += source.length; |
+ String source = readAll(uri.path); |
+ dartBytesRead += source.length; |
sourceFiles[uri.toString()] = |
- new frog.SourceFile(relativize(cwd, uri), source); |
+ new SourceFile(relativize(cwd, uri), source); |
Completer<String> completer = new Completer<String>(); |
completer.complete(source); |
return completer.future; |
} |
+ void info(var message) { |
+ if (verbose) print('${green("info:")} $message'); |
+ } |
+ |
void handler(Uri uri, int begin, int end, String message, bool fatal) { |
if (uri === null && !fatal) { |
- world.info('[leg] $message'); |
+ info(message); |
return; |
} |
if (uri === null) { |
assert(fatal); |
print(message); |
} else if (fatal || showWarnings) { |
- frog.SourceFile file = sourceFiles[uri.toString()]; |
+ SourceFile file = sourceFiles[uri.toString()]; |
print(file.getLocationMessage(message, begin, end, true)); |
} |
if (fatal && throwOnError) throw new AbortLeg(message); |
} |
+ Uri uri = cwd.resolve(arguments[0]); |
+ info('compiling $uri'); |
+ |
// TODO(ahe): We expect the future to be complete and call value |
// directly. In effect, we don't support truly asynchronous API. |
String code = api.compile(uri, libraryRoot, provider, handler).value; |
- if (code === null) return false; |
- world.legCode = code; |
- world.jsBytesWritten = code.length; |
- return true; |
+ if (code === null) throw new AbortLeg('compilation failed'); |
+ writeString(out, code); |
+ int jsBytesWritten = code.length; |
+ info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' |
+ + 'in ${relativize(cwd, out)}'); |
} |
class AbortLeg { |
@@ -93,3 +133,21 @@ class AbortLeg { |
AbortLeg(this.message); |
toString() => 'Aborted due to --throw-on-error: $message'; |
} |
+ |
+void writeString(Uri uri, String text) { |
+ if (uri.scheme != 'file') { |
+ throw new AbortLeg('unhandled scheme ${uri.scheme}'); |
+ } |
+ var file = new File(uri.path).openSync(FileMode.WRITE); |
+ file.writeStringSync(text); |
+ file.closeSync(); |
+} |
+ |
+String readAll(String filename) { |
+ var file = (new File(filename)).openSync(); |
+ var length = file.lengthSync(); |
+ var buffer = new List<int>(length); |
+ var bytes = file.readListSync(buffer, 0, length); |
+ file.closeSync(); |
+ return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); |
+} |