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

Make sdkPath a getter #100

Merged
merged 12 commits into from
Apr 11, 2024
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,3 +1,7 @@
## 0.4.2-wip

- Add `sdkPath` getter, deprecate `getSdkPath` function.

## 0.4.1

- Fix a broken link in the readme.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand All @@ -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());
}
```
Expand Down
56 changes: 27 additions & 29 deletions lib/cli_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
// 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';
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
Expand All @@ -29,12 +32,12 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
/// (if `$XDG_CONFIG_HOME` is defined), and,
/// * `$HOME/.config/<productName>` 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
Expand All @@ -43,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) {
Expand All @@ -62,26 +60,26 @@ 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 {
final home = _env['HOME'];
if (home == null) {
throw EnvironmentNotFoundException(
r'Environment variable $HOME is not defined!');
}
return home;
}
String _requireEnv(String name) =>
_env[name] ?? (throw EnvironmentNotFoundException(name));
lrhn marked this conversation as resolved.
Show resolved Hide resolved

/// 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 message;
EnvironmentNotFoundException(this.message);
/// 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;
}
Expand Down
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.4.1
version: 0.4.2-wip
description: A library to help in building Dart command-line apps.
repository: https://github.com/dart-lang/cli_util

Expand Down
8 changes: 3 additions & 5 deletions test/cli_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down