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.
66library cli_util;
77
88import 'dart:async' ;
99import 'dart:io' ;
1010
1111import '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
4447String 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.
8278class 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}
0 commit comments