feat(bundler): add TAURI_BUNDLER_TOOLS_GITHUB_MIRRORto specify a GitHub mirror (#10866)

closes #7338
This commit is contained in:
thep0y 2024-09-11 10:36:12 +08:00 committed by GitHub
parent d8ccf9d76a
commit 6566182258
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

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