Skip to content

Commit

Permalink
feat: Add Install Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Sep 3, 2023
1 parent 5a15547 commit 3fe3bad
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tauri-build = { version = "1.4", features = [] }

[dependencies]

tauri = { version = "1.4", features = [ "fs-all", "protocol-asset", "shell-all", "clipboard-all", "os-all", "http-all", "http-multipart", "updater", "notification-all", "global-shortcut-all", "window-all", "path-all", "system-tray"] }
tauri = { version = "1.4", features = [ "dialog-open", "fs-all", "protocol-asset", "shell-all", "clipboard-all", "os-all", "http-all", "http-multipart", "updater", "notification-all", "global-shortcut-all", "window-all", "path-all", "system-tray"] }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
Expand Down
45 changes: 45 additions & 0 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,48 @@ pub fn unset_proxy() -> Result<bool, ()> {
std::env::remove_var("all_proxy");
Ok(true)
}

#[tauri::command]
pub fn install_plugin(path_list: Vec<String>, plugin_type: &str) -> Result<i32, Error> {
let mut success_count = 0;
use std::env::consts::OS;

let ext_name = match OS {
"linux" => ".so",
"macos" => ".dylib",
"windows" => ".dll",
_ => return Err(Error::Error("Unknown OS".into())),
};
for path in path_list {
if !path.ends_with("zip") {
continue;
}
let path = std::path::Path::new(&path);
let file_name = path.file_name().unwrap().to_str().unwrap();
let file_name = file_name.replace(".zip", "");
if !file_name.starts_with("[plugin]") {
return Err(Error::Error(
"Invalid Plugin: file name must start with [plugin]".into(),
));
}
let config_path = dirs::config_dir().unwrap();
let config_path =
config_path.join(APP.get().unwrap().config().tauri.bundle.identifier.clone());
let config_path = config_path.join("plugins");
let config_path = config_path.join(plugin_type);
let plugin_path = config_path.join(file_name);
std::fs::create_dir_all(&config_path)?;
let mut zip = zip::ZipArchive::new(std::fs::File::open(path)?)?;
if zip.by_name("info.json").is_err() {
return Err(Error::Error("Invalid Plugin: miss info.json".into()));
}
if zip.by_name(format!("plugin{ext_name}").as_str()).is_err() {
return Err(Error::Error(
format!("Invalid Plugin: miss plugin{ext_name}").into(),
));
}
zip.extract(plugin_path)?;
success_count += 1;
}
Ok(success_count)
}
3 changes: 2 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ fn main() {
updater_window,
screenshot,
lang_detect,
webdav
webdav,
install_plugin
])
.on_system_tray_event(tray_event_handler)
.build(tauri::generate_context!())
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"fs": {
"all": true,
"scope": ["$APPCONFIG/**"]
},
"dialog": {
"open": true
}
},
"bundle": {
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"add_buildin_service": "Add Buildin Service",
"add_external_service": "Add Extension",
"add_service": "Add Service",
"test_failed": "Test Failed"
"test_failed": "Test Failed",
"install_plugin": "Install Plugin"
},
"history": {
"label": "History",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"add_buildin_service": "添加内置服务",
"add_external_service": "添加外部插件",
"add_service": "添加服务",
"test_failed": "测试失败"
"test_failed": "测试失败",
"install_plugin": "安装外部插件"
},
"history": {
"label": "历史记录",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { open } from '@tauri-apps/api/dialog';
import { invoke } from '@tauri-apps/api';
import { useAtomValue } from 'jotai';
import React from 'react';

import { useToastStyle } from '../../../../../../hooks';
import { pluginListAtom } from '..';

export default function SelectPluginModal(props) {
const { isOpen, onOpenChange, setConfigName, onConfigOpen } = props;
const pluginList = useAtomValue(pluginListAtom);
const { t } = useTranslation();
const toastStyle = useToastStyle();

return (
<Modal
isOpen={isOpen}
onOpenChange={onOpenChange}
scrollBehavior='inside'
>
<Toaster />
<ModalContent className='max-h-[80vh]'>
{(onClose) => (
<>
Expand All @@ -36,6 +42,42 @@ export default function SelectPluginModal(props) {
</div>
);
})}
<div>
<Button
fullWidth
color='secondary'
variant='flat'
onPress={async () => {
const selected = await open({
multiple: true,
directory: false,
filters: [
{
name: 'Plugin',
extensions: ['zip'],
},
],
});
if (selected !== null) {
invoke('install_plugin', {
pathList: selected,
pluginType: 'translate',
}).then(
(count) => {
toast.success('Installed ' + count + ' plugins', {
style: toastStyle,
});
},
(e) => {
toast.error(e.toString(), { style: toastStyle });
}
);
}
}}
>
<div className='w-full'>{t('config.service.install_plugin')}</div>
</Button>
</div>
</ModalBody>
<ModalFooter>
<Button
Expand Down

0 comments on commit 3fe3bad

Please sign in to comment.