浏览代码

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

Lucas Fernandes Nogueira 2 年之前
父节点
当前提交
522de0e788

+ 6 - 0
.changes/cli-expose-plugin-config.md

@@ -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 - 0
.changes/plugin-config-getter.md

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

+ 20 - 0
core/tauri-build/src/config.rs

@@ -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 - 0
core/tauri-build/src/lib.rs

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

+ 10 - 0
tooling/cli/src/helpers/config.rs

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