Skip to content

Commit

Permalink
feat(core): allow a plugin build script to read the plugin config obj…
Browse files Browse the repository at this point in the history
…ect (#7447)
  • Loading branch information
lucasfernog committed Jul 18, 2023
1 parent aba04fa commit 522de0e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-expose-plugin-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:feat
"@tauri-apps/cli": patch:feat
---

Expose an environment variable `TAURI_${PLUGIN_NAME}_PLUGIN_CONFIG` for each defined plugin configuration object.
5 changes: 5 additions & 0 deletions .changes/plugin-config-getter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-build": patch:feat
---

Added the `config::plugin_config` function to read the plugin configuration set from the CLI.
20 changes: 20 additions & 0 deletions core/tauri-build/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use serde::de::DeserializeOwned;

use std::{env::var, io::Cursor};

pub fn plugin_config<T: DeserializeOwned>(name: &str) -> Option<T> {
if let Ok(config_str) = var(format!(
"TAURI_{}_PLUGIN_CONFIG",
name.to_uppercase().replace('-', "_")
)) {
serde_json::from_reader(Cursor::new(config_str))
.map(Some)
.expect("failed to parse configuration")
} else {
None
}
}
2 changes: 2 additions & 0 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::{
mod allowlist;
#[cfg(feature = "codegen")]
mod codegen;
/// Tauri configuration functions.
pub mod config;
/// Mobile build functions.
pub mod mobile;
mod static_vcruntime;
Expand Down
10 changes: 10 additions & 0 deletions tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
// revert to previous working directory
set_current_dir(current_dir)?;

for (plugin, conf) in &config.plugins.0 {
set_var(
format!(
"TAURI_{}_PLUGIN_CONFIG",
plugin.to_uppercase().replace('-', "_")
),
serde_json::to_string(&conf)?,
);
}

*config_handle().lock().unwrap() = Some(ConfigMetadata {
inner: config,
extensions,
Expand Down

0 comments on commit 522de0e

Please sign in to comment.