From df4380b06638d75fda44cfe6cb2db38bfd33cde2 Mon Sep 17 00:00:00 2001 From: Julia Date: Mon, 27 Mar 2023 11:05:17 -0400 Subject: [PATCH] Download aarch64 or x64 Node binary according to system architecture --- crates/language/src/language.rs | 2 +- crates/zed/src/languages/node_runtime.rs | 73 +++++++++++++----------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index dc9a74e144..a4bf37c78b 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -792,7 +792,7 @@ impl LanguageRegistry { Ok(server) })); } - + dbg!(); let download_dir = self diff --git a/crates/zed/src/languages/node_runtime.rs b/crates/zed/src/languages/node_runtime.rs index da4ce42b43..7f3b138f2a 100644 --- a/crates/zed/src/languages/node_runtime.rs +++ b/crates/zed/src/languages/node_runtime.rs @@ -1,17 +1,20 @@ -use anyhow::{anyhow, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use async_compression::futures::bufread::GzipDecoder; use async_tar::Archive; use client::http::HttpClient; -use futures::{future::Shared, FutureExt, TryFutureExt}; +use futures::{future::Shared, FutureExt}; use gpui::{executor::Background, Task}; use parking_lot::Mutex; use serde::Deserialize; use smol::{fs, io::BufReader}; use std::{ + env::consts, path::{Path, PathBuf}, sync::Arc, }; +const VERSION: &str = "v18.15.0"; + #[derive(Deserialize)] #[serde(rename_all = "kebab-case")] pub struct NpmInfo { @@ -115,38 +118,7 @@ impl NodeRuntime { .get_or_insert_with(|| { let http = self.http.clone(); self.background - .spawn( - async move { - let version = "v18.15.0"; - let arch = "arm64"; - - let folder_name = format!("node-{version}-darwin-{arch}"); - let node_containing_dir = util::paths::SUPPORT_DIR.join("node"); - let node_dir = node_containing_dir.join(folder_name); - let node_binary = node_dir.join("bin/node"); - - if fs::metadata(&node_binary).await.is_err() { - _ = fs::remove_dir_all(&node_containing_dir).await; - fs::create_dir(&node_containing_dir) - .await - .context("error creating node containing dir")?; - - let url = format!("https://nodejs.org/dist/{version}/node-{version}-darwin-{arch}.tar.gz"); - let mut response = - http.get(&url, Default::default(), true) - .await - .context("error downloading Node binary tarball")?; - - let decompressed_bytes = - GzipDecoder::new(BufReader::new(response.body_mut())); - let archive = Archive::new(decompressed_bytes); - archive.unpack(&node_containing_dir).await?; - } - - anyhow::Ok(node_dir) - } - .map_err(Arc::new), - ) + .spawn(async move { Self::install(http).await.map_err(Arc::new) }) .shared() }) .clone(); @@ -156,4 +128,37 @@ impl NodeRuntime { Err(error) => Err(anyhow!("{}", error)), } } + + async fn install(http: Arc) -> Result { + let arch = match consts::ARCH { + "x86_64" => "x64", + "aarch64" => "arm64", + other => bail!("Running on unsupported platform: {other}"), + }; + + let folder_name = format!("node-{VERSION}-darwin-{arch}"); + let node_containing_dir = util::paths::SUPPORT_DIR.join("node"); + let node_dir = node_containing_dir.join(folder_name); + let node_binary = node_dir.join("bin/node"); + + if fs::metadata(&node_binary).await.is_err() { + _ = fs::remove_dir_all(&node_containing_dir).await; + fs::create_dir(&node_containing_dir) + .await + .context("error creating node containing dir")?; + + let file_name = format!("node-{VERSION}-darwin-{arch}.tar.gz"); + let url = format!("https://nodejs.org/dist/{VERSION}/{file_name}"); + let mut response = http + .get(&url, Default::default(), true) + .await + .context("error downloading Node binary tarball")?; + + let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut())); + let archive = Archive::new(decompressed_bytes); + archive.unpack(&node_containing_dir).await?; + } + + anyhow::Ok(node_dir) + } }