Browse Source

feat: commit code

mrxiaozhuox 3 năm trước cách đây
mục cha
commit
140d7ad7ed
6 tập tin đã thay đổi với 124 bổ sung23 xóa
  1. 45 0
      Dioxus.toml
  2. 5 1
      src/assets/dioxus.toml
  3. 5 0
      src/cli/config/mod.rs
  4. 25 1
      src/cli/tool/mod.rs
  5. 41 20
      src/cli/tools.rs
  6. 3 1
      src/config.rs

+ 45 - 0
Dioxus.toml

@@ -0,0 +1,45 @@
+[application]
+
+# dioxus project name
+name = "{{project-name}}"
+
+# default platfrom
+# you can also use `dioxus serve/build --platform XXX` to use other platform
+# value: web | desktop
+default_platform = "{{default-platform}}"
+
+# Web `build` & `serve` dist path
+out_dir = "dist"
+
+# resource (static) file folder
+asset_dir = "public"
+
+[web.app]
+
+# HTML title tag content
+title = "dioxus | ⛺"
+
+[web.watcher]
+
+watch_path = ["src"]
+
+# include `assets` in web platform
+[web.resource]
+
+# CSS style file
+style = []
+
+# Javascript code file
+script = []
+
+[web.resource.dev]
+
+# Javascript code file
+# serve: [dev-server] only
+script = []
+
+[application.tools]
+
+# use binaryen.wasm-opt for output Wasm file
+# binaryen just will trigger in `web` platform
+binaryen = { wasm_opt = [".PROJECT_OUT"] }

+ 5 - 1
src/assets/dioxus.toml

@@ -38,4 +38,8 @@ script = []
 # serve: [dev-server] only
 script = []
 
-[web.tools]
+[application.tools]
+
+# use binaryen.wasm-opt for output Wasm file
+# binaryen just will trigger in `web` platform
+binaryen = { wasm_opt = [".PROJECT_OUT"] }

+ 5 - 0
src/cli/config/mod.rs

@@ -18,6 +18,8 @@ pub enum Config {
         #[clap(long, default_value = "web")]
         platform: String,
     },
+    /// Format Print Dioxus-Config.
+    FormatPrint {}
 }
 
 impl Config {
@@ -43,6 +45,9 @@ impl Config {
                 file.write_all(content.as_bytes())?;
                 log::info!("🚩 Init config file completed.");
             }
+            Config::FormatPrint {} => {
+                println!("{:#?}", crate::CrateConfig::new()?.dioxus_config);
+            }
         }
         Ok(())
     }

+ 25 - 1
src/cli/tool/mod.rs

@@ -6,6 +6,9 @@ use super::*;
 pub enum Tool {
     /// Return all dioxus-cli support tools.
     List {},
+    /// Get default app install path.
+    AppPath {},
+    /// Install a new tool.
     Add {
         name: String
     }
@@ -23,6 +26,9 @@ impl Tool {
                     }
                 }
             }
+            Tool::AppPath {} => {
+                println!("{}", tools::tools_path().to_str().unwrap());
+            }
             Tool::Add { name } => {
                 let tool_list = tools::tool_list();
                 
@@ -31,7 +37,25 @@ impl Tool {
                     return Ok(());
                 }
                 let target_tool = tools::Tool::from_str(&name).unwrap();
-                println!("{:?}", target_tool.download_package().await);
+
+                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!");
             }
         }
 

+ 41 - 20
src/cli/tools.rs

@@ -1,15 +1,21 @@
-use std::{fs::create_dir_all, path::PathBuf};
+use std::{
+    fs::{create_dir_all, File},
+    path::PathBuf,
+};
 
 use anyhow::Context;
+use flate2::read::GzDecoder;
 use futures::StreamExt;
+use tar::Archive;
 use tokio::io::AsyncWriteExt;
 
+#[derive(Debug, PartialEq, Eq)]
 pub enum Tool {
-    WasmOpt,
+    Binaryen,
 }
 
 pub fn tool_list() -> Vec<&'static str> {
-    vec!["wasm-opt"]
+    vec!["binaryen"]
 }
 
 pub fn app_path() -> PathBuf {
@@ -30,45 +36,39 @@ pub fn temp_path() -> PathBuf {
     temp_path
 }
 
-pub fn tools_path(tool: &Tool) -> PathBuf {
+pub fn tools_path() -> 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())
+    temp_path
 }
 
 #[allow(clippy::should_implement_trait)]
 impl Tool {
     pub fn from_str(name: &str) -> Option<Self> {
         match name {
-            "wasm-opt" => Some(Self::WasmOpt),
+            "binaryen" => Some(Self::Binaryen),
             _ => None,
         }
     }
 
     pub fn name(&self) -> &str {
         match self {
-            Self::WasmOpt => "wasm-opt",
+            Self::Binaryen => "binaryen",
         }
     }
 
     pub fn bin_path(&self) -> &str {
-        if cfg!(target_os = "windows") {
-            match self {
-                Self::WasmOpt => "bin/wasm-opt.exe",
-            }
-        } else {
-            match self {
-                Self::WasmOpt => "bin/wasm-opt",
-            }
+        match self {
+            Self::Binaryen => "bin",
         }
     }
 
     pub fn target_platform(&self) -> &str {
         match self {
-            Self::WasmOpt => {
+            Self::Binaryen => {
                 if cfg!(target_os = "windows") {
                     "windows"
                 } else if cfg!(target_os = "macos") {
@@ -84,7 +84,7 @@ impl Tool {
 
     pub fn download_url(&self) -> String {
         match self {
-            Self::WasmOpt => {
+            Self::Binaryen => {
                 format!(
                     "https://github.com/WebAssembly/binaryen/releases/download/version_105/binaryen-version_105-x86_64-{target}.tar.gz",
                     target = self.target_platform()
@@ -95,12 +95,12 @@ impl Tool {
 
     pub fn extension(&self) -> &str {
         match self {
-            Self::WasmOpt => "tar.gz",
+            Self::Binaryen => "tar.gz",
         }
     }
 
     pub fn is_installed(&self) -> bool {
-        tools_path(self).is_dir()
+        tools_path().join(self.name()).is_dir()
     }
 
     pub fn temp_out_path(&self) -> PathBuf {
@@ -125,7 +125,28 @@ impl Tool {
         Ok(temp_out)
     }
 
-    pub async fn install_package() -> anyhow::Result<()> {
+    pub async fn install_package(&self) -> anyhow::Result<()> {
+        let temp_path = self.temp_out_path();
+        let tool_path = tools_path();
+
+        let dir_name = if self == &Tool::Binaryen {
+            "binaryen-version_105"
+        } else {
+            ""
+        };
+
+        if self.extension() == "tar.gz" {
+            let tar_gz = File::open(temp_path)?;
+            let tar = GzDecoder::new(tar_gz);
+            let mut archive = Archive::new(tar);
+            archive.unpack(&tool_path)?;
+            // println!("{:?} -> {:?}", tool_path.join(dir_name), tool_path.join(self.name()));
+            std::fs::rename(
+                tool_path.join(dir_name),
+                tool_path.join(self.name()),
+            )?;
+        }
+
         Ok(())
     }
 }

+ 3 - 1
src/config.rs

@@ -1,6 +1,6 @@
 use crate::error::Result;
 use serde::{Deserialize, Serialize};
-use std::{fs::File, io::Read, path::PathBuf};
+use std::{fs::File, io::Read, path::PathBuf, collections::HashMap};
 
 #[derive(Debug, Clone, Serialize, Deserialize)]
 pub struct DioxusConfig {
@@ -34,6 +34,7 @@ impl Default for DioxusConfig {
                 default_platform: "web".to_string(),
                 out_dir: Some(PathBuf::from("dist")),
                 asset_dir: Some(PathBuf::from("public")),
+                tools: None,
             },
             web: WebConfig {
                 app: WebAppConfing {
@@ -62,6 +63,7 @@ pub struct ApplicationConfig {
     pub default_platform: String,
     pub out_dir: Option<PathBuf>,
     pub asset_dir: Option<PathBuf>,
+    pub tools: Option<HashMap<String, toml::Value>>
 }
 
 #[derive(Debug, Clone, Serialize, Deserialize)]