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

Commit e222c56

Browse files
authored
Make sdkPath a getter (#100)
Add `sdkPath` getter and deprecate `getSdkPath` function. Update tests to not use deprecated name. Clean up environment access, make all uses of exception pass only the environment entry name. Increment version to 0.4.2-wip
1 parent 12cd216 commit e222c56

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.2-wip
2+
3+
- Add `sdkPath` getter, deprecate `getSdkPath` function.
4+
15
## 0.4.1
26

37
- Fix a broken link in the readme.

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A package to help in building Dart command-line apps.
77
## What's this?
88

99
`package:cli_util` provides:
10-
- utilities to find the Dart SDK directory (`getSdkPath()`)
10+
- utilities to find the Dart SDK directory (`sdkPath`)
1111
- utilities to find the settings directory for a tool (`applicationConfigHome()`)
1212
- utilities to aid in showing rich CLI output and progress information (`cli_logging.dart`)
1313

@@ -20,11 +20,11 @@ import 'package:cli_util/cli_util.dart';
2020
import 'package:path/path.dart' as path;
2121
2222
main(args) {
23-
// Get sdk dir from cli_util.
24-
var sdkPath = getSdkPath();
23+
// Get SDK directory from cli_util.
24+
var sdkDir = sdkPath;
2525
2626
// Do stuff... For example, print version string
27-
var versionFile = File(path.join(sdkPath, 'version'));
27+
var versionFile = File(path.join(sdkDir, 'version'));
2828
print(versionFile.readAsStringSync());
2929
}
3030
```

lib/cli_util.dart

+27-29
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Utilities to return the Dart SDK location.
5+
/// Utilities to locate the Dart SDK.
66
library cli_util;
77

88
import 'dart:async';
99
import 'dart:io';
1010

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

13-
/// Return the path to the current Dart SDK.
14-
String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
13+
/// The path to the current Dart SDK.
14+
String get sdkPath => path.dirname(path.dirname(Platform.resolvedExecutable));
1515

16-
/// Get the user-specific application configuration folder for the current
17-
/// platform.
16+
/// Returns the path to the current Dart SDK.
17+
@Deprecated("Use 'sdkPath' instead")
18+
String getSdkPath() => sdkPath;
19+
20+
/// The user-specific application configuration folder for the current platform.
1821
///
1922
/// This is a location appropriate for storing application specific
2023
/// configuration for the current user. The [productName] should be unique to
@@ -29,12 +32,12 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
2932
/// (if `$XDG_CONFIG_HOME` is defined), and,
3033
/// * `$HOME/.config/<productName>` otherwise.
3134
///
32-
/// This aims follows best practices for each platform, honoring the
33-
/// [XDG Base Directory Specification][1] on Linux and [File System Basics][2]
34-
/// on Mac OS.
35+
/// The chosen location aims to follow best practices for each platform,
36+
/// honoring the [XDG Base Directory Specification][1] on Linux and
37+
/// [File System Basics][2] on Mac OS.
3538
///
36-
/// Throws an [EnvironmentNotFoundException] if `%APPDATA%` or `$HOME` is needed
37-
/// but undefined.
39+
/// Throws an [EnvironmentNotFoundException] if an environment entry,
40+
/// `%APPDATA%` or `$HOME`, is needed and not available.
3841
///
3942
/// [1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
4043
/// [2]: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1
@@ -43,16 +46,11 @@ String applicationConfigHome(String productName) =>
4346

4447
String get _configHome {
4548
if (Platform.isWindows) {
46-
final appdata = _env['APPDATA'];
47-
if (appdata == null) {
48-
throw EnvironmentNotFoundException(
49-
'Environment variable %APPDATA% is not defined!');
50-
}
51-
return appdata;
49+
return _requireEnv('APPDATA');
5250
}
5351

5452
if (Platform.isMacOS) {
55-
return path.join(_home, 'Library', 'Application Support');
53+
return path.join(_requireEnv('HOME'), 'Library', 'Application Support');
5654
}
5755

5856
if (Platform.isLinux) {
@@ -62,26 +60,26 @@ String get _configHome {
6260
}
6361
// XDG Base Directory Specification says to use $HOME/.config/ when
6462
// $XDG_CONFIG_HOME isn't defined.
65-
return path.join(_home, '.config');
63+
return path.join(_requireEnv('HOME'), '.config');
6664
}
6765

6866
// We have no guidelines, perhaps we should just do: $HOME/.config/
6967
// same as XDG specification would specify as fallback.
70-
return path.join(_home, '.config');
68+
return path.join(_requireEnv('HOME'), '.config');
7169
}
7270

73-
String get _home {
74-
final home = _env['HOME'];
75-
if (home == null) {
76-
throw EnvironmentNotFoundException(
77-
r'Environment variable $HOME is not defined!');
78-
}
79-
return home;
80-
}
71+
String _requireEnv(String name) =>
72+
_env[name] ?? (throw EnvironmentNotFoundException(name));
8173

74+
/// Exception thrown if a required environment entry does not exist.
75+
///
76+
/// Thrown by [applicationConfigHome] if an expected and required
77+
/// platform specific environment entry is not available.
8278
class EnvironmentNotFoundException implements Exception {
83-
final String message;
84-
EnvironmentNotFoundException(this.message);
79+
/// Name of environment entry which was needed, but not found.
80+
final String entryName;
81+
String get message => 'Environment variable \'$entryName\' is not defined!';
82+
EnvironmentNotFoundException(this.entryName);
8583
@override
8684
String toString() => message;
8785
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cli_util
2-
version: 0.4.1
2+
version: 0.4.2-wip
33
description: A library to help in building Dart command-line apps.
44
repository: https://github.com/dart-lang/cli_util
55

test/cli_util_test.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import 'package:cli_util/cli_util.dart';
99
import 'package:path/path.dart' as p;
1010
import 'package:test/test.dart';
1111

12-
void main() => defineTests();
13-
14-
void defineTests() {
15-
group('getSdkPath', () {
12+
void main() {
13+
group('sdkPath', () {
1614
test('sdkPath', () {
17-
expect(getSdkPath(), isNotNull);
15+
expect(sdkPath, isNotNull);
1816
});
1917
});
2018

0 commit comments

Comments
 (0)