Skip to content

Commit

Permalink
using plugin registry (lapce#1643)
Browse files Browse the repository at this point in the history
* using plugin registry

* update changelog

* update changelog
  • Loading branch information
dzhou121 authored Nov 4, 2022
1 parent bfdbbd2 commit 940e7f1
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 135 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

### Features/Changes

- [#1643](https://github.com/lapce/lapce/pull/1643): Use https://plugins.lapce.dev/ as the plugin registry
- [#1620](https://github.com/lapce/lapce/pull/1620): Added "Show Hover" keybinding that will trigger the hover at the cursor location
- [#1619](https://github.com/lapce/lapce/pull/1619):
- Add active/inactive tab colours
- Add primary button colour
- Replace custom drawn checkboxes with icons in source control
- Add hover effect in source control panel
- Add colour preview in settings
- [#1619](https://github.com/lapce/lapce/pull/1619): Add active/inactive tab colours, add primary button colour, replace custom drawn checkboxes with icons in source
- [#1617](https://github.com/lapce/lapce/pull/1617): Fixed a stack overflow that would crash lapce when attempting to sort a large number of PaletteItems
- [#1609](https://github.com/lapce/lapce/pull/1609): Add syntax highlighting for erlang
- [#1590](https://github.com/lapce/lapce/pull/1590): Added ability to open file and file diff from source control context menu
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lapce-data/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,9 @@ pub enum LapceUICommand {
LoadPlugins(Vec<VoltInfo>),
LoadPluginsFailed,
VoltInstalled(VoltMetadata, bool),
VoltInstalling(VoltMetadata, String),
VoltInstalling(VoltInfo, String),
VoltRemoving(VoltMetadata, String),
VoltInstallStatusClear(String),
VoltRemoved(VoltInfo, bool),
EnableVolt(VoltInfo),
DisableVolt(VoltInfo),
Expand Down
46 changes: 21 additions & 25 deletions lapce-data/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use druid::{ExtEventSink, Target, WidgetId};
use indexmap::IndexMap;
use lapce_proxy::plugin::{download_volt, wasi::find_all_volts};
use lapce_rpc::plugin::{VoltInfo, VoltMetadata};
use lsp_types::Url;
use plugin_install_status::PluginInstallStatus;
use serde::{Deserialize, Serialize};
use strum_macros::Display;

use crate::{
Expand Down Expand Up @@ -86,6 +86,11 @@ pub enum PluginLoadStatus {
Success,
}

#[derive(Deserialize, Serialize)]
struct PluginsInfo {
plugins: Vec<VoltInfo>,
}

impl PluginData {
pub fn new(
tab_id: WidgetId,
Expand Down Expand Up @@ -152,7 +157,7 @@ impl PluginData {
if meta.version == volt.version {
PluginStatus::Installed
} else {
PluginStatus::Upgrade(volt.meta.clone())
PluginStatus::Upgrade
}
} else {
PluginStatus::Installed
Expand All @@ -163,9 +168,10 @@ impl PluginData {
}

fn load_volts() -> Result<Vec<VoltInfo>> {
let volts: Vec<VoltInfo> =
reqwest::blocking::get("https://lapce.dev/volts2")?.json()?;
Ok(volts)
let plugins: PluginsInfo =
reqwest::blocking::get("https://plugins.lapce.dev/api/v1/plugins")?
.json()?;
Ok(plugins.plugins)
}

pub fn download_readme(
Expand All @@ -174,8 +180,10 @@ impl PluginData {
config: &LapceConfig,
event_sink: ExtEventSink,
) -> Result<()> {
let url = Url::parse(&volt.meta)?;
let url = url.join("./README.md")?;
let url = format!(
"https://plugins.lapce.dev/api/v1/plugins/{}/{}/{}/readme",
volt.author, volt.name, volt.version
);
let resp = reqwest::blocking::get(url)?;
if resp.status() != 200 {
let text = parse_markdown("Plugin doesn't have a README", 2.0, config);
Expand All @@ -197,27 +205,19 @@ impl PluginData {
}

pub fn install_volt(proxy: Arc<LapceProxy>, volt: VoltInfo) -> Result<()> {
let meta_str = reqwest::blocking::get(&volt.meta)?.text()?;
let meta: VoltMetadata = toml_edit::easy::from_str(&meta_str)?;
proxy.core_rpc.volt_installing(volt.clone(), "".to_string());

proxy.core_rpc.volt_installing(meta.clone(), "".to_string());

if meta.wasm.is_some() {
if volt.wasm {
proxy.proxy_rpc.install_volt(volt);
} else {
std::thread::spawn(move || -> Result<()> {
let download_volt_result =
download_volt(volt, false, &meta, &meta_str);
let download_volt_result = download_volt(&volt);
if let Err(err) = download_volt_result {
log::warn!("download_volt err: {err:?}");
proxy.core_rpc.volt_installing(
meta.clone(),
"Could not download Volt".to_string(),
volt.clone(),
"Could not download Plugin".to_string(),
);
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
proxy.core_rpc.volt_installed(meta, true);
});
return Ok(());
}

Expand Down Expand Up @@ -247,10 +247,6 @@ impl PluginData {
meta.clone(),
"Could not remove Plugin Directory".to_string(),
);
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
proxy.core_rpc.volt_removed(meta.info(), true);
});
} else {
proxy.core_rpc.volt_removed(meta.info(), false);
}
Expand All @@ -265,6 +261,6 @@ impl PluginData {
pub enum PluginStatus {
Installed,
Install,
Upgrade(String),
Upgrade,
Disabled,
}
2 changes: 2 additions & 0 deletions lapce-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ authors = ["Dongdong Zhou <dzhou121@gmail.com>"]
edition = "2021"

[dependencies]
flate2 = "1.0.24"
tar = "0.4.38"
interprocess = "1.1.1"
clap = { version = "3.2.17", features = ["derive"] }
once_cell = "1.15"
Expand Down
134 changes: 38 additions & 96 deletions lapce-proxy/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod wasi;
use std::{
collections::HashMap,
fs,
io::Write,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
Expand All @@ -18,6 +17,7 @@ use std::{
use anyhow::{anyhow, Result};
use crossbeam_channel::{Receiver, Sender};
use dyn_clone::DynClone;
use flate2::read::GzDecoder;
use lapce_core::directory::Directory;
use lapce_rpc::{
core::CoreRpcHandler,
Expand Down Expand Up @@ -50,6 +50,7 @@ use lsp_types::{
use parking_lot::Mutex;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use tar::Archive;

use self::{
catalog::PluginCatalog,
Expand Down Expand Up @@ -930,79 +931,37 @@ pub enum PluginNotification {
},
}

pub fn download_volt(
volt: VoltInfo,
wasm: bool,
meta: &VoltMetadata,
meta_str: &String,
) -> Result<VoltMetadata> {
if meta.wasm.is_some() != wasm {
return Err(anyhow!("plugin type not fit"));
pub fn download_volt(volt: &VoltInfo) -> Result<VoltMetadata> {
let url = format!(
"https://plugins.lapce.dev/api/v1/plugins/{}/{}/{}/download",
volt.author, volt.name, volt.version
);

let resp = reqwest::blocking::get(url)?;
if !resp.status().is_success() {
return Err(anyhow!("can't download plugin"));
}

// this is the s3 url
let url = resp.text()?;

let mut resp = reqwest::blocking::get(url)?;
if !resp.status().is_success() {
return Err(anyhow!("can't download plugin"));
}

let id = volt.id();
let path = Directory::plugins_directory()
let plugin_dir = Directory::plugins_directory()
.ok_or_else(|| anyhow!("can't get plugin directory"))?
.join(&id);
let _ = fs::remove_dir_all(&path);
let _ = fs::remove_dir_all(&plugin_dir);
fs::create_dir_all(&plugin_dir)?;

fs::create_dir_all(&path)?;
let meta_path = path.join("volt.toml");
{
let mut file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&meta_path)?;
file.write_all(meta_str.as_bytes())?;
}
let url = url::Url::parse(&volt.meta)?;
if let Some(wasm) = meta.wasm.as_ref() {
let url = url.join(wasm)?;
{
let mut resp = reqwest::blocking::get(url)?;
if let Some(path) = path.join(wasm).parent() {
if !path.exists() {
fs::DirBuilder::new().recursive(true).create(path)?;
}
}
let mut file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path.join(wasm))?;
std::io::copy(&mut resp, &mut file)?;
}
}
if let Some(themes) = meta.color_themes.as_ref() {
for theme in themes {
let url = url.join(theme)?;
{
let mut resp = reqwest::blocking::get(url)?;
let mut file = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path.join(theme))?;
std::io::copy(&mut resp, &mut file)?;
}
}
}
if let Some(themes) = meta.icon_themes.as_ref() {
for theme in themes {
let url = url.join(theme)?;
{
let mut resp = reqwest::blocking::get(url)?;
let mut file = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path.join(theme))?;
std::io::copy(&mut resp, &mut file)?;
}
}
}
let tar = GzDecoder::new(&mut resp);
let mut archive = Archive::new(tar);
archive.unpack(&plugin_dir)?;

let meta_path = plugin_dir.join("volt.toml");
let meta = load_volt(&meta_path)?;
Ok(meta)
}
Expand All @@ -1013,31 +972,18 @@ pub fn install_volt(
configurations: Option<HashMap<String, serde_json::Value>>,
volt: VoltInfo,
) -> Result<()> {
let meta_str = reqwest::blocking::get(&volt.meta)?.text()?;
let meta: VoltMetadata = toml_edit::easy::from_str(&meta_str)?;

thread::spawn(move || -> Result<()> {
let download_volt_result = download_volt(volt, true, &meta, &meta_str);
if download_volt_result.is_err() {
catalog_rpc.core_rpc.volt_installing(
meta.clone(),
"Could not download Volt".to_string(),
);
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
catalog_rpc.core_rpc.volt_installed(meta, true);
});
return Ok(());
}

let meta = download_volt_result?;
let local_catalog_rpc = catalog_rpc.clone();
let local_meta = meta.clone();

let _ = start_volt(workspace, configurations, local_catalog_rpc, local_meta);
catalog_rpc.core_rpc.volt_installed(meta, false);
Ok(())
});
let download_volt_result = download_volt(&volt);
if download_volt_result.is_err() {
catalog_rpc
.core_rpc
.volt_installing(volt, "Could not download Plugin".to_string());
}
let meta = download_volt_result?;
let local_catalog_rpc = catalog_rpc.clone();
let local_meta = meta.clone();

let _ = start_volt(workspace, configurations, local_catalog_rpc, local_meta);
catalog_rpc.core_rpc.volt_installed(meta, false);
Ok(())
}

Expand All @@ -1058,10 +1004,6 @@ pub fn remove_volt(
volt.clone(),
"Could not remove Plugin Directory".to_string(),
);
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
catalog_rpc.core_rpc.volt_removed(volt.info(), true);
});
} else {
catalog_rpc.core_rpc.volt_removed(volt.info(), false);
}
Expand Down
4 changes: 2 additions & 2 deletions lapce-rpc/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum CoreNotification {
only_installing: bool,
},
VoltInstalling {
volt: VoltMetadata,
volt: VoltInfo,
error: String,
},
VoltRemoving {
Expand Down Expand Up @@ -240,7 +240,7 @@ impl CoreRpcHandler {
});
}

pub fn volt_installing(&self, volt: VoltMetadata, error: String) {
pub fn volt_installing(&self, volt: VoltInfo, error: String) {
self.notification(CoreNotification::VoltInstalling { volt, error });
}

Expand Down
7 changes: 4 additions & 3 deletions lapce-rpc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub struct PluginConfiguration {
}

#[derive(Deserialize, Clone, Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct VoltInfo {
pub name: String,
pub version: String,
pub display_name: String,
pub author: String,
pub description: String,
pub meta: String,
pub repository: Option<String>,
pub wasm: bool,
}

impl VoltInfo {
Expand Down Expand Up @@ -83,7 +83,8 @@ impl VoltMetadata {
display_name: self.display_name.clone(),
author: self.author.clone(),
description: self.description.clone(),
meta: "".to_string(),
repository: self.repository.clone(),
wasm: self.wasm.is_some(),
}
}
}
Loading

0 comments on commit 940e7f1

Please sign in to comment.