| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// A library for compiling Dart code and manipulating analyzer parse trees. | 5 /// A library for compiling Dart code and manipulating analyzer parse trees. |
| 6 library pub.dart; | 6 library pub.dart; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 | 10 |
| 11 import 'package:analyzer_experimental/analyzer.dart'; | 11 import 'package:analyzer_experimental/analyzer.dart'; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 import 'package:stack_trace/stack_trace.dart'; | 13 import 'package:stack_trace/stack_trace.dart'; |
| 14 import '../../../compiler/compiler.dart' as compiler; | 14 import '../../../compiler/compiler.dart' as compiler; |
| 15 import '../../../compiler/implementation/source_file_provider.dart' | 15 import '../../../compiler/implementation/source_file_provider.dart' |
| 16 show FormattingDiagnosticHandler, SourceFileProvider; | 16 show FormattingDiagnosticHandler, SourceFileProvider; |
| 17 import '../../../compiler/implementation/filenames.dart' | 17 import '../../../compiler/implementation/filenames.dart' |
| 18 show appendSlash; | 18 show appendSlash; |
| 19 | 19 |
| 20 import 'io.dart'; | 20 import 'io.dart'; |
| 21 import 'sdk.dart' as sdk; | 21 import 'sdk.dart' as sdk; |
| 22 import 'utils.dart'; | 22 import 'utils.dart'; |
| 23 | 23 |
| 24 /// Returns [entrypoint] compiled to JavaScript (or to Dart if [toDart] is | 24 /// Returns [entrypoint] compiled to JavaScript (or to Dart if [toDart] is |
| 25 /// true). | 25 /// true). |
| 26 /// | 26 /// |
| 27 /// By default, the package root is assumed to be adjacent to [entrypoint], but | 27 /// By default, the package root is assumed to be adjacent to [entrypoint], but |
| 28 /// if [packageRoot] is passed that will be used instead. | 28 /// if [packageRoot] is passed that will be used instead. If [provider] is |
| 29 /// omitted, uses a default [SourceFileProvider] that loads directly from the |
| 30 /// filesystem. |
| 29 Future<String> compile(String entrypoint, {String packageRoot, | 31 Future<String> compile(String entrypoint, {String packageRoot, |
| 30 bool toDart: false}) { | 32 bool toDart: false, SourceFileProvider provider}) { |
| 31 return new Future.sync(() { | 33 return new Future.sync(() { |
| 32 var provider = new SourceFileProvider(); | |
| 33 var options = <String>['--categories=Client,Server', '--minify']; | 34 var options = <String>['--categories=Client,Server', '--minify']; |
| 34 if (toDart) options.add('--output-type=dart'); | 35 if (toDart) options.add('--output-type=dart'); |
| 35 if (packageRoot == null) { | 36 if (packageRoot == null) { |
| 36 packageRoot = path.join(path.dirname(entrypoint), 'packages'); | 37 packageRoot = path.join(path.dirname(entrypoint), 'packages'); |
| 37 } | 38 } |
| 38 | 39 |
| 40 if (provider == null) { |
| 41 provider = new SourceFileProvider(); |
| 42 } |
| 43 |
| 39 return compiler.compile( | 44 return compiler.compile( |
| 40 path.toUri(entrypoint), | 45 path.toUri(entrypoint), |
| 41 path.toUri(appendSlash(_libPath)), | 46 path.toUri(appendSlash(_libPath)), |
| 42 path.toUri(appendSlash(packageRoot)), | 47 path.toUri(appendSlash(packageRoot)), |
| 43 provider.readStringFromUri, | 48 provider.readStringFromUri, |
| 44 new FormattingDiagnosticHandler(provider).diagnosticHandler, | 49 new FormattingDiagnosticHandler(provider).diagnosticHandler, |
| 45 options); | 50 options); |
| 46 }).then((result) { | 51 }).then((result) { |
| 47 if (result != null) return result; | 52 if (result != null) return result; |
| 48 throw new ApplicationException('Failed to compile "$entrypoint".'); | 53 throw new ApplicationException('Failed to compile "$entrypoint".'); |
| 49 }); | 54 }); |
| 50 } | 55 } |
| 51 | 56 |
| 52 /// Returns the path to the library directory. This corresponds to the "sdk" | 57 /// Returns the path to the directory containing the Dart core libraries. |
| 53 /// directory in the repo and to the root of the compiled SDK. | 58 /// |
| 59 /// This corresponds to the "sdk" directory in the repo and to the root of the |
| 60 /// compiled SDK. |
| 54 String get _libPath { | 61 String get _libPath { |
| 55 if (runningFromSdk) return sdk.rootDirectory; | 62 if (runningFromSdk) return sdk.rootDirectory; |
| 56 return path.join(repoRoot, 'sdk'); | 63 return path.join(repoRoot, 'sdk'); |
| 57 } | 64 } |
| 58 | 65 |
| 59 /// Returns whether [dart] looks like an entrypoint file. | 66 /// Returns whether [dart] looks like an entrypoint file. |
| 60 bool isEntrypoint(CompilationUnit dart) { | 67 bool isEntrypoint(CompilationUnit dart) { |
| 61 // TODO(nweiz): this misses the case where a Dart file doesn't contain main(), | 68 // TODO(nweiz): this misses the case where a Dart file doesn't contain main(), |
| 62 // but it parts in another file that does. | 69 // but it parts in another file that does. |
| 63 return dart.declarations.any((node) { | 70 return dart.declarations.any((node) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 if (stack == null) stack = getAttachedStackTrace(error); | 153 if (stack == null) stack = getAttachedStackTrace(error); |
| 147 return { | 154 return { |
| 148 'type': error.runtimeType.toString(), | 155 'type': error.runtimeType.toString(), |
| 149 'message': getErrorMessage(error), | 156 'message': getErrorMessage(error), |
| 150 'stack': stack == null ? null : stack.toString() | 157 'stack': stack == null ? null : stack.toString() |
| 151 }; | 158 }; |
| 152 } | 159 } |
| 153 | 160 |
| 154 String toString() => "$message\n$stackTrace"; | 161 String toString() => "$message\n$stackTrace"; |
| 155 } | 162 } |
| OLD | NEW |