| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library cli_util; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:path/path.dart' as p; | |
| 10 import 'package:which/which.dart'; | |
| 11 | |
| 12 /// Return the path to the current Dart SDK. This will return `null` if we are | |
| 13 /// unable to locate the Dart SDK. | |
| 14 Directory getSdkDir([List<String> cliArgs]) { | |
| 15 // Look for --dart-sdk on the command line. | |
| 16 if (cliArgs != null) { | |
| 17 int index = cliArgs.indexOf('--dart-sdk'); | |
| 18 | |
| 19 if (index != -1 && (index + 1 < cliArgs.length)) { | |
| 20 return new Directory(cliArgs[index + 1]); | |
| 21 } | |
| 22 | |
| 23 for (String arg in cliArgs) { | |
| 24 if (arg.startsWith('--dart-sdk=')) { | |
| 25 return new Directory(arg.substring('--dart-sdk='.length)); | |
| 26 } | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Look in env['DART_SDK'] | |
| 31 if (Platform.environment['DART_SDK'] != null) { | |
| 32 return new Directory(Platform.environment['DART_SDK']); | |
| 33 } | |
| 34 | |
| 35 // Look relative to the dart executable. | |
| 36 Directory sdkDirectory = new File(Platform.executable).parent.parent; | |
| 37 if (_isSdkDir(sdkDirectory)) return sdkDirectory; | |
| 38 | |
| 39 // Try and locate the VM using 'which'. | |
| 40 String executable = whichSync('dart', orElse: () => null); | |
| 41 | |
| 42 if (executable != null) { | |
| 43 // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links. | |
| 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 } | |
| 62 | |
| 63 bool _isSdkDir(Directory dir) => _joinFile(dir, ['version']).existsSync(); | |
| 64 | |
| 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 |