feat(core): allow a plugin build script to read the plugin config object (#7447)

This commit is contained in:
Lucas Fernandes Nogueira 2023-07-18 07:04:15 -07:00 committed by GitHub
parent aba04fa823
commit 522de0e788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 0 deletions

View File

@ -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.

View File

@ -0,0 +1,5 @@
---
"tauri-build": patch:feat
---
Added the `config::plugin_config` function to read the plugin configuration set from the CLI.

View File

@ -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
}
}

View File

@ -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;

View File

@ -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,