-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flexible, extensible configuration infrastructure, with support f…
…or toml, CLI, and attr based config (#776)
- Loading branch information
1 parent
5c383d6
commit a5f1514
Showing
18 changed files
with
432 additions
and
57 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Configuring Diplomat | ||
|
||
Some Diplomat backends have configurable parameters to change the behavior of their output. | ||
|
||
For instance, Kotlin requires a `domain` parameter and `lib_name` parameter. These two parameters are used to generate a package name and folder structure on generation. | ||
|
||
## Configuration Structures | ||
Documentation on what configuration options are available in [`config.rs` on GitHub](https://github.com/rust-diplomat/diplomat/tree/main/tool/src/config.rs). | ||
|
||
Note that every configuration option uses `snake_case` for consistency, and that the `SharedConfig` struct is flattened. So for setting `--config shared_config.lib_name="some_value"`, this would instead be `--config lib_name="some_value"`. | ||
|
||
## Configuration Interfaces | ||
|
||
Configuration information can be set in four ways: | ||
|
||
### `config.toml` | ||
|
||
By default, Diplomat scans for a `config.toml` in the folder where `diplomat_tool` is being run. You can change the location of this folder with the `--config_file` parameter. | ||
|
||
The structure of `config.toml` is as follows: | ||
|
||
```toml | ||
# Top level table specifies Shared Config settings that apply to all backends: | ||
lib-name = "MyLibrary" | ||
|
||
[kotlin] | ||
# Individual tables can override Shared Config settings: | ||
lib-name = "LibraryNameOverride" | ||
# Along with backend specific settings: | ||
domain = "org.myOrganization" | ||
|
||
[demo_gen] | ||
explicit-generation = true | ||
|
||
[other-library-name] | ||
some-value = 100 | ||
``` | ||
|
||
### `diplomat-tool` CLI | ||
When running `diplomat-tool`, you may pass in the `--config` flag for each option you wish to set: | ||
|
||
``` | ||
./diplomat-tool kotlin ./kotlin-folder --config lib_name="MyLibrary" --config kotlin.domain = "org.myOrganization" | ||
``` | ||
|
||
`diplomat-tool` flags take priority over `config.toml`. | ||
|
||
### `diplomat_tool::gen` | ||
|
||
If you call `diplomat_tool::gen` manually, then you have the option of setting configuration yourself, with the `diplomat_tool::config::Config` struct. | ||
|
||
See [Backend Structures](#backend-structures) for more on these structures. | ||
|
||
### `#[diplomat::config(...)]` | ||
|
||
In `lib.rs`, any top-level `mod`ule, `struct`, or `impl` block can use the `#[diplomat::config]` attribute: | ||
|
||
```rust | ||
#[diplomat::config(lib_name="MyLibrary")] | ||
struct SomeConfig; | ||
|
||
#[diplomat::config(kotlin.domain="org.myOrganization")] | ||
mod kotlin_specific_mod; | ||
|
||
#[diplomat::config(...)] | ||
impl SomeConfig { | ||
|
||
} | ||
``` | ||
|
||
Due to a quirk of how Diplomat reads these attributes, `#[diplomat::config]` has priority over all other methods of setting configuration. |
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 |
---|---|---|
@@ -1,25 +1,23 @@ | ||
# Configuring Markup | ||
Diplomat takes the `-l` or `--library-config` option (in `diplomat_tool::gen` this is the `library_config` parameter). This represents a path to a `.toml` file that demo_gen will then read and convert into `DemoConfig`. | ||
|
||
Here's a sample .toml file for configuration (with comments for clarity): | ||
Through [Diplomat's configuration interfaces](../config.md), you can customize some of demo_gen's output. Here's a sample .toml file for configuration (with comments for clarity): | ||
|
||
```toml | ||
# If false, demo_gen will automatically search all methods for functions it can generate demonstration JS for. | ||
# If true, demo_gen will look for any methods explicitly flagged with #[diplomat::demo(generate)] to perform generation. | ||
explicit-generation=true # default = false (bool) | ||
explicit_generation=true # default = false (bool) | ||
|
||
# This removes the rendering/ folder. | ||
hide-default-renderer=true # default = false (bool) | ||
hide_default_renderer=true # default = false (bool) | ||
|
||
# Adjusts all imports that demo_gen creates to a specific module. Setting this will not generate the js/ folder. | ||
# | ||
# So for instance, this setting will adjust imports to: `import { type } from "icu4x"; | ||
module-name="icu4x" # (string) | ||
module_name="icu4x" # (string) | ||
|
||
# Adjusts all imports that demo_gen creates to a relative path where Diplomat JS output should be. Setting this will not generate the js/ folder. | ||
# | ||
# Setting this will adjust imports to: `import {type} from "../js/folder/here/index.mjs"; | ||
# | ||
# Intended to be a mutually exclusive setting with module-name, although you can set both simultaneously to import modules from a relative path. | ||
relative-js-path="../js/folder/here" # (string) | ||
# Intended to be a mutually exclusive setting with module_name, although you can set both simultaneously to import modules from a relative path. | ||
relative_js_path="../js/folder/here" # (string) | ||
``` |
11 changes: 11 additions & 0 deletions
11
core/src/ast/snapshots/diplomat_core__ast__attrs__tests__cfg_attr.snap
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,11 @@ | ||
--- | ||
source: core/src/ast/attrs.rs | ||
expression: attr | ||
--- | ||
key_value_pairs: | ||
- key: test.out | ||
value: "24" | ||
- key: other.out | ||
value: test | ||
- key: somefinal.out | ||
value: "\"testing spaces\"" |
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,7 @@ | ||
lib-name = "somelib" | ||
|
||
[kotlin] | ||
domain = "dev.diplomattest" | ||
|
||
[demo-gen] | ||
relative-js-path = "../../js/lib/api/" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
lib-name = "This Will Be Ignored For Testing Purposes (Check src/lib.rs)" | ||
|
||
[kotlin] | ||
domain = "dev.diplomattest" | ||
|
||
[demo-gen] | ||
relative-js-path = "../../js/api/" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.