| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 code_transformers.src.dart_sdk; | 5 library code_transformers.src.dart_sdk; |
| 6 | 6 |
| 7 import 'dart:convert' as convert; | 7 import 'dart:io' show Directory; |
| 8 import 'dart:io' show File, Link, Platform, Process; | 8 |
| 9 import 'package:path/path.dart' as path; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 11 import 'package:analyzer/src/generated/java_io.dart'; | 10 import 'package:analyzer/src/generated/java_io.dart'; |
| 12 import 'package:analyzer/src/generated/sdk.dart'; | 11 import 'package:analyzer/src/generated/sdk.dart'; |
| 13 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 12 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:cli_util/cli_util.dart' as cli_util; |
| 15 | 15 |
| 16 | 16 |
| 17 /// Attempts to provide the current Dart SDK directory. | 17 /// Attempts to provide the current Dart SDK directory. |
| 18 /// | 18 /// |
| 19 /// This will return null if the SDK cannot be found | 19 /// This will return null if the SDK cannot be found |
| 20 /// | 20 /// |
| 21 /// Note that this may not be correct when executing outside of `pub`. | 21 /// Note that this may not be correct when executing outside of `pub`. |
| 22 String get dartSdkDirectory { | 22 String get dartSdkDirectory { |
| 23 | 23 Directory sdkDir = cli_util.getSdkDir(); |
| 24 bool isSdkDir(String dirname) => | 24 return sdkDir != null ? sdkDir.path : null; |
| 25 new File(path.join(dirname, 'lib', '_internal', 'libraries.dart')) | |
| 26 .existsSync(); | |
| 27 | |
| 28 String executable = Platform.executable; | |
| 29 if (path.split(executable).length == 1) { | |
| 30 // TODO(blois): make this cross-platform. | |
| 31 // HACK: A single part, hope it's on the path. | |
| 32 executable = Process.runSync('which', ['dart'], | |
| 33 stdoutEncoding: convert.UTF8).stdout.trim(); | |
| 34 // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links. | |
| 35 var link = new Link(executable); | |
| 36 if (link.existsSync()) { | |
| 37 executable = link.resolveSymbolicLinksSync(); | |
| 38 } | |
| 39 var sdkDir = path.dirname(path.dirname(executable)); | |
| 40 if (isSdkDir(sdkDir)) return sdkDir; | |
| 41 } | |
| 42 | |
| 43 var dartDir = path.dirname(path.absolute(executable)); | |
| 44 // If there's a sub-dir named dart-sdk then we're most likely executing from | |
| 45 // a dart enlistment build directory. | |
| 46 if (isSdkDir(path.join(dartDir, 'dart-sdk'))) { | |
| 47 return path.join(dartDir, 'dart-sdk'); | |
| 48 } | |
| 49 // If we can find libraries.dart then it's the root of the SDK. | |
| 50 if (isSdkDir(dartDir)) return dartDir; | |
| 51 | |
| 52 var parts = path.split(dartDir); | |
| 53 // If the dart executable is within the sdk dir then get the root. | |
| 54 if (parts.contains('dart-sdk')) { | |
| 55 var dartSdkDir = path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); | |
| 56 if (isSdkDir(dartSdkDir)) return dartSdkDir; | |
| 57 } | |
| 58 | |
| 59 return null; | |
| 60 } | 25 } |
| 61 | 26 |
| 62 /// Sources that are annotated with a source uri, so it is easy to resolve how | 27 /// Sources that are annotated with a source uri, so it is easy to resolve how |
| 63 /// to support `Resolver.getImportUri`. | 28 /// to support `Resolver.getImportUri`. |
| 64 abstract class UriAnnotatedSource extends Source { | 29 abstract class UriAnnotatedSource extends Source { |
| 65 Uri get uri; | 30 Uri get uri; |
| 66 } | 31 } |
| 67 | 32 |
| 68 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they | 33 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they |
| 69 /// are tracked with Uris. | 34 /// are tracked with Uris. |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 UriKind get uriKind => UriKind.DART_URI; | 194 UriKind get uriKind => UriKind.DART_URI; |
| 230 | 195 |
| 231 bool get isInSystemLibrary => true; | 196 bool get isInSystemLibrary => true; |
| 232 | 197 |
| 233 Source resolveRelative(Uri relativeUri) => | 198 Source resolveRelative(Uri relativeUri) => |
| 234 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | 199 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 235 | 200 |
| 236 Uri resolveRelativeUri(Uri relativeUri) => | 201 Uri resolveRelativeUri(Uri relativeUri) => |
| 237 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | 202 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 238 } | 203 } |
| OLD | NEW |