From 140d7ad7ed1b724b19806ea8999bde84b904646d Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Mon, 14 Mar 2022 19:11:12 +0800 Subject: [PATCH] feat: commit code --- Dioxus.toml | 45 +++++++++++++++++++++++++++++++ src/assets/dioxus.toml | 6 ++++- src/cli/config/mod.rs | 5 ++++ src/cli/tool/mod.rs | 26 +++++++++++++++++- src/cli/tools.rs | 61 ++++++++++++++++++++++++++++-------------- src/config.rs | 4 ++- 6 files changed, 124 insertions(+), 23 deletions(-) create mode 100644 Dioxus.toml diff --git a/Dioxus.toml b/Dioxus.toml new file mode 100644 index 000000000..ab85abe86 --- /dev/null +++ b/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"] } \ No newline at end of file diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index b897a39a7..ab85abe86 100644 --- a/src/assets/dioxus.toml +++ b/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"] } \ No newline at end of file diff --git a/src/cli/config/mod.rs b/src/cli/config/mod.rs index 2a1f28fda..6422ede27 100644 --- a/src/cli/config/mod.rs +++ b/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(()) } diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index 3f3a86bfc..1b8768497 100644 --- a/src/cli/tool/mod.rs +++ b/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!"); } } diff --git a/src/cli/tools.rs b/src/cli/tools.rs index 1eb68e773..257c7f856 100644 --- a/src/cli/tools.rs +++ b/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 { 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(()) } } diff --git a/src/config.rs b/src/config.rs index ba0427191..f7897f5f8 100644 --- a/src/config.rs +++ b/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, pub asset_dir: Option, + pub tools: Option> } #[derive(Debug, Clone, Serialize, Deserialize)]