diff --git a/lib/cli/src/commands/create_exe.rs b/lib/cli/src/commands/create_exe.rs index 5e685b3ab..3e6f000bb 100644 --- a/lib/cli/src/commands/create_exe.rs +++ b/lib/cli/src/commands/create_exe.rs @@ -1616,22 +1616,6 @@ fn find_zig_binary(path: Option) -> Result { } } retval - .or_else(|| { - #[cfg(feature = "http")] - { - match try_autoinstall_zig() { - Ok(p) => Some(p.join(get_zig_exe_str())), - Err(e) => { - eprintln!("Error when installing zig: {e}"); - None - } - } - } - #[cfg(not(feature = "http"))] - { - None - } - }) .ok_or_else(|| anyhow!("Could not find `zig` binary in PATH."))? }; @@ -1660,148 +1644,3 @@ fn find_zig_binary(path: Option) -> Result { Ok(retval) } } - -fn get_zig_exe_str() -> &'static str { - #[cfg(target_os = "windows")] - { - "zig.exe" - } - #[cfg(not(target_os = "windows"))] - { - "zig" - } -} - -/// Tries to auto-install zig into ~/.wasmer/utils/zig/{version} -#[cfg(feature = "http")] -fn try_autoinstall_zig() -> Result { - let zig_dir = wasmer_registry::get_wasmer_root_dir() - .ok_or_else(|| anyhow!("no wasmer root dir"))? - .join("utils") - .join(get_zig_exe_str()); - let mut existing_version = None; - - if !zig_dir.exists() { - return install_zig(&zig_dir); - } - - if let Ok(mut rd) = std::fs::read_dir(&zig_dir) { - existing_version = rd.next().and_then(|entry| { - let string = entry.ok()?.file_name().to_str()?.to_string(); - if zig_dir.join(&string).join(get_zig_exe_str()).exists() { - Some(string) - } else { - None - } - }) - } - - if let Some(exist) = existing_version { - return Ok(zig_dir.join(exist)); - } - - install_zig(&zig_dir) -} - -#[cfg(feature = "http")] -fn install_zig(target_targz_path: &Path) -> Result { - let url = "https://ziglang.org/download/index.json"; - let resp = reqwest::blocking::get(url); - let resp = resp.map_err(|e| anyhow!("{e}")).context(anyhow!("{url}"))?; - let resp = resp.json::(); - let resp = resp.map_err(|e| anyhow!("{e}")).context(anyhow!("{url}"))?; - - let default_key = "master".to_string(); - let (latest_version, latest_version_json) = resp - .versions - .get(&default_key) - .map(|v| (&default_key, v)) - .or_else(|| resp.versions.iter().next()) - .ok_or_else(|| anyhow!("no latest version of zig: {url}"))?; - - let latest_version = match latest_version_json.version.as_ref() { - Some(s) => s, - None => latest_version, - }; - - let install_dir = target_targz_path.join(latest_version); - if install_dir.join(get_zig_exe_str()).exists() { - return Ok(install_dir); - } - - let native_host_url = latest_version_json.get_native_host_url().ok_or_else(|| { - let native_host = format!("{}", target_lexicon::HOST); - anyhow!("could not get native host url for target {native_host:?}: {latest_version_json:#?}") - })?; - let _ = std::fs::create_dir_all(&install_dir); - wasmer_registry::download_and_unpack_targz(&native_host_url, &install_dir, true).context( - anyhow!( - "could not unpack {native_host_url} into {}", - install_dir.display() - ), - ) -} - -#[cfg(feature = "http")] -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -struct ZiglangOrgJson { - #[serde(flatten)] - versions: BTreeMap, -} - -#[cfg(feature = "http")] -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -struct ZiglangOrgJsonTarget { - version: Option, - date: String, - src: ZiglangOrgJsonBuildTarget, - #[serde(rename = "x86_64-freebsd")] - x86_64_freebsd: Option, - #[serde(rename = "x86_64-macos")] - x86_64_macos: Option, - #[serde(rename = "aarch64-macos")] - aarch64_macos: Option, - #[serde(rename = "x86_64-windows")] - x86_64_windows: Option, - #[serde(rename = "x86_64-linux")] - x86_64_linux: Option, - #[serde(rename = "aarch64-linux")] - aarch64_linux: Option, -} - -impl ZiglangOrgJsonTarget { - pub fn get_native_host_url(&self) -> Option { - let native_host = format!("{}", target_lexicon::HOST); - if native_host.starts_with("x86_64") { - if native_host.contains("freebsd") { - Some(self.x86_64_freebsd.as_ref()?.tarball.clone()) - } else if native_host.contains("darwin") || native_host.contains("macos") { - Some(self.x86_64_macos.as_ref()?.tarball.clone()) - } else if native_host.contains("windows") { - Some(self.x86_64_windows.as_ref()?.tarball.clone()) - } else if native_host.contains("linux") { - Some(self.x86_64_linux.as_ref()?.tarball.clone()) - } else { - None - } - } else if native_host.starts_with("aarch64") { - if native_host.contains("darwin") || native_host.contains("macos") { - Some(self.aarch64_macos.as_ref()?.tarball.clone()) - } else if native_host.contains("linux") { - Some(self.aarch64_linux.as_ref()?.tarball.clone()) - } else { - None - } - } else { - None - } - } -} - -#[cfg(feature = "http")] -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -struct ZiglangOrgJsonBuildTarget { - tarball: String, - shasum: String, - size: String, -}