| 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('dart2js'); | 5 #library('dart2js'); |
| 6 | 6 |
| 7 #import('dart:io'); | 7 #import('dart:io'); |
| 8 #import('dart:uri'); | 8 #import('dart:uri'); |
| 9 #import('dart:utf'); | 9 #import('dart:utf'); |
| 10 | 10 |
| 11 #import('../compiler.dart', prefix: 'api'); | 11 #import('../compiler.dart', prefix: 'api'); |
| 12 #import('colors.dart'); | 12 #import('colors.dart'); |
| 13 #import('source_file.dart'); | 13 #import('source_file.dart'); |
| 14 | 14 #import('filenames.dart'); |
| 15 String relativize(Uri base, Uri uri) { | 15 #import('util/uri_extras.dart'); |
| 16 if (base.scheme == 'file' && | |
| 17 base.scheme == uri.scheme && | |
| 18 base.userInfo == uri.userInfo && | |
| 19 base.domain == uri.domain && | |
| 20 base.port == uri.port && | |
| 21 uri.query == "" && uri.fragment == "") { | |
| 22 if (uri.path.startsWith(base.path)) { | |
| 23 return uri.path.substring(base.path.length); | |
| 24 } | |
| 25 List<String> uriParts = uri.path.split('/'); | |
| 26 List<String> baseParts = base.path.split('/'); | |
| 27 int common = 0; | |
| 28 int length = Math.min(uriParts.length, baseParts.length); | |
| 29 while (common < length && uriParts[common] == baseParts[common]) { | |
| 30 common++; | |
| 31 } | |
| 32 StringBuffer sb = new StringBuffer(); | |
| 33 for (int i = common + 1; i < baseParts.length; i++) { | |
| 34 sb.add('../'); | |
| 35 } | |
| 36 for (int i = common; i < uriParts.length - 1; i++) { | |
| 37 sb.add('${uriParts[i]}/'); | |
| 38 } | |
| 39 sb.add('${uriParts.last()}'); | |
| 40 return sb.toString(); | |
| 41 } | |
| 42 return uri.toString(); | |
| 43 } | |
| 44 | 16 |
| 45 void compile(List<String> argv) { | 17 void compile(List<String> argv) { |
| 46 Uri cwd = new Uri(scheme: 'file', path: getCurrentDirectory()); | 18 Uri cwd = getCurrentDirectory(); |
| 47 bool throwOnError = false; | 19 bool throwOnError = false; |
| 48 bool showWarnings = true; | 20 bool showWarnings = true; |
| 49 bool verbose = false; | 21 bool verbose = false; |
| 50 Uri libraryRoot = cwd; | 22 Uri libraryRoot = cwd; |
| 51 Uri out = cwd.resolve('out.js'); | 23 Uri out = cwd.resolve('out.js'); |
| 52 | 24 |
| 53 List<String> arguments = <String>[]; | 25 List<String> arguments = <String>[]; |
| 54 for (String argument in argv) { | 26 for (String argument in argv) { |
| 55 if ('--throw-on-error' == argument) { | 27 if ('--throw-on-error' == argument) { |
| 56 throwOnError = true; | 28 throwOnError = true; |
| 57 } else if ('--suppress-warnings' == argument) { | 29 } else if ('--suppress-warnings' == argument) { |
| 58 showWarnings = false; | 30 showWarnings = false; |
| 59 } else if ('--verbose' == argument) { | 31 } else if ('--verbose' == argument) { |
| 60 verbose = true; | 32 verbose = true; |
| 61 } else if (argument.startsWith('--library-root=')) { | 33 } else if (argument.startsWith('--library-root=')) { |
| 62 String path = argument.substring(argument.indexOf('=') + 1); | 34 String path = |
| 35 nativeToUriPath(argument.substring(argument.indexOf('=') + 1)); |
| 63 if (!path.endsWith("/")) path = "$path/"; | 36 if (!path.endsWith("/")) path = "$path/"; |
| 64 libraryRoot = cwd.resolve(path); | 37 libraryRoot = cwd.resolve(path); |
| 65 } else if (argument.startsWith('--out=')) { | 38 } else if (argument.startsWith('--out=')) { |
| 66 String path = argument.substring(argument.indexOf('=') + 1); | 39 String path = |
| 40 nativeToUriPath(argument.substring(argument.indexOf('=') + 1)); |
| 67 out = cwd.resolve(path); | 41 out = cwd.resolve(path); |
| 68 } else if (argument.startsWith('-')) { | 42 } else if (argument.startsWith('-')) { |
| 69 throw new AbortLeg('unknown option $argument'); | 43 throw new AbortLeg('unknown option $argument'); |
| 70 } else { | 44 } else { |
| 71 arguments.add(argument); | 45 arguments.add(nativeToUriPath(argument)); |
| 72 } | 46 } |
| 73 } | 47 } |
| 74 if (arguments.isEmpty()) { | 48 if (arguments.isEmpty()) { |
| 75 throw new AbortLeg('no files to compile'); | 49 throw new AbortLeg('no files to compile'); |
| 76 } | 50 } |
| 77 if (arguments.length > 1) { | 51 if (arguments.length > 1) { |
| 78 var extra = arguments.getRange(1, arguments.length - 1); | 52 var extra = arguments.getRange(1, arguments.length - 1); |
| 79 throw new AbortLeg('extra arguments: $extra'); | 53 throw new AbortLeg('extra arguments: $extra'); |
| 80 } | 54 } |
| 81 | 55 |
| 82 Map<String, SourceFile> sourceFiles = <SourceFile>{}; | 56 Map<String, SourceFile> sourceFiles = <SourceFile>{}; |
| 83 int dartBytesRead = 0; | 57 int dartBytesRead = 0; |
| 84 | 58 |
| 85 Future<String> provider(Uri uri) { | 59 Future<String> provider(Uri uri) { |
| 86 if (uri.scheme != 'file') { | 60 if (uri.scheme != 'file') { |
| 87 throw new IllegalArgumentException(uri); | 61 throw new IllegalArgumentException(uri); |
| 88 } | 62 } |
| 89 String source = readAll(uri.path); | 63 String source = readAll(uriPathToNative(uri.path)); |
| 90 dartBytesRead += source.length; | 64 dartBytesRead += source.length; |
| 91 sourceFiles[uri.toString()] = | 65 sourceFiles[uri.toString()] = |
| 92 new SourceFile(relativize(cwd, uri), source); | 66 new SourceFile(relativize(cwd, uri), source); |
| 93 Completer<String> completer = new Completer<String>(); | 67 Completer<String> completer = new Completer<String>(); |
| 94 completer.complete(source); | 68 completer.complete(source); |
| 95 return completer.future; | 69 return completer.future; |
| 96 } | 70 } |
| 97 | 71 |
| 98 void info(var message) { | 72 void info(var message) { |
| 99 if (verbose) print('${green("info:")} $message'); | 73 if (verbose) print('${green("info:")} $message'); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 class AbortLeg { | 104 class AbortLeg { |
| 131 final message; | 105 final message; |
| 132 AbortLeg(this.message); | 106 AbortLeg(this.message); |
| 133 toString() => 'Aborted due to --throw-on-error: $message'; | 107 toString() => 'Aborted due to --throw-on-error: $message'; |
| 134 } | 108 } |
| 135 | 109 |
| 136 void writeString(Uri uri, String text) { | 110 void writeString(Uri uri, String text) { |
| 137 if (uri.scheme != 'file') { | 111 if (uri.scheme != 'file') { |
| 138 throw new AbortLeg('unhandled scheme ${uri.scheme}'); | 112 throw new AbortLeg('unhandled scheme ${uri.scheme}'); |
| 139 } | 113 } |
| 140 var file = new File(uri.path).openSync(FileMode.WRITE); | 114 var file = new File(uriPathToNative(uri.path)).openSync(FileMode.WRITE); |
| 141 file.writeStringSync(text); | 115 file.writeStringSync(text); |
| 142 file.closeSync(); | 116 file.closeSync(); |
| 143 } | 117 } |
| 144 | 118 |
| 145 String readAll(String filename) { | 119 String readAll(String filename) { |
| 146 var file = (new File(filename)).openSync(FileMode.READ); | 120 var file = (new File(filename)).openSync(FileMode.READ); |
| 147 var length = file.lengthSync(); | 121 var length = file.lengthSync(); |
| 148 var buffer = new List<int>(length); | 122 var buffer = new List<int>(length); |
| 149 var bytes = file.readListSync(buffer, 0, length); | 123 var bytes = file.readListSync(buffer, 0, length); |
| 150 file.closeSync(); | 124 file.closeSync(); |
| 151 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); | 125 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); |
| 152 } | 126 } |
| 153 | |
| 154 String getCurrentDirectory() { | |
| 155 String dir = new File(".").fullPathSync(); | |
| 156 if (dir.endsWith("/")) return dir; | |
| 157 return "$dir/"; | |
| 158 } | |
| OLD | NEW |