From 5d013ecf824b1e62b8741e7e4565af8bf3262f97 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 11:54:00 +0800 Subject: [PATCH 01/26] Try to add `Sass` support --- src/assets/dioxus.toml | 5 ++++- src/tools.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index b43d0d6df..0c57542b8 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -42,4 +42,7 @@ script = [] # use binaryen.wasm-opt for output Wasm file # binaryen just will trigger in `web` platform -binaryen = { wasm_opt = true } \ No newline at end of file +binaryen = { wasm_opt = true } + +# use sass auto will auto check sass file and build it. +sass = { auto = true } \ No newline at end of file diff --git a/src/tools.rs b/src/tools.rs index 937063be0..db4d82268 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -13,10 +13,11 @@ use tokio::io::AsyncWriteExt; #[derive(Debug, PartialEq, Eq)] pub enum Tool { Binaryen, + Sass, } pub fn tool_list() -> Vec<&'static str> { - vec!["binaryen"] + vec!["binaryen", "sass"] } pub fn app_path() -> PathBuf { @@ -60,6 +61,7 @@ impl Tool { pub fn name(&self) -> &str { match self { Self::Binaryen => "binaryen", + Self::Sass => "sass", } } @@ -67,6 +69,7 @@ impl Tool { pub fn bin_path(&self) -> &str { match self { Self::Binaryen => "bin", + Self::Sass => ".", } } @@ -84,6 +87,17 @@ impl Tool { panic!("unsupported platformm"); } } + Self::Sass => { + if cfg!(target_os = "windows") { + "windows" + } else if cfg!(target_os = "macos") { + "macos" + } else if cfg!(target_os = "linux") { + "linux" + } else { + panic!("unsupported platformm"); + } + } } } @@ -96,6 +110,12 @@ impl Tool { target = self.target_platform() ) } + Self::Sass => { + format!( + "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.tar.gz", + target = self.target_platform() + ) + } } } @@ -103,6 +123,13 @@ impl Tool { pub fn extension(&self) -> &str { match self { Self::Binaryen => "tar.gz", + Self::Sass => { + if cfg!(target_os = "windows") { + "zip" + } else { + "tar.ge" + } + }, } } @@ -169,6 +196,9 @@ impl Tool { command.to_string() } } + Tool::Sass => { + command.to_string() + } }; if !bin_path.join(&command_file).is_file() { From c48345b86e556c0535d4ead6134921c68d4c121c Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 16:25:23 +0800 Subject: [PATCH 02/26] Add `zip` support --- Cargo.toml | 2 ++ src/tools.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21e2dad7e..d2bda881f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,11 +39,13 @@ hyper = "0.14.17" axum = { version = "0.5.1", features = ["ws", "headers"] } tower-http = { version = "0.2.2", features = ["fs", "trace"] } 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" +zip = "0.6.2" tower = "0.4.12" [[bin]] diff --git a/src/tools.rs b/src/tools.rs index db4d82268..b37d10cd2 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,6 @@ use std::{ fs::{create_dir_all, File}, - path::PathBuf, + path::{Path, PathBuf}, process::Command, }; @@ -112,8 +112,9 @@ impl Tool { } Self::Sass => { format!( - "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.tar.gz", - target = self.target_platform() + "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.{extension}", + target = self.target_platform(), + extension = self.extension() ) } } @@ -129,7 +130,7 @@ impl Tool { } else { "tar.ge" } - }, + } } } @@ -169,6 +170,8 @@ impl Tool { let dir_name = if self == &Tool::Binaryen { "binaryen-version_105" + } else if self == &Tool::Sass { + "sass-version-1_51" } else { "" }; @@ -180,6 +183,8 @@ impl Tool { 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()))?; + } else if self.extension() == "zip" { + // decompress the `zip` file } Ok(()) @@ -196,9 +201,7 @@ impl Tool { command.to_string() } } - Tool::Sass => { - command.to_string() - } + Tool::Sass => command.to_string(), }; if !bin_path.join(&command_file).is_file() { @@ -215,3 +218,32 @@ impl Tool { Ok(output.stdout) } } + +fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { + let zip_file = std::fs::File::open(&file)?; + let mut zip = zip::ZipArchive::new(zip_file)?; + + if !target.exists() { + let _ = std::fs::create_dir_all(target)?; + } + + for i in 0..zip.len() { + let mut file = zip.by_index(i)?; + if file.is_dir() { + // dir + let target = target.join(Path::new(&file.name().replace('\\', ""))); + let _ = std::fs::create_dir_all(target)?; + } else { + // file + let file_path = target.join(Path::new(file.name())); + let mut target_file = if !file_path.exists() { + std::fs::File::create(file_path)? + } else { + std::fs::File::open(file_path)? + }; + let _num = std::io::copy(&mut file, &mut target_file)?; + } + } + + Ok(()) +} From ea723f64cd362d4139d0766fc5a74b95166e2ca0 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 19:11:12 +0800 Subject: [PATCH 03/26] Support `sass` install --- src/cli/tool/mod.rs | 4 ++-- src/tools.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index d5c4581aa..f13c1620e 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -20,9 +20,9 @@ impl Tool { Tool::List {} => { for item in tools::tool_list() { if tools::Tool::from_str(item).unwrap().is_installed() { - println!("{item} [installed]"); + println!("- {item} [installed]"); } else { - println!("{item}"); + println!("- {item}"); } } } diff --git a/src/tools.rs b/src/tools.rs index b37d10cd2..9f96ce0b9 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -53,6 +53,7 @@ impl Tool { pub fn from_str(name: &str) -> Option { match name { "binaryen" => Some(Self::Binaryen), + "sass" => Some(Self::Sass), _ => None, } } @@ -128,7 +129,7 @@ impl Tool { if cfg!(target_os = "windows") { "zip" } else { - "tar.ge" + "tar.gz" } } } @@ -159,7 +160,7 @@ impl Tool { let chunk = chunk_res.context("error reading chunk from download")?; let _ = file.write(chunk.as_ref()).await; } - + // log::info!("temp file path: {:?}", temp_out); Ok(temp_out) } @@ -170,10 +171,8 @@ impl Tool { let dir_name = if self == &Tool::Binaryen { "binaryen-version_105" - } else if self == &Tool::Sass { - "sass-version-1_51" } else { - "" + "dart-sass" }; if self.extension() == "tar.gz" { @@ -181,10 +180,11 @@ impl Tool { 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()))?; } else if self.extension() == "zip" { // decompress the `zip` file + extract_zip(&temp_path, &tool_path)?; + std::fs::rename(tool_path.join(dir_name), tool_path.join(self.name()))?; } Ok(()) From f7f1d90520bf9712172175ad6ccbec41803172f2 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sun, 8 May 2022 00:56:22 +0800 Subject: [PATCH 04/26] Add `sass` support --- src/assets/dioxus.toml | 14 +++++++++++++- src/cli/tool/mod.rs | 7 +++++-- src/tools.rs | 8 +++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 0c57542b8..8f160b293 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -45,4 +45,16 @@ script = [] binaryen = { wasm_opt = true } # use sass auto will auto check sass file and build it. -sass = { auto = true } \ No newline at end of file +[application.tools.sass] + +version = "1.51.0" + +# if auto = true, we will check style array, and translate all sass file to css. +auto = true + +# or you can specify some sass file -> css file +files = [ + # some sass file path + # this file will translate to `/css/test.css` + "/css/test.sass" +] \ No newline at end of file diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index f13c1620e..45c866c40 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -27,7 +27,11 @@ impl Tool { } } Tool::AppPath {} => { - println!("{}", tools::tools_path().to_str().unwrap()); + if let Some(v) = tools::tools_path().to_str() { + println!("{}", v); + } else { + log::error!("Tools path get failed."); + } } Tool::Add { name } => { let tool_list = tools::tool_list(); @@ -58,7 +62,6 @@ impl Tool { log::info!("Tool {name} install successfully!"); } } - Ok(()) } } diff --git a/src/tools.rs b/src/tools.rs index 9f96ce0b9..ae4e58b79 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -201,7 +201,13 @@ impl Tool { command.to_string() } } - Tool::Sass => command.to_string(), + Tool::Sass => { + if cfg!(target_os = "windows") { + format!("{}.bat", command) + } else { + command.to_string() + } + }, }; if !bin_path.join(&command_file).is_file() { From 768a0426d8519073259f9e642bc0b44f75110f52 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 10:31:02 +0800 Subject: [PATCH 05/26] Add `sass` support --- Cargo.toml | 2 ++ src/assets/dioxus.toml | 4 +--- src/builder.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2bda881f..c869267b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,8 @@ axum = { version = "0.5.1", features = ["ws", "headers"] } tower-http = { version = "0.2.2", features = ["fs", "trace"] } headers = "0.3.7" +walkdir = "2" + # tools download dirs = "4.0.0" reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] } diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 8f160b293..3e82e0c93 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -47,8 +47,6 @@ binaryen = { wasm_opt = true } # use sass auto will auto check sass file and build it. [application.tools.sass] -version = "1.51.0" - # if auto = true, we will check style array, and translate all sass file to css. auto = true @@ -56,5 +54,5 @@ auto = true files = [ # some sass file path # this file will translate to `/css/test.css` - "/css/test.sass" + "/css/test.scss" ] \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index f7f68e2bc..7c3a7e9fb 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,6 +1,7 @@ use crate::{ config::{CrateConfig, ExecutableType}, error::{Error, Result}, + tools::Tool, DioxusConfig, }; use std::{ @@ -29,6 +30,10 @@ pub fn build(config: &CrateConfig) -> Result<()> { .. } = config; + + // start to build the assets + build_assets(config)?; + let t_start = std::time::Instant::now(); // [1] Build the .wasm module @@ -339,6 +344,49 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { html.replace("{app_title}", &title) } +// this function will build some assets file +// like sass tool resources +fn build_assets(config: &CrateConfig) -> Result<()> { + let dioxus_config = &config.dioxus_config; + let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); + + // check sass tool state + let sass = Tool::Sass; + if sass.is_installed() && dioxus_tools.contains_key("sass") { + let sass_conf = dioxus_tools.get("sass").unwrap(); + if let Some(tab) = sass_conf.as_table() { + if tab.contains_key("auto") && tab.get("auto").unwrap().as_bool().unwrap_or(false) { + // if the sass open auto, we need auto-check the assets dir. + let asset_dir = config.asset_dir.clone(); + if asset_dir.is_dir() { + for entry in walkdir::WalkDir::new(asset_dir) + .into_iter() + .filter_map(|e| e.ok()) + { + let temp = entry.path(); + if temp.is_file() { + let suffix = temp.file_name().unwrap().to_str().unwrap(); + let suffix = suffix.split('.').collect::>(); + let suffix: &str = suffix.last().unwrap(); + if suffix == "scss" { + // if file suffix is `scss` we need transform it. + let temp_stem = temp.file_stem().unwrap().to_str().unwrap(); + let target_path = + temp.parent().unwrap().join(format!("{}.css", temp_stem)); + sass.call( + "sass", + vec![temp.to_str().unwrap(), target_path.to_str().unwrap()], + )?; + } + } + } + } + } + } + } + Ok(()) +} + // use binary_install::{Cache, Download}; // /// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a From 8b06fe60596371118e47ef71a72627044c17672a Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 13:46:53 +0800 Subject: [PATCH 06/26] Fixed sass tool --- src/assets/dioxus.toml | 4 ++-- src/builder.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 3e82e0c93..105251aa1 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -47,10 +47,10 @@ binaryen = { wasm_opt = true } # use sass auto will auto check sass file and build it. [application.tools.sass] -# if auto = true, we will check style array, and translate all sass file to css. +# auto will check the assets dirs, and auto to transform all scss file to css file. auto = true -# or you can specify some sass file -> css file +# you can specify some scss file -> css file files = [ # some sass file path # this file will translate to `/css/test.css` diff --git a/src/builder.rs b/src/builder.rs index 7c3a7e9fb..0e0cf7e9a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -30,7 +30,6 @@ pub fn build(config: &CrateConfig) -> Result<()> { .. } = config; - // start to build the assets build_assets(config)?; @@ -359,20 +358,21 @@ fn build_assets(config: &CrateConfig) -> Result<()> { // if the sass open auto, we need auto-check the assets dir. let asset_dir = config.asset_dir.clone(); if asset_dir.is_dir() { - for entry in walkdir::WalkDir::new(asset_dir) + for entry in walkdir::WalkDir::new(&asset_dir) .into_iter() .filter_map(|e| e.ok()) { let temp = entry.path(); if temp.is_file() { - let suffix = temp.file_name().unwrap().to_str().unwrap(); - let suffix = suffix.split('.').collect::>(); - let suffix: &str = suffix.last().unwrap(); + let suffix = temp.extension().unwrap().to_str().unwrap(); if suffix == "scss" { // if file suffix is `scss` we need transform it. - let temp_stem = temp.file_stem().unwrap().to_str().unwrap(); - let target_path = - temp.parent().unwrap().join(format!("{}.css", temp_stem)); + let out_file = + format!("{}.css", temp.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join(temp.strip_prefix(&asset_dir).unwrap().parent().unwrap()) + .join(out_file); sass.call( "sass", vec![temp.to_str().unwrap(), target_path.to_str().unwrap()], From b5ecd09185062724415e9bf7b6b10d7e2c6860e8 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 19:22:19 +0800 Subject: [PATCH 07/26] Add `files` list option --- src/assets/dioxus.toml | 2 +- src/builder.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 105251aa1..5f5032e2a 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -50,7 +50,7 @@ binaryen = { wasm_opt = true } # auto will check the assets dirs, and auto to transform all scss file to css file. auto = true -# you can specify some scss file -> css file +# or you can specify some scss file -> css file files = [ # some sass file path # this file will translate to `/css/test.css` diff --git a/src/builder.rs b/src/builder.rs index 0e0cf7e9a..fb0c8088d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -381,6 +381,28 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } } } + } else if tab.contains_key("files") && tab.get("files").unwrap().is_array() { + // check files list. + let list = tab.get("files").unwrap().as_array().unwrap(); + for i in list { + if i.is_str() { + let path = i.as_str().unwrap(); + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); + if path.is_file() { + sass.call( + "sass", + vec![ + path.to_str().unwrap(), + path.parent().unwrap().join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )).to_str().unwrap(), + ], + )?; + } + } + } } } } From 551d2477ffcd84d13cfc4c5d15e1c8033a23a160 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 22:32:47 +0800 Subject: [PATCH 08/26] Add `source_map` option --- src/assets/dioxus.toml | 4 +++- src/builder.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 5f5032e2a..b481193a8 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -55,4 +55,6 @@ files = [ # some sass file path # this file will translate to `/css/test.css` "/css/test.scss" -] \ No newline at end of file +] + +source_map = true \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index fb0c8088d..825a9eebf 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -354,6 +354,17 @@ fn build_assets(config: &CrateConfig) -> Result<()> { if sass.is_installed() && dioxus_tools.contains_key("sass") { let sass_conf = dioxus_tools.get("sass").unwrap(); if let Some(tab) = sass_conf.as_table() { + let source_map = tab.contains_key("source_map"); + let source_map = if source_map && tab.get("source_map").unwrap().is_bool() { + if tab.get("source_map").unwrap().as_bool().unwrap_or_default() { + "--source-map" + } else { + "--no-source-map" + } + } else { + "--no-source-map" + }; + if tab.contains_key("auto") && tab.get("auto").unwrap().as_bool().unwrap_or(false) { // if the sass open auto, we need auto-check the assets dir. let asset_dir = config.asset_dir.clone(); @@ -375,7 +386,11 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .join(out_file); sass.call( "sass", - vec![temp.to_str().unwrap(), target_path.to_str().unwrap()], + vec![ + temp.to_str().unwrap(), + target_path.to_str().unwrap(), + source_map, + ], )?; } } @@ -394,10 +409,15 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "sass", vec![ path.to_str().unwrap(), - path.parent().unwrap().join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )).to_str().unwrap(), + path.parent() + .unwrap() + .join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )) + .to_str() + .unwrap(), + source_map, ], )?; } From 9ddaa773d9ffa8d6274a2731dfdac00223f6b74d Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 22:58:48 +0800 Subject: [PATCH 09/26] Remove `auto` option --- src/builder.rs | 111 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 38 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 825a9eebf..75c83990d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -362,48 +362,54 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "--no-source-map" } } else { - "--no-source-map" + "--source-map" }; - if tab.contains_key("auto") && tab.get("auto").unwrap().as_bool().unwrap_or(false) { - // if the sass open auto, we need auto-check the assets dir. - let asset_dir = config.asset_dir.clone(); - if asset_dir.is_dir() { - for entry in walkdir::WalkDir::new(&asset_dir) - .into_iter() - .filter_map(|e| e.ok()) - { - let temp = entry.path(); - if temp.is_file() { - let suffix = temp.extension().unwrap().to_str().unwrap(); - if suffix == "scss" { - // if file suffix is `scss` we need transform it. - let out_file = - format!("{}.css", temp.file_stem().unwrap().to_str().unwrap()); - let target_path = config - .out_dir - .join(temp.strip_prefix(&asset_dir).unwrap().parent().unwrap()) - .join(out_file); - sass.call( - "sass", - vec![ - temp.to_str().unwrap(), - target_path.to_str().unwrap(), - source_map, - ], - )?; + if tab.contains_key("files") { + if tab.get("files").unwrap().is_str() { + let file = tab.get("files").unwrap().as_str().unwrap().trim(); + + if file == "*" { + // if the sass open auto, we need auto-check the assets dir. + let asset_dir = config.asset_dir.clone(); + if asset_dir.is_dir() { + for entry in walkdir::WalkDir::new(&asset_dir) + .into_iter() + .filter_map(|e| e.ok()) + { + let temp = entry.path(); + if temp.is_file() { + let suffix = temp.extension().unwrap().to_str().unwrap(); + if suffix == "scss" { + // if file suffix is `scss` we need transform it. + let out_file = format!( + "{}.css", + temp.file_stem().unwrap().to_str().unwrap() + ); + let target_path = config + .out_dir + .join( + temp.strip_prefix(&asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); + sass.call( + "sass", + vec![ + temp.to_str().unwrap(), + target_path.to_str().unwrap(), + source_map, + ], + )?; + } + } } } - } - } - } else if tab.contains_key("files") && tab.get("files").unwrap().is_array() { - // check files list. - let list = tab.get("files").unwrap().as_array().unwrap(); - for i in list { - if i.is_str() { - let path = i.as_str().unwrap(); - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + } else { + // just transform one file. + let path = config.asset_dir.join(file); if path.is_file() { sass.call( "sass", @@ -422,10 +428,39 @@ fn build_assets(config: &CrateConfig) -> Result<()> { )?; } } + } else if tab.get("files").unwrap().is_array() { + // check files list. + let list = tab.get("files").unwrap().as_array().unwrap(); + for i in list { + if i.is_str() { + let path = i.as_str().unwrap(); + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); + if path.is_file() { + sass.call( + "sass", + vec![ + path.to_str().unwrap(), + path.parent() + .unwrap() + .join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )) + .to_str() + .unwrap(), + source_map, + ], + )?; + } + } + } } } } } + // SASS END + Ok(()) } From ecb846dd24d34c0827bfa3503048bf97a8389fda Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 23:00:39 +0800 Subject: [PATCH 10/26] Rename `files` to `input` --- src/assets/dioxus.toml | 12 ++++++------ src/builder.rs | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index b481193a8..a5e849bfb 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -48,13 +48,13 @@ binaryen = { wasm_opt = true } [application.tools.sass] # auto will check the assets dirs, and auto to transform all scss file to css file. -auto = true +input = "*" # or you can specify some scss file -> css file -files = [ - # some sass file path - # this file will translate to `/css/test.css` - "/css/test.scss" -] +# input = [ +# # some sass file path +# # this file will translate to `/css/test.css` +# "/css/test.scss" +# ] source_map = true \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index 75c83990d..f9f0bd9bd 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -365,9 +365,9 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "--source-map" }; - if tab.contains_key("files") { - if tab.get("files").unwrap().is_str() { - let file = tab.get("files").unwrap().as_str().unwrap().trim(); + if tab.contains_key("input") { + if tab.get("input").unwrap().is_str() { + let file = tab.get("input").unwrap().as_str().unwrap().trim(); if file == "*" { // if the sass open auto, we need auto-check the assets dir. @@ -428,9 +428,9 @@ fn build_assets(config: &CrateConfig) -> Result<()> { )?; } } - } else if tab.get("files").unwrap().is_array() { + } else if tab.get("input").unwrap().is_array() { // check files list. - let list = tab.get("files").unwrap().as_array().unwrap(); + let list = tab.get("input").unwrap().as_array().unwrap(); for i in list { if i.is_str() { let path = i.as_str().unwrap(); From bf49d526d773cc87449ac343659609c52890f0ac Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 00:27:37 +0800 Subject: [PATCH 11/26] Fixed some problem --- src/builder.rs | 13 ++++++++++++- src/tools.rs | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index f9f0bd9bd..77f0838f0 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -409,7 +409,13 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } } else { // just transform one file. - let path = config.asset_dir.join(file); + let path = if &file[0..] == "/" { + &file[1..file.len() - 1] + } else { + file + }; + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); if path.is_file() { sass.call( "sass", @@ -434,6 +440,11 @@ fn build_assets(config: &CrateConfig) -> Result<()> { for i in list { if i.is_str() { let path = i.as_str().unwrap(); + let path = if &path[0..] == "/" { + &path[1..path.len() - 1] + } else { + path + }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { diff --git a/src/tools.rs b/src/tools.rs index ae4e58b79..2952a9614 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -207,7 +207,7 @@ impl Tool { } else { command.to_string() } - }, + } }; if !bin_path.join(&command_file).is_file() { @@ -232,7 +232,7 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { if !target.exists() { let _ = std::fs::create_dir_all(target)?; } - + for i in 0..zip.len() { let mut file = zip.by_index(i)?; if file.is_dir() { @@ -252,4 +252,4 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { } Ok(()) -} +} \ No newline at end of file From c6aa4e332df58c223416fafc62e729574ff8c564 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 10:01:02 +0800 Subject: [PATCH 12/26] Ignore `scss` file in out_dir --- src/builder.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 77f0838f0..25d087191 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -31,7 +31,7 @@ pub fn build(config: &CrateConfig) -> Result<()> { } = config; // start to build the assets - build_assets(config)?; + let ignore_files = build_assets(config)?; let t_start = std::time::Instant::now(); @@ -158,6 +158,13 @@ pub fn build(config: &CrateConfig) -> Result<()> { log::warn!("Error copying dir: {}", _e); } } + for ignore in &ignore_files { + let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); + let ignore = config.out_dir.join(ignore); + if ignore.is_file() { + std::fs::remove_file(ignore)?; + } + } } } } @@ -345,7 +352,11 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { // this function will build some assets file // like sass tool resources -fn build_assets(config: &CrateConfig) -> Result<()> { +// this function will return a array which file don't need copy to out_dir. +fn build_assets(config: &CrateConfig) -> Result> { + + let mut result = vec![]; + let dioxus_config = &config.dioxus_config; let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); @@ -395,14 +406,17 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), ) .join(out_file); - sass.call( + let res = sass.call( "sass", vec![ temp.to_str().unwrap(), target_path.to_str().unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(temp.to_path_buf()); + } } } } @@ -417,7 +431,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { - sass.call( + let res = sass.call( "sass", vec![ path.to_str().unwrap(), @@ -431,7 +445,10 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(path); + } } } } else if tab.get("input").unwrap().is_array() { @@ -448,7 +465,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { - sass.call( + let res = sass.call( "sass", vec![ path.to_str().unwrap(), @@ -462,7 +479,10 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(path); + } } } } @@ -472,7 +492,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } // SASS END - Ok(()) + Ok(result) } // use binary_install::{Cache, Download}; From dccd82091a0b499527cd435fccf1a088262235e1 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:12:23 +0800 Subject: [PATCH 13/26] Fixed `input` option error --- src/builder.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 25d087191..e1ed0a8f1 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -354,7 +354,6 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { // like sass tool resources // this function will return a array which file don't need copy to out_dir. fn build_assets(config: &CrateConfig) -> Result> { - let mut result = vec![]; let dioxus_config = &config.dioxus_config; @@ -430,19 +429,23 @@ fn build_assets(config: &CrateConfig) -> Result> { }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); + let out_file = + format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join( + path.strip_prefix(&config.asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); if path.is_file() { let res = sass.call( "sass", vec![ path.to_str().unwrap(), - path.parent() - .unwrap() - .join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )) - .to_str() - .unwrap(), + target_path.to_str().unwrap(), source_map, ], ); @@ -464,19 +467,23 @@ fn build_assets(config: &CrateConfig) -> Result> { }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); + let out_file = + format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join( + path.strip_prefix(&config.asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); if path.is_file() { let res = sass.call( "sass", vec![ path.to_str().unwrap(), - path.parent() - .unwrap() - .join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )) - .to_str() - .unwrap(), + target_path.to_str().unwrap(), source_map, ], ); From 065da8db6589a1c745392b3973bae127ebecb183 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:14:49 +0800 Subject: [PATCH 14/26] Add `sass` desktop build support --- src/builder.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index e1ed0a8f1..ae6e83605 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -177,6 +177,8 @@ pub fn build(config: &CrateConfig) -> Result<()> { pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { log::info!("🚅 Running build [Desktop] command..."); + let ignore_files = build_assets(config)?; + let mut cmd = Command::new("cargo"); cmd.current_dir(&config.crate_dir) .arg("build") @@ -261,6 +263,13 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { log::warn!("Error copying dir: {}", e); } } + for ignore in &ignore_files { + let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); + let ignore = config.out_dir.join(ignore); + if ignore.is_file() { + std::fs::remove_file(ignore)?; + } + } } } } From 60bb0d9eb9f6a6e10267df85840914afa04d460b Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:15:33 +0800 Subject: [PATCH 15/26] Format code --- src/cli/tool/mod.rs | 2 +- src/tools.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index 45c866c40..fde94b29a 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -27,7 +27,7 @@ impl Tool { } } Tool::AppPath {} => { - if let Some(v) = tools::tools_path().to_str() { + if let Some(v) = tools::tools_path().to_str() { println!("{}", v); } else { log::error!("Tools path get failed."); diff --git a/src/tools.rs b/src/tools.rs index 2952a9614..2ab358d6b 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -252,4 +252,4 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { } Ok(()) -} \ No newline at end of file +} From cb650bad8a726c50eacdf8d29669532a5fb75c42 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:47:49 +0800 Subject: [PATCH 16/26] Fixed some problem --- src/builder.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index ae6e83605..8f3823a65 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -431,24 +431,19 @@ fn build_assets(config: &CrateConfig) -> Result> { } } else { // just transform one file. - let path = if &file[0..] == "/" { - &file[1..file.len() - 1] + let relative_path = if &file[0..1] == "/" { + &file[1..file.len()] } else { file }; - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + let path = config.asset_dir.join(relative_path); let out_file = format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); let target_path = config .out_dir - .join( - path.strip_prefix(&config.asset_dir) - .unwrap() - .parent() - .unwrap(), - ) + .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); + println!("{:?} -- {:?}", target_path, path); if path.is_file() { let res = sass.call( "sass", @@ -460,6 +455,8 @@ fn build_assets(config: &CrateConfig) -> Result> { ); if res.is_ok() { result.push(path); + } else { + log::error!("{:?}", res); } } } @@ -469,23 +466,17 @@ fn build_assets(config: &CrateConfig) -> Result> { for i in list { if i.is_str() { let path = i.as_str().unwrap(); - let path = if &path[0..] == "/" { - &path[1..path.len() - 1] + let relative_path = if &path[0..1] == "/" { + &path[1..path.len()] } else { path }; - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + let path = config.asset_dir.join(relative_path); let out_file = format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); let target_path = config .out_dir - .join( - path.strip_prefix(&config.asset_dir) - .unwrap() - .parent() - .unwrap(), - ) + .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); if path.is_file() { let res = sass.call( From 7f829c4553eaf45eda2e9c7964ba28cac63723cb Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:50:01 +0800 Subject: [PATCH 17/26] Format code --- src/builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index 8f3823a65..e0d3ac285 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -443,7 +443,6 @@ fn build_assets(config: &CrateConfig) -> Result> { .out_dir .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); - println!("{:?} -- {:?}", target_path, path); if path.is_file() { let res = sass.call( "sass", From cc56dd19ddbdaf9578ee6f56df7a354bbd2d3bc6 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Thu, 12 May 2022 00:11:51 +0800 Subject: [PATCH 18/26] Add `sass` file extension --- src/builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index e0d3ac285..cdb431933 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -399,8 +399,8 @@ fn build_assets(config: &CrateConfig) -> Result> { let temp = entry.path(); if temp.is_file() { let suffix = temp.extension().unwrap().to_str().unwrap(); - if suffix == "scss" { - // if file suffix is `scss` we need transform it. + if suffix == "scss" || suffix == "sass" { + // if file suffix is `scss` / `sass` we need transform it. let out_file = format!( "{}.css", temp.file_stem().unwrap().to_str().unwrap() From 87e6ed0a3d13dfb023c185a249ceee3a7a22d9e8 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Thu, 12 May 2022 22:49:27 +0800 Subject: [PATCH 19/26] Fixed builder problem --- src/builder.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index cdb431933..4f6824780 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -398,7 +398,9 @@ fn build_assets(config: &CrateConfig) -> Result> { { let temp = entry.path(); if temp.is_file() { - let suffix = temp.extension().unwrap().to_str().unwrap(); + let suffix = temp.extension(); + if suffix.is_none() { continue; } + let suffix = suffix.unwrap().to_str().unwrap(); if suffix == "scss" || suffix == "sass" { // if file suffix is `scss` / `sass` we need transform it. let out_file = format!( From 51639af48230bb8a4bb7b50105c1bcd270ac0774 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Mon, 6 Jun 2022 00:16:15 +0800 Subject: [PATCH 20/26] feat: add `profile` flag --- src/builder.rs | 5 +++++ src/cli/build/mod.rs | 4 ++++ src/cli/cfg.rs | 8 ++++++++ src/cli/serve/mod.rs | 4 ++++ src/config.rs | 9 +++++++++ 5 files changed, 30 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index 4f6824780..31bb96f46 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -49,6 +49,11 @@ pub fn build(config: &CrateConfig) -> Result<()> { cmd.arg("--release"); } + if config.custom_profile.is_some() { + let custom_profile = config.custom_profile.as_ref().unwrap(); + cmd.arg(format!("--profile {}", custom_profile)); + } + match executable { ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), diff --git a/src/cli/build/mod.rs b/src/cli/build/mod.rs index 4c043091f..7f8c7b420 100644 --- a/src/cli/build/mod.rs +++ b/src/cli/build/mod.rs @@ -19,6 +19,10 @@ impl Build { crate_config.as_example(self.build.example.unwrap()); } + if self.build.profile.is_some() { + crate_config.set_profile(self.build.profile.unwrap()); + } + let platform = self.build.platform.unwrap_or_else(|| { crate_config .dioxus_config diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index f5ee45b87..11a1603c7 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -16,6 +16,10 @@ pub struct ConfigOptsBuild { #[clap(long)] pub example: Option, + /// Build with custom profile + #[clap(long)] + pub profile: Option, + /// Build platform: support Web & Desktop [default: "default_platform"] #[clap(long)] pub platform: Option, @@ -36,6 +40,10 @@ pub struct ConfigOptsServe { #[serde(default)] pub release: bool, + /// Build with custom profile + #[clap(long)] + pub profile: Option, + /// Build platform: support Web & Desktop [default: "default_platform"] #[clap(long)] pub platform: Option, diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 85bd92c74..1b79284b7 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -24,6 +24,10 @@ impl Serve { crate_config.as_example(self.serve.example.unwrap()); } + if self.serve.profile.is_some() { + crate_config.set_profile(self.serve.profile.unwrap()); + } + let platform = self.serve.platform.unwrap_or_else(|| { crate_config .dioxus_config diff --git a/src/config.rs b/src/config.rs index 783c72ccd..9679fadaa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -114,6 +114,7 @@ pub struct CrateConfig { pub executable: ExecutableType, pub dioxus_config: DioxusConfig, pub release: bool, + pub custom_profile: Option, } #[derive(Debug, Clone)] @@ -163,6 +164,8 @@ impl CrateConfig { let release = false; + let custom_profile = None; + Ok(Self { out_dir, crate_dir, @@ -173,6 +176,7 @@ impl CrateConfig { executable, release, dioxus_config, + custom_profile, }) } @@ -186,6 +190,11 @@ impl CrateConfig { self } + pub fn set_profile(&mut self, profile: String) -> &mut Self { + self.custom_profile = Some(profile); + self + } + // pub fn with_build_options(&mut self, options: &BuildOptions) { // if let Some(name) = &options.example { // self.as_example(name.clone()); From 6aa609144d4b8dde995749ad2de4aaa8d69f16f4 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Mon, 6 Jun 2022 08:46:16 +0800 Subject: [PATCH 21/26] feat: add `profile` flag --- src/builder.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index 31bb96f46..57738b1bc 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -51,7 +51,8 @@ pub fn build(config: &CrateConfig) -> Result<()> { if config.custom_profile.is_some() { let custom_profile = config.custom_profile.as_ref().unwrap(); - cmd.arg(format!("--profile {}", custom_profile)); + cmd.arg("--profile"); + cmd.arg(custom_profile); } match executable { @@ -194,6 +195,12 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { cmd.arg("--release"); } + if config.custom_profile.is_some() { + let custom_profile = config.custom_profile.as_ref().unwrap(); + cmd.arg("--profile"); + cmd.arg(custom_profile); + } + match &config.executable { crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), From d20e7ab50815014e67871ec2227413a085762690 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Mon, 6 Jun 2022 18:18:22 +0800 Subject: [PATCH 22/26] feat: add `features` flag --- src/builder.rs | 12 ++++++++++++ src/cli/cfg.rs | 8 ++++++++ src/config.rs | 9 ++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index 57738b1bc..46d4c15f5 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -55,6 +55,12 @@ pub fn build(config: &CrateConfig) -> Result<()> { cmd.arg(custom_profile); } + if config.features.is_some() { + let features_str = config.features.as_ref().unwrap().join(" "); + cmd.arg("--features"); + cmd.arg(features_str); + } + match executable { ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), @@ -201,6 +207,12 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { cmd.arg(custom_profile); } + if config.features.is_some() { + let features_str = config.features.as_ref().unwrap().join(" "); + cmd.arg("--features"); + cmd.arg(features_str); + } + match &config.executable { crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index 11a1603c7..b8305e3a8 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -23,6 +23,10 @@ pub struct ConfigOptsBuild { /// Build platform: support Web & Desktop [default: "default_platform"] #[clap(long)] pub platform: Option, + + /// Space separated list of features to activate + #[clap(long)] + pub features: Option>, } #[derive(Clone, Debug, Default, Deserialize, Parser)] @@ -47,6 +51,10 @@ pub struct ConfigOptsServe { /// Build platform: support Web & Desktop [default: "default_platform"] #[clap(long)] pub platform: Option, + + /// Space separated list of features to activate + #[clap(long)] + pub features: Option>, } /// Ensure the given value for `--public-url` is formatted correctly. diff --git a/src/config.rs b/src/config.rs index 9679fadaa..6804ae6cd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -115,6 +115,7 @@ pub struct CrateConfig { pub dioxus_config: DioxusConfig, pub release: bool, pub custom_profile: Option, + pub features: Option>, } #[derive(Debug, Clone)] @@ -163,8 +164,8 @@ impl CrateConfig { let executable = ExecutableType::Binary(output_filename); let release = false; - let custom_profile = None; + let features = None; Ok(Self { out_dir, @@ -177,6 +178,7 @@ impl CrateConfig { release, dioxus_config, custom_profile, + features }) } @@ -195,6 +197,11 @@ impl CrateConfig { self } + pub fn set_features(&mut self, features: Vec) -> &mut Self { + self.features = Some(features); + self + } + // pub fn with_build_options(&mut self, options: &BuildOptions) { // if let Some(name) = &options.example { // self.as_example(name.clone()); From 421d2ed2980d4e4d542f1b7b28c31e9ca279a563 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:48:14 +0800 Subject: [PATCH 23/26] feat: add `verbose` flag --- src/builder.rs | 6 ++++++ src/cli/build/mod.rs | 1 + src/cli/cfg.rs | 10 ++++++++++ src/cli/serve/mod.rs | 1 + src/config.rs | 10 +++++++++- 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index 46d4c15f5..02add84ad 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -48,6 +48,9 @@ pub fn build(config: &CrateConfig) -> Result<()> { if config.release { cmd.arg("--release"); } + if config.verbose { + cmd.arg("--verbose"); + } if config.custom_profile.is_some() { let custom_profile = config.custom_profile.as_ref().unwrap(); @@ -200,6 +203,9 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { if config.release { cmd.arg("--release"); } + if config.verbose { + cmd.arg("--verbose"); + } if config.custom_profile.is_some() { let custom_profile = config.custom_profile.as_ref().unwrap(); diff --git a/src/cli/build/mod.rs b/src/cli/build/mod.rs index 7f8c7b420..661f17a5a 100644 --- a/src/cli/build/mod.rs +++ b/src/cli/build/mod.rs @@ -14,6 +14,7 @@ impl Build { // change the release state. crate_config.with_release(self.build.release); + crate_config.with_verbose(self.build.verbose); if self.build.example.is_some() { crate_config.as_example(self.build.example.unwrap()); diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index b8305e3a8..26409c370 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -12,6 +12,11 @@ pub struct ConfigOptsBuild { #[serde(default)] pub release: bool, + // Use verbose output [default: false] + #[clap(long)] + #[serde(default)] + pub verbose: bool, + /// Build a example [default: ""] #[clap(long)] pub example: Option, @@ -44,6 +49,11 @@ pub struct ConfigOptsServe { #[serde(default)] pub release: bool, + // Use verbose output [default: false] + #[clap(long)] + #[serde(default)] + pub verbose: bool, + /// Build with custom profile #[clap(long)] pub profile: Option, diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 1b79284b7..d25aa5bad 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -19,6 +19,7 @@ impl Serve { // change the relase state. crate_config.with_release(self.serve.release); + crate_config.with_verbose(self.serve.verbose); if self.serve.example.is_some() { crate_config.as_example(self.serve.example.unwrap()); diff --git a/src/config.rs b/src/config.rs index 6804ae6cd..6e855002b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -114,6 +114,7 @@ pub struct CrateConfig { pub executable: ExecutableType, pub dioxus_config: DioxusConfig, pub release: bool, + pub verbose: bool, pub custom_profile: Option, pub features: Option>, } @@ -164,6 +165,7 @@ impl CrateConfig { let executable = ExecutableType::Binary(output_filename); let release = false; + let verbose = false; let custom_profile = None; let features = None; @@ -178,7 +180,8 @@ impl CrateConfig { release, dioxus_config, custom_profile, - features + features, + verbose, }) } @@ -192,6 +195,11 @@ impl CrateConfig { self } + pub fn with_verbose(&mut self, verbose: bool) -> &mut Self { + self.verbose = verbose; + self + } + pub fn set_profile(&mut self, profile: String) -> &mut Self { self.custom_profile = Some(profile); self From d1c619e5dc9409eb358dfaf141458a5b38edc02b Mon Sep 17 00:00:00 2001 From: xTeKc <81730792+xTeKc@users.noreply.github.com> Date: Sun, 12 Jun 2022 03:31:13 -0400 Subject: [PATCH 24/26] update edition --- rustfmt.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 9071fb4b5..39b98f125 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,8 +1,8 @@ version = "Two" -edition = "2018" +edition = "2021" imports_granularity = "Crate" #use_small_heuristics = "Max" #control_brace_style = "ClosingNextLine" normalize_comments = true -format_code_in_doc_comments = true \ No newline at end of file +format_code_in_doc_comments = true From f287e757611ac2a84a8622bcf9b4a3d3cf60294b Mon Sep 17 00:00:00 2001 From: xTeKc <81730792+xTeKc@users.noreply.github.com> Date: Sun, 12 Jun 2022 03:31:35 -0400 Subject: [PATCH 25/26] update edition --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c869267b1..24493e787 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "dioxus-cli" version = "0.1.4" authors = ["Jonathan Kelley"] -edition = "2018" +edition = "2021" description = "CLI tool for developing, testing, and publishing Dioxus apps" license = "MIT/Apache-2.0" From bfab711010dd4f508930c3ce6af8f39c65c6c025 Mon Sep 17 00:00:00 2001 From: xTeKc <81730792+xTeKc@users.noreply.github.com> Date: Sun, 12 Jun 2022 03:35:25 -0400 Subject: [PATCH 26/26] force closure to take ownership --- src/server/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 1df13ca6a..ada3e10c7 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -79,7 +79,7 @@ pub async fn startup(config: CrateConfig) -> Result<()> { let file_service_config = config.clone(); let file_service = ServiceBuilder::new() .and_then( - |response: Response| async move { + move |response: Response| async move { let response = if file_service_config .dioxus_config .web