Jelajahi Sumber

feat(cli): load Cargo configuration to check default build target (#4990)

Lucas Fernandes Nogueira 3 tahun lalu
induk
melakukan
436f3d8d66

+ 6 - 0
.changes/cargo-config-target.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Check if the default build target is set in the Cargo configuration.

+ 14 - 3
tooling/cli/src/interface/rust.rs

@@ -35,8 +35,10 @@ use crate::helpers::{
   config::{reload as reload_config, wix_settings, Config},
 };
 
+mod cargo_config;
 mod desktop;
 mod manifest;
+use cargo_config::Config as CargoConfig;
 use manifest::{rewrite_manifest, Manifest};
 
 #[derive(Debug, Clone)]
@@ -427,6 +429,7 @@ pub struct RustAppSettings {
   cargo_settings: CargoSettings,
   cargo_package_settings: CargoPackageSettings,
   package_settings: PackageSettings,
+  cargo_config: CargoConfig,
 }
 
 impl AppSettings for RustAppSettings {
@@ -614,11 +617,14 @@ impl RustAppSettings {
       default_run: cargo_package_settings.default_run.clone(),
     };
 
+    let cargo_config = CargoConfig::load(&tauri_dir())?;
+
     Ok(Self {
       manifest,
       cargo_settings,
       cargo_package_settings,
       package_settings,
+      cargo_config,
     })
   }
 
@@ -627,7 +633,12 @@ impl RustAppSettings {
   }
 
   pub fn out_dir(&self, target: Option<String>, debug: bool) -> crate::Result<PathBuf> {
-    get_target_dir(target, !debug)
+    get_target_dir(
+      target
+        .as_deref()
+        .or_else(|| self.cargo_config.build().target()),
+      !debug,
+    )
   }
 }
 
@@ -655,12 +666,12 @@ fn get_cargo_metadata() -> crate::Result<CargoMetadata> {
 
 /// This function determines the 'target' directory and suffixes it with 'release' or 'debug'
 /// to determine where the compiled binary will be located.
-fn get_target_dir(target: Option<String>, is_release: bool) -> crate::Result<PathBuf> {
+fn get_target_dir(target: Option<&str>, is_release: bool) -> crate::Result<PathBuf> {
   let mut path = get_cargo_metadata()
     .with_context(|| "failed to get cargo metadata")?
     .target_directory;
 
-  if let Some(ref triple) = target {
+  if let Some(triple) = target {
     path.push(triple);
   }
 

+ 123 - 0
tooling/cli/src/interface/rust/cargo_config.rs

@@ -0,0 +1,123 @@
+use anyhow::{Context, Result};
+use serde::Deserialize;
+use std::{
+  fs,
+  path::{Path, PathBuf},
+};
+
+struct PathAncestors<'a> {
+  current: Option<&'a Path>,
+}
+
+impl<'a> PathAncestors<'a> {
+  fn new(path: &'a Path) -> PathAncestors<'a> {
+    PathAncestors {
+      current: Some(path),
+    }
+  }
+}
+
+impl<'a> Iterator for PathAncestors<'a> {
+  type Item = &'a Path;
+
+  fn next(&mut self) -> Option<&'a Path> {
+    if let Some(path) = self.current {
+      self.current = path.parent();
+
+      Some(path)
+    } else {
+      None
+    }
+  }
+}
+
+#[derive(Default, Deserialize)]
+pub struct BuildConfig {
+  target: Option<String>,
+}
+
+#[derive(Deserialize)]
+pub struct ConfigSchema {
+  build: Option<BuildConfig>,
+}
+
+#[derive(Default)]
+pub struct Config {
+  build: BuildConfig,
+}
+
+impl Config {
+  pub fn load(path: &Path) -> Result<Self> {
+    let mut config = Self::default();
+
+    for current in PathAncestors::new(path) {
+      if let Some(path) = get_file_path(&current.join(".cargo"), "config", true)? {
+        let contents = fs::read_to_string(&path)
+          .with_context(|| format!("failed to read configuration file `{}`", path.display()))?;
+        let toml: ConfigSchema = toml::from_str(&contents)
+          .with_context(|| format!("could not parse TOML configuration in `{}`", path.display()))?;
+
+        if let Some(target) = toml.build.and_then(|b| b.target) {
+          config.build.target = Some(target);
+          break;
+        }
+      }
+    }
+
+    Ok(config)
+  }
+
+  pub fn build(&self) -> &BuildConfig {
+    &self.build
+  }
+}
+
+impl BuildConfig {
+  pub fn target(&self) -> Option<&str> {
+    self.target.as_deref()
+  }
+}
+
+/// The purpose of this function is to aid in the transition to using
+/// .toml extensions on Cargo's config files, which were historically not used.
+/// Both 'config.toml' and 'credentials.toml' should be valid with or without extension.
+/// When both exist, we want to prefer the one without an extension for
+/// backwards compatibility, but warn the user appropriately.
+fn get_file_path(
+  dir: &Path,
+  filename_without_extension: &str,
+  warn: bool,
+) -> Result<Option<PathBuf>> {
+  let possible = dir.join(filename_without_extension);
+  let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
+
+  if possible.exists() {
+    if warn && possible_with_extension.exists() {
+      // We don't want to print a warning if the version
+      // without the extension is just a symlink to the version
+      // WITH an extension, which people may want to do to
+      // support multiple Cargo versions at once and not
+      // get a warning.
+      let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
+        target_path == possible_with_extension
+      } else {
+        false
+      };
+
+      if !skip_warning {
+        log::warn!(
+          "Both `{}` and `{}` exist. Using `{}`",
+          possible.display(),
+          possible_with_extension.display(),
+          possible.display()
+        );
+      }
+    }
+
+    Ok(Some(possible))
+  } else if possible_with_extension.exists() {
+    Ok(Some(possible_with_extension))
+  } else {
+    Ok(None)
+  }
+}