From 6cce4b9f4da055fb75fc555b1fae43be58ba3463 Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Fri, 26 Aug 2022 10:27:06 -0700 Subject: [PATCH] feat: commit code --- src/cli/mod.rs | 8 ++--- src/cli/plugin/mod.rs | 67 +++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++-- src/plugin/interface/mod.rs | 7 ++++ src/plugin/mod.rs | 2 +- 5 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 src/cli/plugin/mod.rs diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 818b3fa8f1..4508982d17 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -4,7 +4,7 @@ pub mod clean; pub mod config; pub mod create; pub mod serve; -pub mod tool; +pub mod plugin; pub mod translate; use crate::{ @@ -52,9 +52,9 @@ pub enum Commands { /// Dioxus config file controls. #[clap(subcommand)] Config(config::Config), - /// Install & Manage tools for Dioxus-cli. #[clap(subcommand)] - Tool(tool::Tool), + /// Manage plugins for dioxus cli + Plugin(plugin::Plugin), } impl Commands { @@ -66,7 +66,7 @@ impl Commands { Commands::Create(_) => "create", Commands::Clean(_) => "clean", Commands::Config(_) => "config", - Commands::Tool(_) => "tool", + Commands::Plugin(_) => "plugin", }.to_string() } } \ No newline at end of file diff --git a/src/cli/plugin/mod.rs b/src/cli/plugin/mod.rs new file mode 100644 index 0000000000..aee4b54b07 --- /dev/null +++ b/src/cli/plugin/mod.rs @@ -0,0 +1,67 @@ +use crate::tools; + +use super::*; + +/// Build the Rust WASM app and all of its assets. +#[derive(Clone, Debug, Deserialize, Subcommand)] +#[clap(name = "plugin")] +pub enum Plugin { + /// Return all dioxus-cli support tools. + List {}, + /// Get default app install path. + AppPath {}, + /// Install a new tool. + Add { name: String }, +} + +impl Plugin { + pub async fn plugin(self) -> Result<()> { + match self { + Plugin::List {} => { + for item in tools::tool_list() { + if tools::Tool::from_str(item).unwrap().is_installed() { + println!("- {item} [installed]"); + } else { + println!("- {item}"); + } + } + } + Plugin::AppPath {} => { + if let Some(v) = tools::tools_path().to_str() { + println!("{}", v); + } else { + log::error!("Tools path get failed."); + } + } + Plugin::Add { name } => { + let tool_list = tools::tool_list(); + + if !tool_list.contains(&name.as_str()) { + log::error!("Tool {name} not found."); + return Ok(()); + } + let target_tool = tools::Tool::from_str(&name).unwrap(); + + if target_tool.is_installed() { + log::warn!("Tool {name} is installed."); + return Ok(()); + } + + log::info!("Start to download tool package..."); + if let Err(e) = target_tool.download_package().await { + log::error!("Tool download failed: {e}"); + return Ok(()); + } + + log::info!("Start to install tool package..."); + if let Err(e) = target_tool.install_package().await { + log::error!("Tool install failed: {e}"); + return Ok(()); + } + + log::info!("Tool {name} install successfully!"); + } + } + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 7758c6a638..e7b96d4b55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,9 +48,9 @@ async fn main() -> Result<()> { } } - Commands::Tool(opts) => { - if let Err(e) = opts.tool().await { - log::error!("tool error: {}", e); + Commands::Plugin(opts) => { + if let Err(e) = opts.plugin().await { + log::error!("plugin error: {}", e); } } } diff --git a/src/plugin/interface/mod.rs b/src/plugin/interface/mod.rs index 0852f72117..2ef1920fcf 100644 --- a/src/plugin/interface/mod.rs +++ b/src/plugin/interface/mod.rs @@ -17,6 +17,7 @@ pub struct PluginInfo<'lua> { pub on_init: Option>, pub build: PluginBuildInfo<'lua>, + pub serve: PluginServeInfo<'lua>, } impl<'lua> FromLua<'lua> for PluginInfo<'lua> { @@ -29,6 +30,7 @@ impl<'lua> FromLua<'lua> for PluginInfo<'lua> { on_init: None, build: Default::default(), + serve: Default::default(), }; if let mlua::Value::Table(tab) = lua_value { if let Ok(v) = tab.get::<_, String>("name") { @@ -51,6 +53,10 @@ impl<'lua> FromLua<'lua> for PluginInfo<'lua> { if let Ok(v) = tab.get::<_, PluginBuildInfo>("build") { res.build = v; } + + if let Ok(v) = tab.get::<_, PluginServeInfo>("serve") { + res.serve = v; + } } Ok(res) @@ -70,6 +76,7 @@ impl<'lua> ToLua<'lua> for PluginInfo<'lua> { res.set("on_init", e)?; } res.set("build", self.build)?; + res.set("serve", self.serve)?; Ok(mlua::Value::Table(res)) } diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 45b783281b..7f0bd99dbc 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -3,7 +3,7 @@ use std::{ path::PathBuf, }; -use mlua::{AsChunk, Lua, Table, ToLuaMulti}; +use mlua::{AsChunk, Lua, Table}; use serde::{Deserialize, Serialize}; use serde_json::json;