Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.2

- Fix a bug in `getSdkDir` (#21)

## 0.1.1

- Updated to the output for indeterminate progress
Expand Down
10 changes: 5 additions & 5 deletions lib/cli_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'dart:io';

import 'package:path/path.dart' as path;

import 'src/utils.dart';

/// Return the path to the current Dart SDK.
///
/// This first checks for an explicit SDK listed on the command-line
Expand All @@ -17,6 +19,7 @@ import 'package:path/path.dart' as path;
/// [Platform.resolvedExecutable] API.
///
/// Callers should generally prefer using the [getSdkPath] function.
@Deprecated('Clients should generally prefer getSdkPath()')
Directory getSdkDir([List<String> cliArgs]) {
// Look for --dart-sdk on the command line.
if (cliArgs != null) {
Expand All @@ -41,20 +44,17 @@ Directory getSdkDir([List<String> cliArgs]) {
// Look relative to the dart executable.
File platformExecutable = new File(Platform.executable);
Directory sdkDirectory = platformExecutable.parent.parent;
if (_isSdkDir(sdkDirectory)) return sdkDirectory;
if (isSdkDir(sdkDirectory)) return sdkDirectory;

// Handle the case where Platform.executable is a sibling of the SDK directory
// (this happens during internal testing).
sdkDirectory =
new Directory(path.join(platformExecutable.parent.path, 'dart-sdk'));
if (_isSdkDir(sdkDirectory)) return sdkDirectory;
if (isSdkDir(sdkDirectory)) return sdkDirectory;

// Use `Platform.resolvedExecutable`.
return new Directory(getSdkPath());
}

/// Return the path to the current Dart SDK.
String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));

bool _isSdkDir(Directory dir) =>
FileSystemEntity.isDirectorySync(path.join(dir.path, 'version'));
10 changes: 10 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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.

import 'dart:io';

import 'package:path/path.dart' as path;

bool isSdkDir(Directory dir) =>
FileSystemEntity.isFileSync(path.join(dir.path, 'version'));
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cli_util
version: 0.1.1
version: 0.1.2
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
Expand Down
12 changes: 12 additions & 0 deletions test/cli_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
// 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.

import 'dart:io';

import 'package:cli_util/cli_util.dart';
import 'package:cli_util/src/utils.dart';
import 'package:test/test.dart';

main() => defineTests();

void defineTests() {
group('getSdkDir', () {
test('arg parsing', () {
// ignore: deprecated_member_use
expect(getSdkDir(['--dart-sdk', '/dart/sdk']).path, equals('/dart/sdk'));
// ignore: deprecated_member_use
expect(getSdkDir(['--dart-sdk=/dart/sdk']).path, equals('/dart/sdk'));
});

test('finds the SDK without cli args', () {
// ignore: deprecated_member_use
expect(getSdkDir(), isNotNull);
});
});
Expand All @@ -24,4 +30,10 @@ void defineTests() {
expect(getSdkPath(), isNotNull);
});
});

group('utils', () {
test('isSdkDir', () {
expect(isSdkDir(new Directory(getSdkPath())), true);
});
});
}