| 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:convert' as convert; |
| 8 import 'dart:io' show Directory, File, Platform, Process; | 8 import 'dart:io' show Directory, File, Platform, Process; |
| 9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
| 10 | 10 |
| 11 | 11 |
| 12 /// Attempts to provide the current Dart SDK directory. | 12 /// Attempts to provide the current Dart SDK directory. |
| 13 /// | 13 /// |
| 14 /// This will return null if the SDK cannot be found |
| 15 /// |
| 14 /// Note that this may not be correct when executing outside of `pub`. | 16 /// Note that this may not be correct when executing outside of `pub`. |
| 15 String get dartSdkDirectory { | 17 String get dartSdkDirectory { |
| 18 |
| 19 bool isSdkDir(String dirname) => |
| 20 new File(path.join(dirname, 'lib', '_internal', 'libraries.dart')) |
| 21 .existsSync(); |
| 22 |
| 16 if (path.split(Platform.executable).length == 1) { | 23 if (path.split(Platform.executable).length == 1) { |
| 17 // TODO(blois): make this cross-platform. | 24 // TODO(blois): make this cross-platform. |
| 18 // HACK: A single part, hope it's on the path. | 25 // HACK: A single part, hope it's on the path. |
| 19 var result = Process.runSync('which', ['dart'], | 26 var result = Process.runSync('which', ['dart'], |
| 20 stdoutEncoding: convert.UTF8); | 27 stdoutEncoding: convert.UTF8); |
| 21 return path.dirname(path.dirname(result.stdout)); | 28 |
| 29 var sdkDir = path.dirname(path.dirname(result.stdout)); |
| 30 if (isSdkDir(sdkDir)) return sdkDir; |
| 22 } | 31 } |
| 23 var sdkDir = path.dirname(path.absolute(Platform.executable)); | 32 var dartDir = path.dirname(path.absolute(Platform.executable)); |
| 24 // If there's a sub-dir named dart-sdk then we're most likely executing from | 33 // If there's a sub-dir named dart-sdk then we're most likely executing from |
| 25 // a dart enlistment build directory. | 34 // a dart enlistment build directory. |
| 26 if (new Directory(path.join(sdkDir, 'dart-sdk')).existsSync()) { | 35 if (isSdkDir(path.join(dartDir, 'dart-sdk'))) { |
| 27 return path.join(sdkDir, 'dart-sdk'); | 36 return path.join(dartDir, 'dart-sdk'); |
| 28 } | 37 } |
| 29 // If we can find libraries.dart then it's the root of the SDK. | 38 // If we can find libraries.dart then it's the root of the SDK. |
| 30 if (new File(path.join(sdkDir, 'lib', '_internal', 'libraries.dart')) | 39 if (isSdkDir(dartDir)) return dartDir; |
| 31 .existsSync()) { | 40 |
| 32 return sdkDir; | 41 var parts = path.split(dartDir); |
| 42 // If the dart executable is within the sdk dir then get the root. |
| 43 if (parts.contains('dart-sdk')) { |
| 44 var dartSdkDir = path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); |
| 45 if (isSdkDir(dartSdkDir)) return dartSdkDir; |
| 33 } | 46 } |
| 34 | 47 |
| 35 var parts = path.split(sdkDir); | 48 return null; |
| 36 // If the dart executable is within the sdk dir then get the root. | |
| 37 if (parts.contains('dart-sdk')) { | |
| 38 return path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); | |
| 39 } | |
| 40 | |
| 41 return sdkDir; | |
| 42 } | 49 } |
| OLD | NEW |