Przeglądaj źródła

refactor(build): use cargo_toml instead of toml_edit for perf [TRI-023]

Lucas Nogueira 3 lat temu
rodzic
commit
9b4c47c4f8
2 zmienionych plików z 14 dodań i 33 usunięć
  1. 1 1
      core/tauri-build/Cargo.toml
  2. 13 32
      core/tauri-build/src/lib.rs

+ 1 - 1
core/tauri-build/Cargo.toml

@@ -22,7 +22,7 @@ quote = { version = "1", optional = true }
 tauri-codegen = { version = "1.0.0-beta.4", path = "../tauri-codegen", optional = true }
 serde_json = "1.0"
 tauri-utils = { version = "1.0.0-beta.0", path = "../tauri-utils", features = [ "build" ] }
-toml_edit = "0.5"
+cargo_toml = "0.10"
 
 [target."cfg(windows)".dependencies]
 winres = "0.1"

+ 13 - 32
core/tauri-build/src/lib.rs

@@ -105,49 +105,30 @@ pub fn build() {
 #[allow(unused_variables)]
 pub fn try_build(attributes: Attributes) -> Result<()> {
   use anyhow::anyhow;
+  use cargo_toml::{Dependency, Manifest};
   use std::fs::read_to_string;
   use tauri_utils::config::Config;
-  use toml_edit::{Document, Item, Table, Value};
 
   println!("cargo:rerun-if-changed=tauri.conf.json");
   println!("cargo:rerun-if-changed=src/Cargo.toml");
 
   let config: Config = serde_json::from_str(&read_to_string("tauri.conf.json")?)?;
 
-  let mut features = Vec::new();
-  let mut manifest: Document = read_to_string("Cargo.toml")?.parse::<Document>()?;
-  let dependencies = manifest
-    .as_table_mut()
-    .entry("dependencies")
-    .or_insert(Item::Table(Table::new()))
-    .as_table_mut()
-    .expect("manifest dependencies isn't a table");
-  let tauri_item = dependencies.entry("tauri").or_insert(Item::None);
-  if let Some(tauri) = tauri_item.as_table_mut() {
-    if let Item::Value(Value::Array(f)) = tauri.entry("features").or_insert(Item::None) {
-      for feat in f.iter() {
-        if let Value::String(feature) = feat {
-          features.push(feature.value().to_string());
-        }
-      }
-    }
-  } else if let Some(Value::InlineTable(table)) = tauri_item.as_value_mut() {
-    if let Some(Value::Array(f)) = table.get("features") {
-      for feat in f.iter() {
-        if let Value::String(feature) = feat {
-          features.push(feature.value().to_string());
-        }
-      }
-    }
-  }
-
-  features.sort();
-  let expected_features = config.tauri.features();
-  if features != expected_features {
-    return Err(anyhow!("
+  let mut manifest = Manifest::from_path("Cargo.toml")?;
+  if let Some(tauri) = manifest.dependencies.remove("tauri") {
+    let mut features = match tauri {
+      Dependency::Simple(_) => Vec::new(),
+      Dependency::Detailed(dep) => dep.features,
+    };
+    features.sort();
+
+    let expected_features = config.tauri.features();
+    if features != expected_features {
+      return Err(anyhow!("
       The `tauri` dependency features on the `Cargo.toml` file does not match the allowlist defined under `tauri.conf.json`.
       Please run `tauri dev` or `tauri build` or set it to {:?}.
     ", expected_features));
+    }
   }
 
   #[cfg(windows)]