First pass of plugin side of things complete

This commit is contained in:
Isaac Clayton 2022-06-08 10:32:19 +02:00
parent d7b97b25b8
commit 923f093aca
4 changed files with 78 additions and 56 deletions

14
plugins/Cargo.lock generated
View file

@ -75,6 +75,20 @@ name = "serde"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
plugin = { path = "../../crates/plugin" }
serde = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[lib]

View file

@ -1,10 +1,13 @@
use plugin::prelude::*;
use serde::Deserialize;
use serde_json::json;
use std::fs;
use std::path::PathBuf;
// #[import]
// fn command(string: String) -> Option<String>;
fn command(string: &str) -> Option<String> {
todo!()
}
// TODO: some sort of macro to generate ABI bindings
extern "C" {
@ -48,45 +51,46 @@ pub fn fetch_latest_server_version() -> Option<String> {
versions: Vec<String>,
}
// TODO: command returns error code
let output = command("npm info vscode-json-languageserver --json")?;
if !output.status.success() {
return None;
}
// if !output.is_ok() {
// return None;
// }
let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
let mut info: NpmInfo = serde_json::from_str(&output).ok()?;
info.versions.pop()
}
// #[bind]
// pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Option<PathBuf> {
// let version_dir = container_dir.join(version.as_str());
// fs::create_dir_all(&version_dir)
// .or_or_else(|| "failed to create version directory".to_string())?;
// let binary_path = version_dir.join(Self::BIN_PATH);
#[bind]
pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<PathBuf, String> {
let version_dir = container_dir.join(version.as_str());
fs::create_dir_all(&version_dir)
.map_err(|_| "failed to create version directory".to_string())?;
let binary_path = version_dir.join(BIN_PATH);
// if fs::metadata(&binary_path).await.is_err() {
// let output = command(format!(
// "npm install vscode-json-languageserver@{}",
// version
// ));
// if !output.status.success() {
// Err(anyhow!("failed to install vscode-json-languageserver"))?;
// }
if fs::metadata(&binary_path).is_err() {
let output = command(&format!(
"npm install vscode-json-languageserver@{}",
version
));
if output.is_none() {
return Err("failed to install vscode-json-languageserver".to_string());
}
// if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
// while let Some(entry) = entries.next().await {
// if let Some(entry) = entry.log_err() {
// let entry_path = entry.path();
// if entry_path.as_path() != version_dir {
// fs::remove_dir_all(&entry_path).await.log_err();
// }
// }
// }
// }
// }
if let Some(mut entries) = fs::read_dir(&container_dir).ok() {
while let Some(entry) = entries.next() {
if let Some(entry) = entry.ok() {
let entry_path = entry.path();
if entry_path.as_path() != version_dir {
fs::remove_dir_all(&entry_path).ok();
}
}
}
}
}
// Ok(binary_path)
// }
Ok(binary_path)
}
#[bind]
pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {