2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- /// Utilities to return the Dart SDK location .
5
+ /// Utilities to locate the Dart SDK.
6
6
library cli_util;
7
7
8
8
import 'dart:async' ;
9
9
import 'dart:io' ;
10
10
11
11
import 'package:path/path.dart' as path;
12
12
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));
15
15
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.
18
21
///
19
22
/// This is a location appropriate for storing application specific
20
23
/// configuration for the current user. The [productName] should be unique to
@@ -29,12 +32,12 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
29
32
/// (if `$XDG_CONFIG_HOME` is defined), and,
30
33
/// * `$HOME/.config/<productName>` otherwise.
31
34
///
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.
35
38
///
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 .
38
41
///
39
42
/// [1] : https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
40
43
/// [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) =>
43
46
44
47
String get _configHome {
45
48
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' );
52
50
}
53
51
54
52
if (Platform .isMacOS) {
55
- return path.join (_home , 'Library' , 'Application Support' );
53
+ return path.join (_requireEnv ( 'HOME' ) , 'Library' , 'Application Support' );
56
54
}
57
55
58
56
if (Platform .isLinux) {
@@ -62,26 +60,26 @@ String get _configHome {
62
60
}
63
61
// XDG Base Directory Specification says to use $HOME/.config/ when
64
62
// $XDG_CONFIG_HOME isn't defined.
65
- return path.join (_home , '.config' );
63
+ return path.join (_requireEnv ( 'HOME' ) , '.config' );
66
64
}
67
65
68
66
// We have no guidelines, perhaps we should just do: $HOME/.config/
69
67
// same as XDG specification would specify as fallback.
70
- return path.join (_home , '.config' );
68
+ return path.join (_requireEnv ( 'HOME' ) , '.config' );
71
69
}
72
70
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));
81
73
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.
82
78
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);
85
83
@override
86
84
String toString () => message;
87
85
}
0 commit comments