| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Utilities to return the Dart SDK location. |
| 5 library cli_util; | 6 library cli_util; |
| 6 | 7 |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 | 9 |
| 9 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as path; |
| 10 import 'package:which/which.dart'; | |
| 11 | 11 |
| 12 /// Return the path to the current Dart SDK. This will return `null` if we are | 12 import 'src/utils.dart'; |
| 13 /// unable to locate the Dart SDK. | 13 |
| 14 /// Return the path to the current Dart SDK. |
| 15 /// |
| 16 /// This first checks for an explicit SDK listed on the command-line |
| 17 /// (`--dart-sdk`). It then looks in any `DART_SDK` environment variable. Next, |
| 18 /// it looks relative to the Dart VM executable. Last, it uses the |
| 19 /// [Platform.resolvedExecutable] API. |
| 20 /// |
| 21 /// Callers should generally prefer using the [getSdkPath] function. |
| 22 @Deprecated('Clients should generally prefer getSdkPath()') |
| 14 Directory getSdkDir([List<String> cliArgs]) { | 23 Directory getSdkDir([List<String> cliArgs]) { |
| 15 // Look for --dart-sdk on the command line. | 24 // Look for --dart-sdk on the command line. |
| 16 if (cliArgs != null) { | 25 if (cliArgs != null) { |
| 17 int index = cliArgs.indexOf('--dart-sdk'); | 26 int index = cliArgs.indexOf('--dart-sdk'); |
| 18 | 27 |
| 19 if (index != -1 && (index + 1 < cliArgs.length)) { | 28 if (index != -1 && (index + 1 < cliArgs.length)) { |
| 20 return new Directory(cliArgs[index + 1]); | 29 return new Directory(cliArgs[index + 1]); |
| 21 } | 30 } |
| 22 | 31 |
| 23 for (String arg in cliArgs) { | 32 for (String arg in cliArgs) { |
| 24 if (arg.startsWith('--dart-sdk=')) { | 33 if (arg.startsWith('--dart-sdk=')) { |
| 25 return new Directory(arg.substring('--dart-sdk='.length)); | 34 return new Directory(arg.substring('--dart-sdk='.length)); |
| 26 } | 35 } |
| 27 } | 36 } |
| 28 } | 37 } |
| 29 | 38 |
| 30 // Look in env['DART_SDK'] | 39 // Look in env['DART_SDK'] |
| 31 if (Platform.environment['DART_SDK'] != null) { | 40 if (Platform.environment['DART_SDK'] != null) { |
| 32 return new Directory(Platform.environment['DART_SDK']); | 41 return new Directory(Platform.environment['DART_SDK']); |
| 33 } | 42 } |
| 34 | 43 |
| 35 // Look relative to the dart executable. | 44 // Look relative to the dart executable. |
| 36 Directory sdkDirectory = new File(Platform.executable).parent.parent; | 45 File platformExecutable = new File(Platform.executable); |
| 37 if (_isSdkDir(sdkDirectory)) return sdkDirectory; | 46 Directory sdkDirectory = platformExecutable.parent.parent; |
| 47 if (isSdkDir(sdkDirectory)) return sdkDirectory; |
| 38 | 48 |
| 39 // Try and locate the VM using 'which'. | 49 // Handle the case where Platform.executable is a sibling of the SDK directory |
| 40 String executable = whichSync('dart', orElse: () => null); | 50 // (this happens during internal testing). |
| 51 sdkDirectory = |
| 52 new Directory(path.join(platformExecutable.parent.path, 'dart-sdk')); |
| 53 if (isSdkDir(sdkDirectory)) return sdkDirectory; |
| 41 | 54 |
| 42 if (executable != null) { | 55 // Use `Platform.resolvedExecutable`. |
| 43 // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links. | 56 return new Directory(getSdkPath()); |
| 44 Link link = new Link(executable); | |
| 45 if (link.existsSync()) { | |
| 46 executable = link.resolveSymbolicLinksSync(); | |
| 47 } | |
| 48 | |
| 49 Link parentLink = new Link(p.dirname(executable)); | |
| 50 if (parentLink.existsSync()) { | |
| 51 executable = p.join( | |
| 52 parentLink.resolveSymbolicLinksSync(), p.basename(executable)); | |
| 53 } | |
| 54 | |
| 55 File dartVm = new File(executable); | |
| 56 Directory dir = dartVm.parent.parent; | |
| 57 if (_isSdkDir(dir)) return dir; | |
| 58 } | |
| 59 | |
| 60 return null; | |
| 61 } | 57 } |
| 62 | 58 |
| 63 bool _isSdkDir(Directory dir) => _joinFile(dir, ['version']).existsSync(); | 59 /// Return the path to the current Dart SDK. |
| 64 | 60 String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable)); |
| 65 File _joinFile(Directory dir, List<String> files) { | |
| 66 String pathFragment = files.join(Platform.pathSeparator); | |
| 67 return new File("${dir.path}${Platform.pathSeparator}${pathFragment}"); | |
| 68 } | |
| OLD | NEW |