This repository was archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Introduce applicationConfigHome #66
Merged
jonasfj
merged 2 commits into
dart-archive:master
from
jonasfj:add-applicationConfigHome
Sep 17, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -59,3 +59,66 @@ Directory getSdkDir([List<String>? cliArgs]) { | |||||
|
||||||
/// Return the path to the current Dart SDK. | ||||||
String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable)); | ||||||
|
||||||
/// Get 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 | ||||||
/// avoid clashes with other applications on the same machine. This method won't | ||||||
/// actually create the folder, merely return the recommended location for | ||||||
/// storing user-specific application configuration. | ||||||
/// | ||||||
/// The folder location depends on the platform: | ||||||
/// * `%APPDATA%\<productName>` on **Windows**, | ||||||
/// * `$HOME/Library/Application Support/<productName>` on **Mac OS**, | ||||||
/// * `$XDG_CONFIG_HOME/<productName>` on **Linux** | ||||||
/// (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. | ||||||
/// | ||||||
/// Throws if `%APPDATA%` or `$HOME` is undefined. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// | ||||||
/// [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 | ||||||
String applicationConfigHome(String productName) => | ||||||
path.join(_configHome, productName); | ||||||
|
||||||
String get _configHome { | ||||||
if (Platform.isWindows) { | ||||||
final appdata = Platform.environment['APPDATA']; | ||||||
if (appdata == null) { | ||||||
throw StateError('Environment variable %APPDATA% is not defined!'); | ||||||
} | ||||||
return appdata; | ||||||
} | ||||||
|
||||||
if (Platform.isMacOS) { | ||||||
return path.join(_home, 'Library', 'Application Support'); | ||||||
} | ||||||
|
||||||
if (Platform.isLinux) { | ||||||
final xdgConfigHome = Platform.environment['XDG_CONFIG_HOME']; | ||||||
if (xdgConfigHome != null) { | ||||||
return xdgConfigHome; | ||||||
} | ||||||
// XDG Base Directory Specification says to use $HOME/.config/ when | ||||||
// $XDG_CONFIG_HOME isn't defined. | ||||||
return path.join(_home, '.config'); | ||||||
pq marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
// We have no guidelines, perhaps we should just do: $HOME/.config/ | ||||||
// same as XDG specification would specify as fallback. | ||||||
return path.join(_home, '.config'); | ||||||
} | ||||||
|
||||||
String get _home { | ||||||
final home = Platform.environment['HOME']; | ||||||
if (home == null) { | ||||||
throw StateError('Environment variable \$HOME is not defined!'); | ||||||
} | ||||||
return home; | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,10 @@ void defineTests() { | |
expect(isSdkDir(Directory(getSdkPath())), true); | ||
}); | ||
}); | ||
|
||
group('applicationConfigHome', () { | ||
test('returns a string', () { | ||
expect(applicationConfigHome('dart'), isA<String>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm - this is hard to test - but it seems we should be able to do better. Could we at least confirm that the string ends with '${separator}dart' |
||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have documentation for the windows convention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, I can't find any canonical source on MSDN... The closes I got was:
https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables
But I have no idea what that page actually pertains to.. It looks like it says:
So it pertains to people writing such XML files.. and not as general documentation of the Windows environment variables. Or at-least this is not clear to me 🙈
There is also references to Windows File System Namespace Usage Guidelines floating around, but again I can't find updated documentation from original source.
Who knows... maybe it's hidden behind an MSDN subscription? Or maybe it's just so widely known and there is so many search hits that finding the canonical documentation is very hard.
We could use wikipedia:
I just gave up 🤣