Merge pull request #13 from dart-lang/use_platform_resolvedExecutable
introduce a getSdkPath() API
diff --git a/.gitignore b/.gitignore
index cc69f5b..76b960e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,5 +5,7 @@
.project
.settings
build/
+doc/api/
packages
-pubspec.lock
\ No newline at end of file
+pubspec.lock
+.packages
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0047f2..e4cd8cd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog
+## unreleased
+
+- Use the new `Platform.resolvedExecutable` API to locate the SDK
+
## 0.0.1+3
- Find SDK properly when invoked from inside SDK tests.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6f5e0ea..286d61c 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -23,7 +23,7 @@
### File headers
All files in the project must start with the following header.
- // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+ // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
diff --git a/README.md b/README.md
index bcff2f3..bf2dcf9 100644
--- a/README.md
+++ b/README.md
@@ -17,11 +17,11 @@
import 'package:path/path.dart' as path;
main(args) {
- // Get sdk dir from cli_util
- Directory sdkDir = getSdkDir(args);
+ // Get sdk dir from cli_util.
+ String sdkPath = getSdkPath();
// Do stuff... For example, print version string
- File versionFile = new File(path.join(sdkDir.path, 'version'));
+ File versionFile = new File(path.join(sdkPath, 'version'));
print(versionFile.readAsStringSync());
}
```
diff --git a/.analysis_options b/analysis_options.yaml
similarity index 100%
rename from .analysis_options
rename to analysis_options.yaml
diff --git a/lib/cli_util.dart b/lib/cli_util.dart
index 6533e9f..62191ec 100644
--- a/lib/cli_util.dart
+++ b/lib/cli_util.dart
@@ -6,11 +6,16 @@
import 'dart:io';
-import 'package:path/path.dart' as p;
-import 'package:which/which.dart';
+import 'package:path/path.dart' as path;
-/// Return the path to the current Dart SDK. This will return `null` if we are
-/// unable to locate the Dart SDK.
+/// Return the path to the current Dart SDK.
+///
+/// This first checks for an explicit SDK listed on the command-line
+/// (`--dart-sdk`). It then looks in any `DART_SDK` environment variable. Next,
+/// it looks relative to the Dart VM executable. Last, it uses the
+/// [Platform.resolvedExecutable] API.
+///
+/// Callers should generally prefer using the [getSdkPath] function.
Directory getSdkDir([List<String> cliArgs]) {
// Look for --dart-sdk on the command line.
if (cliArgs != null) {
@@ -40,36 +45,15 @@
// Handle the case where Platform.executable is a sibling of the SDK directory
// (this happens during internal testing).
sdkDirectory =
- new Directory(p.join(platformExecutable.parent.path, 'dart-sdk'));
+ new Directory(path.join(platformExecutable.parent.path, 'dart-sdk'));
if (_isSdkDir(sdkDirectory)) return sdkDirectory;
- // Try and locate the VM using 'which'.
- String executable = whichSync('dart', orElse: () => null);
-
- if (executable != null) {
- // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links.
- Link link = new Link(executable);
- if (link.existsSync()) {
- executable = link.resolveSymbolicLinksSync();
- }
-
- Link parentLink = new Link(p.dirname(executable));
- if (parentLink.existsSync()) {
- executable = p.join(
- parentLink.resolveSymbolicLinksSync(), p.basename(executable));
- }
-
- File dartVm = new File(executable);
- Directory dir = dartVm.parent.parent;
- if (_isSdkDir(dir)) return dir;
- }
-
- return null;
+ // Use `Platform.resolvedExecutable`.
+ return new Directory(getSdkPath());
}
-bool _isSdkDir(Directory dir) => _joinFile(dir, ['version']).existsSync();
+/// Return the path to the current Dart SDK.
+String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
-File _joinFile(Directory dir, List<String> files) {
- String pathFragment = files.join(Platform.pathSeparator);
- return new File("${dir.path}${Platform.pathSeparator}${pathFragment}");
-}
+bool _isSdkDir(Directory dir) =>
+ FileSystemEntity.isDirectorySync(path.join(dir.path, 'version'));
diff --git a/pubspec.yaml b/pubspec.yaml
index 560d428..1afebf3 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,12 +1,14 @@
name: cli_util
-version: 0.0.1+3
+version: 0.1.0-dev.1
author: Dart Team <misc@dartlang.org>
description: A library to help in building Dart command-line apps.
homepage: https://github.com/dart-lang/cli_util
+
environment:
- sdk: '>=1.0.0 <2.0.0'
+ sdk: '>=1.11.0 <2.0.0'
+
dependencies:
path: '>=1.0.0 <2.0.0'
- which: '>=0.1.2 <0.2.0'
+
dev_dependencies:
- unittest: '>=0.11.0 <0.12.0'
+ test: ^0.12.0
diff --git a/test/cli_util_test.dart b/test/cli_util_test.dart
index bf092e7..7cbdcf9 100644
--- a/test/cli_util_test.dart
+++ b/test/cli_util_test.dart
@@ -3,7 +3,9 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:cli_util/cli_util.dart';
-import 'package:unittest/unittest.dart';
+import 'package:test/test.dart';
+
+main() => defineTests();
void defineTests() {
group('getSdkDir', () {
@@ -11,14 +13,15 @@
expect(getSdkDir(['--dart-sdk', '/dart/sdk']).path, equals('/dart/sdk'));
expect(getSdkDir(['--dart-sdk=/dart/sdk']).path, equals('/dart/sdk'));
});
+
test('finds the SDK without cli args', () {
expect(getSdkDir(), isNotNull);
});
});
-}
-main() {
- groupSep = ' | ';
-
- defineTests();
+ group('getSdkPath', () {
+ test('sdkPath', () {
+ expect(getSdkPath(), isNotNull);
+ });
+ });
}
diff --git a/tool/travis.sh b/tool/travis.sh
index e8e10b7..0b0aef9 100755
--- a/tool/travis.sh
+++ b/tool/travis.sh
@@ -23,4 +23,4 @@
--retry 2 \
--exclude-test-files \
test/cli_util_test.dart
-fi
\ No newline at end of file
+fi