@@ -6,11 +6,16 @@ library cli_util;
66
77import 'dart:io' ;
88
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.
9+ import 'package:path/path.dart' as path;
10+
11+ /// Return the path to the current Dart SDK.
12+ ///
13+ /// This first checks for an explicit SDK listed on the command-line
14+ /// (`--dart-sdk` ). It then looks in any `DART_SDK` environment variable. Next,
15+ /// it looks relative to the Dart VM executable. Last, it uses the
16+ /// [Platform.resolvedExecutable] API.
17+ ///
18+ /// Callers should generally prefer using the [getSdkPath] function.
1419Directory getSdkDir ([List <String > cliArgs]) {
1520 // Look for --dart-sdk on the command line.
1621 if (cliArgs != null ) {
@@ -40,36 +45,15 @@ Directory getSdkDir([List<String> cliArgs]) {
4045 // Handle the case where Platform.executable is a sibling of the SDK directory
4146 // (this happens during internal testing).
4247 sdkDirectory =
43- new Directory (p .join (platformExecutable.parent.path, 'dart-sdk' ));
48+ new Directory (path .join (platformExecutable.parent.path, 'dart-sdk' ));
4449 if (_isSdkDir (sdkDirectory)) return sdkDirectory;
4550
46- // Try and locate the VM using 'which'.
47- String executable = whichSync ('dart' , orElse: () => null );
48-
49- if (executable != null ) {
50- // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links.
51- Link link = new Link (executable);
52- if (link.existsSync ()) {
53- executable = link.resolveSymbolicLinksSync ();
54- }
55-
56- Link parentLink = new Link (p.dirname (executable));
57- if (parentLink.existsSync ()) {
58- executable = p.join (
59- parentLink.resolveSymbolicLinksSync (), p.basename (executable));
60- }
61-
62- File dartVm = new File (executable);
63- Directory dir = dartVm.parent.parent;
64- if (_isSdkDir (dir)) return dir;
65- }
66-
67- return null ;
51+ // Use `Platform.resolvedExecutable`.
52+ return new Directory (getSdkPath ());
6853}
6954
70- bool _isSdkDir (Directory dir) => _joinFile (dir, ['version' ]).existsSync ();
55+ /// Return the path to the current Dart SDK.
56+ String getSdkPath () => path.dirname (path.dirname (Platform .resolvedExecutable));
7157
72- File _joinFile (Directory dir, List <String > files) {
73- String pathFragment = files.join (Platform .pathSeparator);
74- return new File ("${dir .path }${Platform .pathSeparator }${pathFragment }" );
75- }
58+ bool _isSdkDir (Directory dir) =>
59+ FileSystemEntity .isDirectorySync (path.join (dir.path, 'version' ));
0 commit comments