-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the `Preferences` standard library; a way to store a TOML-serializable dictionary into top-level `Project.toml` files, then force recompilation of child projects when the preferences are modified. This pull request adds the `Preferences` standard library, which does the actual writing to `Project.toml` files, as well as modifies the loading code to check whether the preferences have changed.
- Loading branch information
1 parent
ebbf55c
commit c81d14f
Showing
13 changed files
with
472 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -47,6 +47,7 @@ let | |
:Distributed, | ||
:SharedArrays, | ||
:TOML, | ||
:Preferences, | ||
:Artifacts, | ||
:Pkg, | ||
:Test, | ||
|
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name = "Preferences" | ||
uuid = "21216c6a-2e73-6563-6e65-726566657250" | ||
|
||
[deps] | ||
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" | ||
|
||
[extras] | ||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test", "Pkg"] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Preferences | ||
|
||
!!! compat "Julia 1.6" | ||
Julia's `Preferences` API requires at least Julia 1.6. | ||
|
||
Preferences support embedding a simple `Dict` of metadata for a package on a per-project basis. These preferences allow for packages to set simple, persistent pieces of data that the user has selected, that can persist across multiple versions of a package. | ||
|
||
## API Overview | ||
|
||
`Preferences` are used primarily through the `@load_preferences`, `@save_preferences` and `@modify_preferences` macros. These macros will auto-detect the UUID of the calling package, throwing an error if the calling module does not belong to a package. The function forms can be used to load, save or modify preferences belonging to another package. | ||
|
||
Example usage: | ||
|
||
```julia | ||
using Preferences | ||
|
||
function get_preferred_backend() | ||
prefs = @load_preferences() | ||
return get(prefs, "backend", "native") | ||
end | ||
|
||
function set_backend(new_backend) | ||
@modify_preferences!() do prefs | ||
prefs["backend"] = new_backend | ||
end | ||
end | ||
``` | ||
|
||
By default, preferences are stored within the `Project.toml` file of the currently-active project, and as such all new projects will start from a blank state, with all preferences being un-set. | ||
Package authors that wish to have a default value set for their preferences should use the `get(prefs, key, default)` pattern as shown in the code example above. | ||
|
||
# API Reference | ||
|
||
!!! compat "Julia 1.6" | ||
Julia's `Preferences` API requires at least Julia 1.6. | ||
|
||
```@docs | ||
Preferences.load_preferences | ||
Preferences.@load_preferences | ||
Preferences.save_preferences! | ||
Preferences.@save_preferences! | ||
Preferences.modify_preferences! | ||
Preferences.@modify_preferences! | ||
Preferences.clear_preferences! | ||
Preferences.@clear_preferences! | ||
``` |
Oops, something went wrong.