Refactor out the node runtime crate and hook up all related imports
This commit is contained in:
parent
0ef9cefe0f
commit
941da24f73
21 changed files with 78 additions and 79 deletions
|
@ -1,21 +1,17 @@
|
|||
use anyhow::Context;
|
||||
use gpui::executor::Background;
|
||||
pub use language::*;
|
||||
use node_runtime::NodeRuntime;
|
||||
use rust_embed::RustEmbed;
|
||||
use std::{borrow::Cow, str, sync::Arc};
|
||||
use theme::ThemeRegistry;
|
||||
use util::http::HttpClient;
|
||||
|
||||
mod c;
|
||||
mod elixir;
|
||||
mod github;
|
||||
mod go;
|
||||
mod html;
|
||||
mod json;
|
||||
mod language_plugin;
|
||||
mod lua;
|
||||
mod node_runtime;
|
||||
mod python;
|
||||
mod ruby;
|
||||
mod rust;
|
||||
|
@ -37,13 +33,10 @@ mod yaml;
|
|||
struct LanguageDir;
|
||||
|
||||
pub fn init(
|
||||
http: Arc<dyn HttpClient>,
|
||||
background: Arc<Background>,
|
||||
languages: Arc<LanguageRegistry>,
|
||||
themes: Arc<ThemeRegistry>,
|
||||
node_runtime: Arc<NodeRuntime>,
|
||||
) {
|
||||
let node_runtime = NodeRuntime::new(http, background);
|
||||
|
||||
for (name, grammar, lsp_adapter) in [
|
||||
(
|
||||
"c",
|
||||
|
|
|
@ -9,7 +9,7 @@ use util::github::latest_github_release;
|
|||
use util::http::HttpClient;
|
||||
use util::ResultExt;
|
||||
|
||||
use super::github::GitHubLspBinaryVersion;
|
||||
use util::github::GitHubLspBinaryVersion;
|
||||
|
||||
pub struct CLspAdapter;
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use util::github::latest_github_release;
|
|||
use util::http::HttpClient;
|
||||
use util::ResultExt;
|
||||
|
||||
use super::github::GitHubLspBinaryVersion;
|
||||
use util::github::GitHubLspBinaryVersion;
|
||||
|
||||
pub struct ElixirLspAdapter;
|
||||
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use smol::io::AsyncReadExt;
|
||||
use std::sync::Arc;
|
||||
use util::http::HttpClient;
|
||||
|
||||
pub struct GitHubLspBinaryVersion {
|
||||
pub name: String,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(crate) struct GithubRelease {
|
||||
pub name: String,
|
||||
pub assets: Vec<GithubReleaseAsset>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(crate) struct GithubReleaseAsset {
|
||||
pub name: String,
|
||||
pub browser_download_url: String,
|
||||
}
|
||||
|
||||
pub(crate) async fn latest_github_release(
|
||||
repo_name_with_owner: &str,
|
||||
http: Arc<dyn HttpClient>,
|
||||
) -> Result<GithubRelease, anyhow::Error> {
|
||||
let mut response = http
|
||||
.get(
|
||||
&format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),
|
||||
Default::default(),
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.context("error fetching latest release")?;
|
||||
let mut body = Vec::new();
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_end(&mut body)
|
||||
.await
|
||||
.context("error reading latest release")?;
|
||||
let release: GithubRelease =
|
||||
serde_json::from_slice(body.as_slice()).context("error deserializing latest release")?;
|
||||
Ok(release)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use super::node_runtime::NodeRuntime;
|
||||
use node_runtime::NodeRuntime;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::node_runtime::NodeRuntime;
|
||||
use node_runtime::NodeRuntime;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use collections::HashMap;
|
||||
|
|
|
@ -8,7 +8,7 @@ use smol::fs;
|
|||
use std::{any::Any, env::consts, ffi::OsString, path::PathBuf, sync::Arc};
|
||||
use util::{async_iife, github::latest_github_release, http::HttpClient, ResultExt};
|
||||
|
||||
use super::github::GitHubLspBinaryVersion;
|
||||
use util::github::GitHubLspBinaryVersion;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct LuaLspAdapter;
|
||||
|
|
|
@ -1,166 +0,0 @@
|
|||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use async_compression::futures::bufread::GzipDecoder;
|
||||
use async_tar::Archive;
|
||||
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,
|
||||
};
|
||||
use util::http::HttpClient;
|
||||
|
||||
const VERSION: &str = "v18.15.0";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct NpmInfo {
|
||||
#[serde(default)]
|
||||
dist_tags: NpmInfoDistTags,
|
||||
versions: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub struct NpmInfoDistTags {
|
||||
latest: Option<String>,
|
||||
}
|
||||
|
||||
pub struct NodeRuntime {
|
||||
http: Arc<dyn HttpClient>,
|
||||
background: Arc<Background>,
|
||||
installation_path: Mutex<Option<Shared<Task<Result<PathBuf, Arc<anyhow::Error>>>>>>,
|
||||
}
|
||||
|
||||
impl NodeRuntime {
|
||||
pub fn new(http: Arc<dyn HttpClient>, background: Arc<Background>) -> Arc<NodeRuntime> {
|
||||
Arc::new(NodeRuntime {
|
||||
http,
|
||||
background,
|
||||
installation_path: Mutex::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn binary_path(&self) -> Result<PathBuf> {
|
||||
let installation_path = self.install_if_needed().await?;
|
||||
Ok(installation_path.join("bin/node"))
|
||||
}
|
||||
|
||||
pub async fn npm_package_latest_version(&self, name: &str) -> Result<String> {
|
||||
let installation_path = self.install_if_needed().await?;
|
||||
let node_binary = installation_path.join("bin/node");
|
||||
let npm_file = installation_path.join("bin/npm");
|
||||
|
||||
let output = smol::process::Command::new(node_binary)
|
||||
.arg(npm_file)
|
||||
.args(["-fetch-retry-mintimeout", "2000"])
|
||||
.args(["-fetch-retry-maxtimeout", "5000"])
|
||||
.args(["-fetch-timeout", "5000"])
|
||||
.args(["info", name, "--json"])
|
||||
.output()
|
||||
.await
|
||||
.context("failed to run npm info")?;
|
||||
|
||||
if !output.status.success() {
|
||||
Err(anyhow!(
|
||||
"failed to execute npm info:\nstdout: {:?}\nstderr: {:?}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
))?;
|
||||
}
|
||||
|
||||
let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
|
||||
info.dist_tags
|
||||
.latest
|
||||
.or_else(|| info.versions.pop())
|
||||
.ok_or_else(|| anyhow!("no version found for npm package {}", name))
|
||||
}
|
||||
|
||||
pub async fn npm_install_packages(
|
||||
&self,
|
||||
packages: impl IntoIterator<Item = (&str, &str)>,
|
||||
directory: &Path,
|
||||
) -> Result<()> {
|
||||
let installation_path = self.install_if_needed().await?;
|
||||
let node_binary = installation_path.join("bin/node");
|
||||
let npm_file = installation_path.join("bin/npm");
|
||||
|
||||
let output = smol::process::Command::new(node_binary)
|
||||
.arg(npm_file)
|
||||
.args(["-fetch-retry-mintimeout", "2000"])
|
||||
.args(["-fetch-retry-maxtimeout", "5000"])
|
||||
.args(["-fetch-timeout", "5000"])
|
||||
.arg("install")
|
||||
.arg("--prefix")
|
||||
.arg(directory)
|
||||
.args(
|
||||
packages
|
||||
.into_iter()
|
||||
.map(|(name, version)| format!("{name}@{version}")),
|
||||
)
|
||||
.output()
|
||||
.await
|
||||
.context("failed to run npm install")?;
|
||||
if !output.status.success() {
|
||||
Err(anyhow!(
|
||||
"failed to execute npm install:\nstdout: {:?}\nstderr: {:?}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn install_if_needed(&self) -> Result<PathBuf> {
|
||||
let task = self
|
||||
.installation_path
|
||||
.lock()
|
||||
.get_or_insert_with(|| {
|
||||
let http = self.http.clone();
|
||||
self.background
|
||||
.spawn(async move { Self::install(http).await.map_err(Arc::new) })
|
||||
.shared()
|
||||
})
|
||||
.clone();
|
||||
|
||||
match task.await {
|
||||
Ok(path) => Ok(path),
|
||||
Err(error) => Err(anyhow!("{}", error)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn install(http: Arc<dyn HttpClient>) -> Result<PathBuf> {
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use super::node_runtime::NodeRuntime;
|
||||
use node_runtime::NodeRuntime;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use super::github::{latest_github_release, GitHubLspBinaryVersion};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_compression::futures::bufread::GzipDecoder;
|
||||
use async_trait::async_trait;
|
||||
|
@ -9,6 +8,7 @@ use regex::Regex;
|
|||
use smol::fs::{self, File};
|
||||
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, str, sync::Arc};
|
||||
use util::fs::remove_matching;
|
||||
use util::github::{latest_github_release, GitHubLspBinaryVersion};
|
||||
use util::http::HttpClient;
|
||||
use util::ResultExt;
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use super::node_runtime::NodeRuntime;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
|
||||
use node_runtime::NodeRuntime;
|
||||
use serde_json::json;
|
||||
use smol::fs;
|
||||
use std::{
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use super::node_runtime::NodeRuntime;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use gpui::MutableAppContext;
|
||||
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
|
||||
use node_runtime::NodeRuntime;
|
||||
use serde_json::Value;
|
||||
use settings::Settings;
|
||||
use smol::fs;
|
||||
|
|
|
@ -18,6 +18,7 @@ use gpui::{Action, App, AssetSource, AsyncAppContext, MutableAppContext, Task, V
|
|||
use isahc::{config::Configurable, Request};
|
||||
use language::LanguageRegistry;
|
||||
use log::LevelFilter;
|
||||
use node_runtime::NodeRuntime;
|
||||
use parking_lot::Mutex;
|
||||
use project::Fs;
|
||||
use serde_json::json;
|
||||
|
@ -136,12 +137,9 @@ fn main() {
|
|||
languages.set_executor(cx.background().clone());
|
||||
languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
|
||||
let languages = Arc::new(languages);
|
||||
languages::init(
|
||||
http.clone(),
|
||||
cx.background().clone(),
|
||||
languages.clone(),
|
||||
themes.clone(),
|
||||
);
|
||||
let node_runtime = NodeRuntime::new(http.clone(), cx.background().to_owned());
|
||||
|
||||
languages::init(languages.clone(), themes.clone(), node_runtime.clone());
|
||||
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
|
||||
|
||||
cx.set_global(client.clone());
|
||||
|
@ -162,7 +160,7 @@ fn main() {
|
|||
terminal_view::init(cx);
|
||||
theme_testbench::init(cx);
|
||||
recent_projects::init(cx);
|
||||
copilot::init(client.clone(), cx);
|
||||
copilot::init(client.clone(), node_runtime, cx);
|
||||
|
||||
cx.spawn(|cx| watch_themes(fs.clone(), themes.clone(), cx))
|
||||
.detach();
|
||||
|
|
|
@ -657,6 +657,7 @@ mod tests {
|
|||
executor::Deterministic, AssetSource, MutableAppContext, TestAppContext, ViewHandle,
|
||||
};
|
||||
use language::LanguageRegistry;
|
||||
use node_runtime::NodeRuntime;
|
||||
use project::{Project, ProjectPath};
|
||||
use serde_json::json;
|
||||
use std::{
|
||||
|
@ -1851,12 +1852,9 @@ mod tests {
|
|||
languages.set_executor(cx.background().clone());
|
||||
let languages = Arc::new(languages);
|
||||
let themes = ThemeRegistry::new((), cx.font_cache().clone());
|
||||
languages::init(
|
||||
FakeHttpClient::with_404_response(),
|
||||
cx.background().clone(),
|
||||
languages.clone(),
|
||||
themes,
|
||||
);
|
||||
let http = FakeHttpClient::with_404_response();
|
||||
let node_runtime = NodeRuntime::new(http, cx.background().to_owned());
|
||||
languages::init(languages.clone(), themes, node_runtime);
|
||||
for name in languages.language_names() {
|
||||
languages.language_for_name(&name);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue