From eea01f7f8c94c00d6c8ae4cbe6bde29e184552a6 Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Sun, 13 Mar 2022 13:19:54 +0800 Subject: [PATCH] feat: commit code --- Cargo.toml | 2 ++ src/cli/mod.rs | 4 ++++ src/cli/tool/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/cli/tools.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 6 ++++++ 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/cli/tool/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 0b96d72a2..8beca8f7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,8 @@ headers = "0.3.7" # tools download dirs = "4.0.0" reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] } +flate2 = "1.0.22" +tar = "0.4.38" [[bin]] path = "src/main.rs" diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 9f31c60ae..6e653507d 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -6,6 +6,7 @@ pub mod create; pub mod serve; pub mod translate; pub mod tools; +pub mod tool; use crate::{ cfg::{ConfigOptsBuild, ConfigOptsServe}, @@ -56,4 +57,7 @@ pub enum Commands { /// Dioxus config file controls. #[clap(subcommand)] Config(config::Config), + /// Install & Manage tools for Dioxus-cli. + #[clap(subcommand)] + Tool(tool::Tool), } diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs new file mode 100644 index 000000000..3f3a86bfc --- /dev/null +++ b/src/cli/tool/mod.rs @@ -0,0 +1,40 @@ +use super::*; + +/// Build the Rust WASM app and all of its assets. +#[derive(Clone, Debug, Deserialize, Subcommand)] +#[clap(name = "tool")] +pub enum Tool { + /// Return all dioxus-cli support tools. + List {}, + Add { + name: String + } +} + +impl Tool { + pub async fn tool(self) -> Result<()> { + match self { + Tool::List {} => { + for item in tools::tool_list() { + if tools::Tool::from_str(item).unwrap().is_installed() { + println!("{item} [installed]"); + } else { + println!("{item}"); + } + } + } + Tool::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(); + println!("{:?}", target_tool.download_package().await); + } + } + + Ok(()) + } +} diff --git a/src/cli/tools.rs b/src/cli/tools.rs index a042e7565..1eb68e773 100644 --- a/src/cli/tools.rs +++ b/src/cli/tools.rs @@ -8,6 +8,10 @@ pub enum Tool { WasmOpt, } +pub fn tool_list() -> Vec<&'static str> { + vec!["wasm-opt"] +} + pub fn app_path() -> PathBuf { let data_local = dirs::data_local_dir().unwrap(); let dioxus_dir = data_local.join("dioxus"); @@ -26,7 +30,24 @@ pub fn temp_path() -> PathBuf { temp_path } +pub fn tools_path(tool: &Tool) -> PathBuf { + let app_path = app_path(); + let temp_path = app_path.join("tools"); + if !temp_path.is_dir() { + create_dir_all(&temp_path).unwrap(); + } + temp_path.join(tool.name()) +} + +#[allow(clippy::should_implement_trait)] impl Tool { + pub fn from_str(name: &str) -> Option { + match name { + "wasm-opt" => Some(Self::WasmOpt), + _ => None, + } + } + pub fn name(&self) -> &str { match self { Self::WasmOpt => "wasm-opt", @@ -72,11 +93,23 @@ impl Tool { } } - pub async fn download_package(&self) -> anyhow::Result { - let temp_dir = temp_path(); - let download_url = self.download_url(); + pub fn extension(&self) -> &str { + match self { + Self::WasmOpt => "tar.gz", + } + } - let temp_out = temp_dir.join(format!("{}-tool.tmp", self.name())); + pub fn is_installed(&self) -> bool { + tools_path(self).is_dir() + } + + pub fn temp_out_path(&self) -> PathBuf { + temp_path().join(format!("{}-tool.tmp", self.name())) + } + + pub async fn download_package(&self) -> anyhow::Result { + let download_url = self.download_url(); + let temp_out = self.temp_out_path(); let mut file = tokio::fs::File::create(&temp_out) .await .context("failed creating temporary output file")?; @@ -91,4 +124,8 @@ impl Tool { Ok(temp_out) } + + pub async fn install_package() -> anyhow::Result<()> { + Ok(()) + } } diff --git a/src/main.rs b/src/main.rs index 97d4a3bb4..099ff74c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,12 @@ async fn main() -> Result<()> { log::error!("config error: {}", e); } } + + Commands::Tool(opts) => { + if let Err(e) = opts.tool().await { + log::error!("tool error: {}", e); + } + } } Ok(())