Browse Source

feat: commit code

mrxiaozhuox 3 năm trước cách đây
mục cha
commit
eea01f7f8c
5 tập tin đã thay đổi với 92 bổ sung3 xóa
  1. 2 0
      Cargo.toml
  2. 4 0
      src/cli/mod.rs
  3. 40 0
      src/cli/tool/mod.rs
  4. 40 3
      src/cli/tools.rs
  5. 6 0
      src/main.rs

+ 2 - 0
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"

+ 4 - 0
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),
 }

+ 40 - 0
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(())
+    }
+}

+ 40 - 3
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<Self> {
+        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 fn extension(&self) -> &str {
+        match self {
+            Self::WasmOpt => "tar.gz",
+        }
+    }
+
+    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<PathBuf> {
-        let temp_dir = temp_path();
         let download_url = self.download_url();
-
-        let temp_out = temp_dir.join(format!("{}-tool.tmp", self.name()));
+        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(())
+    }
 }

+ 6 - 0
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(())