Browse Source

feat(bundler): add `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR`to specify a GitHub mirror (#10866)

closes #7338
thep0y 10 tháng trước cách đây
mục cha
commit
6566182258

+ 6 - 0
.changes/bundler-github-mirror-from-env.md

@@ -0,0 +1,6 @@
+---
+"tauri-bundler": patch:feat
+"tauri-cli": patch:feat
+---
+
+Add `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR` environment variable to specify a GitHub mirror to download files and tools used by tauri bundler. This is designed for areas like Mainland China where GitHub access can be unreliable.

+ 1 - 0
crates/tauri-bundler/Cargo.toml

@@ -42,6 +42,7 @@ sha1 = "0.10"
 sha2 = "0.10"
 zip = { version = "2.0", default-features = false, features = ["deflate"] }
 dunce = "1"
+url = "2"
 
 [target."cfg(target_os = \"windows\")".dependencies]
 uuid = { version = "1", features = ["v4", "v5"] }

+ 19 - 2
crates/tauri-bundler/src/bundle/windows/util.rs

@@ -9,6 +9,8 @@ use std::{
 };
 
 use sha2::Digest;
+use ureq::AgentBuilder;
+use url::Url;
 use zip::ZipArchive;
 
 pub const WEBVIEW2_BOOTSTRAPPER_URL: &str = "https://go.microsoft.com/fwlink/p/?LinkId=2124703";
@@ -67,11 +69,26 @@ pub fn download_webview2_offline_installer(base_path: &Path, arch: &str) -> crat
   Ok(file_path)
 }
 
+fn create_agent_and_url(url: &str) -> crate::Result<(ureq::Agent, String)> {
+  match std::env::var("TAURI_BUNDLER_TOOLS_GITHUB_MIRROR") {
+    Ok(cdn) if url.starts_with("https://github.com/") => {
+      let mut parsed_cdn = Url::parse(&cdn)?;
+      parsed_cdn.set_path(url);
+      Ok((AgentBuilder::new().build(), parsed_cdn.into()))
+    }
+    _ => Ok((
+      AgentBuilder::new().try_proxy_from_env(true).build(),
+      url.to_owned(),
+    )),
+  }
+}
+
 pub fn download(url: &str) -> crate::Result<Vec<u8>> {
   log::info!(action = "Downloading"; "{}", url);
 
-  let agent = ureq::AgentBuilder::new().try_proxy_from_env(true).build();
-  let response = agent.get(url).call().map_err(Box::new)?;
+  let (agent, final_url) = create_agent_and_url(url)?;
+
+  let response = agent.get(&final_url).call().map_err(Box::new)?;
   let mut bytes = Vec::new();
   response.into_reader().read_to_end(&mut bytes)?;
   Ok(bytes)

+ 3 - 0
crates/tauri-bundler/src/error.rs

@@ -58,6 +58,9 @@ pub enum Error {
   #[cfg(windows)]
   #[error("`{0}`")]
   Glob(#[from] glob::GlobError),
+  /// Failed to parse the URL
+  #[error("`{0}`")]
+  UrlParse(#[from] url::ParseError),
   /// Failed to validate downloaded file hash.
   #[error("hash mismatch of downloaded file")]
   HashError,

+ 1 - 0
crates/tauri-cli/ENVIRONMENT_VARIABLES.md

@@ -15,6 +15,7 @@ These environment variables are inputs to the CLI which may have an equivalent C
 - `TAURI_CLI_NO_DEV_SERVER_WAIT` — Skip waiting for the frontend dev server to start before building the tauri application.
 - `TAURI_LINUX_AYATANA_APPINDICATOR` — Set this var to `true` or `1` to force usage of `libayatana-appindicator` for system tray on Linux.
 - `TAURI_BUNDLER_WIX_FIPS_COMPLIANT` — Specify the bundler's WiX `FipsCompliant` option.
+- `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR` - Specify a GitHub mirror to download files and tools used by tauri bundler.
 - `TAURI_SKIP_SIDECAR_SIGNATURE_CHECK` - Skip signing sidecars.
 - `TAURI_SIGNING_PRIVATE_KEY` — Private key used to sign your app bundles, can be either a string or a path to the file.
 - `TAURI_SIGNING_PRIVATE_KEY_PASSWORD` — The signing private key password, see `TAURI_SIGNING_PRIVATE_KEY`.