From 143ef58daa02400812d2ebe633d53e6248ca5b91 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 10:49:25 +0200 Subject: [PATCH 01/12] Make `sdkPath` a getter Add `sdkPath` getter and deprecate `getSdkPath` function. --- lib/cli_util.dart | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index c7b867b..b23b228 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -10,11 +10,14 @@ import 'dart:io'; import 'package:path/path.dart' as path; -/// Return the path to the current Dart SDK. -String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable)); +/// The path to the current Dart SDK. +String get sdkPath => path.dirname(path.dirname(Platform.resolvedExecutable)); -/// Get the user-specific application configuration folder for the current -/// platform. +/// Returns the path to the current Dart SDK. +@Deprecated("Use 'sdkPath' instead") +String getSdkPath() => sdkPath; + +/// The user-specific application configuration folder for the current platform. /// /// This is a location appropriate for storing application specific /// configuration for the current user. The [productName] should be unique to @@ -70,18 +73,12 @@ String get _configHome { return path.join(_home, '.config'); } -String get _home { - final home = _env['HOME']; - if (home == null) { - throw EnvironmentNotFoundException( - r'Environment variable $HOME is not defined!'); - } - return home; -} +String get _home => _env['HOME'] ?? (throw EnvironmentNotFoundException('HOME')); class EnvironmentNotFoundException implements Exception { - final String message; - EnvironmentNotFoundException(this.message); + final String entryName; + String get message => 'Environment variable \$$entryName is not defined!'; + EnvironmentNotFoundException(this.entryName); @override String toString() => message; } From ab01aac71f8e4d4e33087087d3f0a1a1a51ca2fd Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 10:51:49 +0200 Subject: [PATCH 02/12] Update cli_util_test.dart Update tests to not use deprecated name. --- test/cli_util_test.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/cli_util_test.dart b/test/cli_util_test.dart index 349b629..e16bc59 100644 --- a/test/cli_util_test.dart +++ b/test/cli_util_test.dart @@ -9,12 +9,10 @@ import 'package:cli_util/cli_util.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; -void main() => defineTests(); - -void defineTests() { - group('getSdkPath', () { +void main() { + group('sdkPath', () { test('sdkPath', () { - expect(getSdkPath(), isNotNull); + expect(sdkPath, isNotNull); }); }); From a8659f29bc58906cb54adb265f7be428a1e3e645 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 10:55:04 +0200 Subject: [PATCH 03/12] Update pubspec.yaml Increment version to 0.4.2-dev. --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index b3e05ed..13bd912 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cli_util -version: 0.4.1 +version: 0.4.2-dev description: A library to help in building Dart command-line apps. repository: https://github.com/dart-lang/cli_util From 9960604cb56305352cbcd213ce41de7fc1855da5 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 10:57:16 +0200 Subject: [PATCH 04/12] Update README.md to use `sdkPath` --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c5a1b8b..d1add7c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A package to help in building Dart command-line apps. ## What's this? `package:cli_util` provides: -- utilities to find the Dart SDK directory (`getSdkPath()`) +- utilities to find the Dart SDK directory (`sdkPath`) - utilities to find the settings directory for a tool (`applicationConfigHome()`) - utilities to aid in showing rich CLI output and progress information (`cli_logging.dart`) @@ -20,11 +20,11 @@ import 'package:cli_util/cli_util.dart'; import 'package:path/path.dart' as path; main(args) { - // Get sdk dir from cli_util. - var sdkPath = getSdkPath(); + // Get SDK directory from cli_util. + var sdkDir = sdkPath; // Do stuff... For example, print version string - var versionFile = File(path.join(sdkPath, 'version')); + var versionFile = File(path.join(sdkDir, 'version')); print(versionFile.readAsStringSync()); } ``` From 4ad848bf0701408317c12066efe6f5491e50ab3a Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 10:57:52 +0200 Subject: [PATCH 05/12] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3cca22..ac4e21e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.2 + +- Add `sdkPath` getter, deprecate `getSdkPath` function. + ## 0.4.1 - Fix a broken link in the readme. From 695c763ea14201ce3a3a5c1245ba42d9202027a4 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 11:00:31 +0200 Subject: [PATCH 06/12] Update cli_util.dart Break long line. (Editing directly in GitHub, so no formatter.) --- lib/cli_util.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index b23b228..a67e233 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -73,7 +73,8 @@ String get _configHome { return path.join(_home, '.config'); } -String get _home => _env['HOME'] ?? (throw EnvironmentNotFoundException('HOME')); +String get _home => + _env['HOME'] ?? (throw EnvironmentNotFoundException('HOME')); class EnvironmentNotFoundException implements Exception { final String entryName; From 77241daf179418bf0b1c868257578ed7728c0df8 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 11:18:42 +0200 Subject: [PATCH 07/12] Update cli_util.dart Clean up environment access, make all uses of exception pass only the environment entry name. --- lib/cli_util.dart | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index a67e233..29e15fb 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -2,7 +2,7 @@ // 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. -/// Utilities to return the Dart SDK location. +/// Utilities to locate the Dart SDK. library cli_util; import 'dart:async'; @@ -32,12 +32,12 @@ String getSdkPath() => sdkPath; /// (if `$XDG_CONFIG_HOME` is defined), and, /// * `$HOME/.config/` otherwise. /// -/// This aims follows best practices for each platform, honoring the -/// [XDG Base Directory Specification][1] on Linux and [File System Basics][2] -/// on Mac OS. +/// The chosen location aims to follow best practices for each platform, +/// honoring the [XDG Base Directory Specification][1] on Linux and +/// [File System Basics][2] on Mac OS. /// -/// Throws an [EnvironmentNotFoundException] if `%APPDATA%` or `$HOME` is needed -/// but undefined. +/// Throws an [EnvironmentNotFoundException] if an environment entry, +/// `%APPDATA%` or `$HOME`, is needed and not available. /// /// [1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html /// [2]: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1 @@ -46,16 +46,11 @@ String applicationConfigHome(String productName) => String get _configHome { if (Platform.isWindows) { - final appdata = _env['APPDATA']; - if (appdata == null) { - throw EnvironmentNotFoundException( - 'Environment variable %APPDATA% is not defined!'); - } - return appdata; + return _requireEnv('APPDATA'); } if (Platform.isMacOS) { - return path.join(_home, 'Library', 'Application Support'); + return path.join(_requireEnv('HOME'), 'Library', 'Application Support'); } if (Platform.isLinux) { @@ -65,20 +60,25 @@ String get _configHome { } // XDG Base Directory Specification says to use $HOME/.config/ when // $XDG_CONFIG_HOME isn't defined. - return path.join(_home, '.config'); + return path.join(_requireEnv('HOME'), '.config'); } // We have no guidelines, perhaps we should just do: $HOME/.config/ // same as XDG specification would specify as fallback. - return path.join(_home, '.config'); + return path.join(_requireEnv('HOME'), '.config'); } -String get _home => - _env['HOME'] ?? (throw EnvironmentNotFoundException('HOME')); +String _requireEnv(String name) => + _env[name] ?? (throw EnvironmentNotFoundException(name)); +/// Exception thrown if a required environment entry does not exist. +/// +/// Thrown by [applicationConfigHome] if an expected and required, platform specific, +/// environment entry is not available. class EnvironmentNotFoundException implements Exception { - final String entryName; - String get message => 'Environment variable \$$entryName is not defined!'; + /// Name of environment entry which was needed, but not found. + final String entryName; + String get message => 'Environment variable \'$entryName\' is not defined!'; EnvironmentNotFoundException(this.entryName); @override String toString() => message; From 79d82a45acad601948041bba6a994890a08c09b7 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 11:19:59 +0200 Subject: [PATCH 08/12] Update cli_util.dart Long line in comment, not caught by formatter. --- lib/cli_util.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index 29e15fb..34c57c5 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -73,8 +73,8 @@ String _requireEnv(String name) => /// Exception thrown if a required environment entry does not exist. /// -/// Thrown by [applicationConfigHome] if an expected and required, platform specific, -/// environment entry is not available. +/// Thrown by [applicationConfigHome] if an expected and required +/// platform specific environment entry is not available. class EnvironmentNotFoundException implements Exception { /// Name of environment entry which was needed, but not found. final String entryName; From 112e74edf11edaf25731ba996352236045e75569 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 11:21:08 +0200 Subject: [PATCH 09/12] Update CHANGELOG.md to have -dev in version. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac4e21e..648427f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.4.2 +## 0.4.2-dev - Add `sdkPath` getter, deprecate `getSdkPath` function. From 1ba21dd497d8e0f798312a399b0c3f18b905f94d Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 3 Apr 2024 11:22:20 +0200 Subject: [PATCH 10/12] Format cli_util.dart --- lib/cli_util.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index 34c57c5..82ce9ea 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -73,7 +73,7 @@ String _requireEnv(String name) => /// Exception thrown if a required environment entry does not exist. /// -/// Thrown by [applicationConfigHome] if an expected and required +/// Thrown by [applicationConfigHome] if an expected and required /// platform specific environment entry is not available. class EnvironmentNotFoundException implements Exception { /// Name of environment entry which was needed, but not found. From 63a56a345ae2c2949ba2ce88622e7bf5e221a647 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Thu, 11 Apr 2024 08:47:22 +0200 Subject: [PATCH 11/12] Update pubspec.yaml Co-authored-by: Devon Carew --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 13bd912..a28627f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cli_util -version: 0.4.2-dev +version: 0.4.2-wip description: A library to help in building Dart command-line apps. repository: https://github.com/dart-lang/cli_util From 87208b2e61f2f03412be885d1e3223d9ac2ded13 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Thu, 11 Apr 2024 08:47:34 +0200 Subject: [PATCH 12/12] Update CHANGELOG.md Co-authored-by: Devon Carew --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 648427f..bab56a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.4.2-dev +## 0.4.2-wip - Add `sdkPath` getter, deprecate `getSdkPath` function.