Skip to content

Commit

Permalink
Implement installing plugins from local files (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjadev64 committed Dec 11, 2024
1 parent 790e80a commit a639531
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
25 changes: 17 additions & 8 deletions src-tauri/src/events/frontend/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ pub async fn list_plugins() -> Result<Vec<PluginInfo>, Error> {
}

#[command]
pub async fn install_plugin(app: AppHandle, id: String, url: Option<String>) -> Result<(), Error> {
let resp = match reqwest::get(url.unwrap_or(format!("https://plugins.amankhanna.me/rezipped/{id}.zip"))).await {
Ok(resp) => resp,
Err(error) => return Err(anyhow::Error::from(error).into()),
};
let bytes = match resp.bytes().await {
Ok(bytes) => bytes,
Err(error) => return Err(anyhow::Error::from(error).into()),
pub async fn install_plugin(app: AppHandle, id: String, url: Option<String>, file: Option<String>) -> Result<(), Error> {
let bytes = match file {
None => {
let resp = match reqwest::get(url.unwrap_or(format!("https://plugins.amankhanna.me/rezipped/{id}.zip"))).await {
Ok(resp) => resp,
Err(error) => return Err(anyhow::Error::from(error).into()),
};
use std::ops::Deref;
match resp.bytes().await {
Ok(bytes) => bytes.deref().to_owned(),
Err(error) => return Err(anyhow::Error::from(error).into()),
}
}
Some(path) => match std::fs::read(path) {
Ok(bytes) => bytes,
Err(error) => return Err(anyhow::Error::from(error).into()),
},
};

let _ = crate::plugins::deactivate_plugin(&app, &format!("{}.sdPlugin", id)).await;
Expand Down
30 changes: 25 additions & 5 deletions src/components/PluginManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ArrowClockwise from "phosphor-svelte/lib/ArrowClockwise";
import CloudArrowDown from "phosphor-svelte/lib/CloudArrowDown";
import FileArrowUp from "phosphor-svelte/lib/FileArrowUp";
import Trash from "phosphor-svelte/lib/Trash";
import ListedPlugin from "./ListedPlugin.svelte";
import Popup from "./Popup.svelte";
Expand All @@ -13,17 +14,17 @@
import { invoke } from "@tauri-apps/api/core";
import { onOpenUrl } from "@tauri-apps/plugin-deep-link";
import { ask, message } from "@tauri-apps/plugin-dialog";
import { ask, message, open } from "@tauri-apps/plugin-dialog";
export let actionList: () => ActionList;
export let deviceSelector: () => DeviceSelector;
let showPopup: boolean;
async function installPlugin(id: string, name: string, url: string | undefined = undefined) {
if (!await ask(`Install "${name}"? It may take a while to download the plugin.`, { title: `Install "${name}"?` })) return;
async function installPlugin(id: string, name: string, url: string | null = null, file: string | null = null) {
if (!file && !await ask(`Install "${name}"? It may take a while to download the plugin.`, { title: `Install "${name}"?` })) return;
try {
await invoke("install_plugin", { id, url });
await invoke("install_plugin", { id, url, file });
message(`Successfully installed "${name}".`, { title: `Installed "${name}"` });
actionList().reload();
installed = await invoke("list_plugins");
Expand Down Expand Up @@ -83,6 +84,16 @@
await installPlugin(plugin.id, plugin.name);
}
async function installPluginFile() {
const path = await open({ multiple: false, directory: false });
const id = prompt("Plugin ID:");
if (!id || id.split(".").length < 3) {
message("Failed to install plugin from file: invalid plugin ID", { title: "Failed to install" });
return;
}
installPlugin(id, id, null, path);
}
async function removePlugin(plugin: any) {
if (!await ask(`Are you sure you want to remove "${plugin.name}"?`, { title: `Remove "${plugin.name}"?` })) return;
try {
Expand Down Expand Up @@ -156,7 +167,16 @@
{/each}
</div>

<h2 class="mx-2 mt-6 mb-2 text-lg dark:text-neutral-400">Plugin store</h2>
<div class="flex flex-row justify-between items-center mx-2 mt-6 mb-2">
<h2 class="text-lg dark:text-neutral-400">Plugin store</h2>
<button
class="flex flex-row items-center mt-2 px-1 py-0.5 text-sm text-neutral-700 dark:text-neutral-300 bg-neutral-100 dark:bg-neutral-700 border dark:border-neutral-600 rounded-lg outline-none"
on:click={installPluginFile}
>
<FileArrowUp />
<span class="ml-1">Install from file</span>
</button>
</div>
<div class="flex flex-row m-2">
<input
bind:value={search}
Expand Down

0 comments on commit a639531

Please sign in to comment.