OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Fletch 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.md file. |
| 4 |
| 5 library fletchc.compiler; |
| 6 |
| 7 import 'dart:io' show |
| 8 File, |
| 9 Platform; |
| 10 |
| 11 import 'package:compiler/compiler.dart' show |
| 12 CompilerInputProvider, |
| 13 CompilerOutputProvider, |
| 14 DiagnosticHandler; |
| 15 |
| 16 import 'package:dart2js_incremental/compiler.dart' show |
| 17 OutputProvider; |
| 18 |
| 19 import 'package:compiler/src/source_file_provider.dart' show |
| 20 CompilerSourceFileProvider, |
| 21 FormattingDiagnosticHandler, |
| 22 SourceFileProvider; |
| 23 |
| 24 import 'package:compiler/src/filenames.dart' show |
| 25 appendSlash; |
| 26 |
| 27 import 'package:compiler/src/apiimpl.dart' as apiimpl; |
| 28 |
| 29 import 'src/fletch_compiler.dart' as implementation; |
| 30 |
| 31 class FletchCompiler { |
| 32 final implementation.FletchCompiler _compiler; |
| 33 |
| 34 FletchCompiler._(this._compiler); |
| 35 |
| 36 factory FletchCompiler( |
| 37 {CompilerInputProvider provider, |
| 38 CompilerOutputProvider outputProvider, |
| 39 DiagnosticHandler handler, |
| 40 /* String or Uri */ libraryRoot, |
| 41 /* String or Uri */ packageRoot, |
| 42 List<String> options, |
| 43 Map<String, dynamic> environment}) { |
| 44 if (options == null) { |
| 45 options = <String>[]; |
| 46 } |
| 47 |
| 48 final bool isVerbose = apiimpl.Compiler.hasOption(options, '--verbose'); |
| 49 |
| 50 if (provider == null) { |
| 51 provider = new CompilerSourceFileProvider(); |
| 52 } |
| 53 |
| 54 if (handler == null) { |
| 55 SourceFileProvider sourceFileProvider = null; |
| 56 if (provider is SourceFileProvider) { |
| 57 sourceFileProvider = provider; |
| 58 } |
| 59 handler = new FormattingDiagnosticHandler(provider) |
| 60 ..throwOnError = false |
| 61 ..verbose = isVerbose; |
| 62 } |
| 63 |
| 64 if (outputProvider == null) { |
| 65 outputProvider = new OutputProvider(); |
| 66 } |
| 67 |
| 68 if (libraryRoot == null) { |
| 69 libraryRoot = _guessLibraryRoot(); |
| 70 if (libraryRoot == null) { |
| 71 throw new StateError("Unable to guess libraryRoot."); |
| 72 } |
| 73 } |
| 74 |
| 75 if (packageRoot == null) { |
| 76 packageRoot = Uri.base.resolve('packages/'); |
| 77 } |
| 78 |
| 79 |
| 80 if (environment == null) { |
| 81 environment = <String, dynamic>{}; |
| 82 } |
| 83 |
| 84 implementation.FletchCompiler compiler = new implementation.FletchCompiler( |
| 85 provider, |
| 86 outputProvider, |
| 87 handler, |
| 88 libraryRoot, |
| 89 packageRoot, |
| 90 options, |
| 91 environment); |
| 92 |
| 93 compiler.log("Using library root: $libraryRoot"); |
| 94 compiler.log("Using package root: $packageRoot"); |
| 95 |
| 96 return new FletchCompiler._(compiler); |
| 97 } |
| 98 |
| 99 void run(Uri script) { |
| 100 _compiler.run(script); |
| 101 } |
| 102 |
| 103 static Uri _guessLibraryRoot() { |
| 104 Uri guess = new Uri.file(Platform.executable).resolve('../'); |
| 105 if (_looksLikeLibraryRoot(guess)) { |
| 106 return _resolveSymbolicLinks(guess); |
| 107 } |
| 108 guess = guess.resolve('../sdk/'); |
| 109 if (_looksLikeLibraryRoot(guess)) { |
| 110 return _resolveSymbolicLinks(guess); |
| 111 } |
| 112 return null; |
| 113 } |
| 114 } |
| 115 |
| 116 /// Resolves any symbolic links in [uri] if its scheme is "file". Otherwise |
| 117 /// return the given [uri]. |
| 118 Uri _resolveSymbolicLinks(Uri uri) { |
| 119 if (uri.scheme != 'file') return uri; |
| 120 File apparentLocation = new File.fromUri(uri); |
| 121 String realLocation = apparentLocation.resolveSymbolicLinksSync(); |
| 122 if (uri.path.endsWith("/")) { |
| 123 realLocation = appendSlash(realLocation); |
| 124 } |
| 125 return new Uri.file(realLocation); |
| 126 } |
| 127 |
| 128 bool _looksLikeLibraryRoot(Uri uri) { |
| 129 String expectedFile = 'lib/_internal/libraries.dart'; |
| 130 return new File.fromUri(uri.resolve(expectedFile)).existsSync(); |
| 131 } |
OLD | NEW |