Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit d8f691a

Browse files
authored
Merge pull request #13 from dart-lang/use_platform_resolvedExecutable
introduce a getSdkPath() API
2 parents 1161b13 + 7fae620 commit d8f691a

File tree

9 files changed

+44
-49
lines changed

9 files changed

+44
-49
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
.project
66
.settings
77
build/
8+
doc/api/
89
packages
9-
pubspec.lock
10+
pubspec.lock
11+
.packages

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## unreleased
4+
5+
- Use the new `Platform.resolvedExecutable` API to locate the SDK
6+
37
## 0.0.1+3
48

59
- Find SDK properly when invoked from inside SDK tests.

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ All submissions, including submissions by project members, require review.
2323
### File headers
2424
All files in the project must start with the following header.
2525

26-
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
26+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2727
// for details. All rights reserved. Use of this source code is governed by a
2828
// BSD-style license that can be found in the LICENSE file.
2929

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import 'package:cli_util/cli_util.dart';
1717
import 'package:path/path.dart' as path;
1818
1919
main(args) {
20-
// Get sdk dir from cli_util
21-
Directory sdkDir = getSdkDir(args);
20+
// Get sdk dir from cli_util.
21+
String sdkPath = getSdkPath();
2222
2323
// Do stuff... For example, print version string
24-
File versionFile = new File(path.join(sdkDir.path, 'version'));
24+
File versionFile = new File(path.join(sdkPath, 'version'));
2525
print(versionFile.readAsStringSync());
2626
}
2727
```
File renamed without changes.

lib/cli_util.dart

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ library cli_util;
66

77
import '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.
1419
Directory 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'));

pubspec.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
name: cli_util
2-
version: 0.0.1+3
2+
version: 0.1.0-dev.1
33
author: Dart Team <[email protected]>
44
description: A library to help in building Dart command-line apps.
55
homepage: https://github.com/dart-lang/cli_util
6+
67
environment:
7-
sdk: '>=1.0.0 <2.0.0'
8+
sdk: '>=1.11.0 <2.0.0'
9+
810
dependencies:
911
path: '>=1.0.0 <2.0.0'
10-
which: '>=0.1.2 <0.2.0'
12+
1113
dev_dependencies:
12-
unittest: '>=0.11.0 <0.12.0'
14+
test: ^0.12.0

test/cli_util_test.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:cli_util/cli_util.dart';
6-
import 'package:unittest/unittest.dart';
6+
import 'package:test/test.dart';
7+
8+
main() => defineTests();
79

810
void defineTests() {
911
group('getSdkDir', () {
1012
test('arg parsing', () {
1113
expect(getSdkDir(['--dart-sdk', '/dart/sdk']).path, equals('/dart/sdk'));
1214
expect(getSdkDir(['--dart-sdk=/dart/sdk']).path, equals('/dart/sdk'));
1315
});
16+
1417
test('finds the SDK without cli args', () {
1518
expect(getSdkDir(), isNotNull);
1619
});
1720
});
18-
}
1921

20-
main() {
21-
groupSep = ' | ';
22-
23-
defineTests();
22+
group('getSdkPath', () {
23+
test('sdkPath', () {
24+
expect(getSdkPath(), isNotNull);
25+
});
26+
});
2427
}

tool/travis.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ if [ "$COVERALLS_TOKEN" ]; then
2323
--retry 2 \
2424
--exclude-test-files \
2525
test/cli_util_test.dart
26-
fi
26+
fi

0 commit comments

Comments
 (0)