2
0
Fork 0
mirror of https://github.com/DioxusLabs/dioxus synced 2025-02-24 11:27:12 +00:00

Use serde default instead of options in the CLI

This commit is contained in:
Evan Almloff 2023-08-08 15:33:48 -07:00
parent 51b62dd33a
commit 8b4e0b3e07
8 changed files with 100 additions and 122 deletions

View file

@ -130,7 +130,7 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<BuildResult> {
}
// check binaryen:wasm-opt tool
let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default();
let dioxus_tools = dioxus_config.application.tools.clone();
if dioxus_tools.contains_key("binaryen") {
let info = dioxus_tools.get("binaryen").unwrap();
let binaryen = crate::tools::Tool::Binaryen;
@ -346,13 +346,7 @@ pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result<BuildResul
log::info!(
"🚩 Build completed: [./{}]",
config
.dioxus_config
.application
.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from("dist"))
.display()
config.dioxus_config.application.out_dir.clone().display()
);
println!("build desktop done");
@ -446,8 +440,8 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
let mut script_list = resouces.script.unwrap_or_default();
if serve {
let mut dev_style = resouces.dev.style.clone().unwrap_or_default();
let mut dev_script = resouces.dev.script.unwrap_or_default();
let mut dev_style = resouces.dev.style.clone();
let mut dev_script = resouces.dev.script;
style_list.append(&mut dev_style);
script_list.append(&mut dev_script);
}
@ -459,13 +453,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
&style.to_str().unwrap(),
))
}
if config
.application
.tools
.clone()
.unwrap_or_default()
.contains_key("tailwindcss")
{
if config.application.tools.clone().contains_key("tailwindcss") {
style_str.push_str("<link rel=\"stylesheet\" href=\"tailwind.css\">\n");
}
@ -516,12 +504,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
);
}
let title = config
.web
.app
.title
.clone()
.unwrap_or_else(|| "dioxus | ⛺".into());
let title = config.web.app.title.clone();
replace_or_insert_before("{app_title}", &title, "</title", &mut html);
@ -548,7 +531,7 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
let mut result = vec![];
let dioxus_config = &config.dioxus_config;
let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default();
let dioxus_tools = dioxus_config.application.tools.clone();
// check sass tool state
let sass = Tool::Sass;

View file

@ -54,14 +54,7 @@ impl Build {
let mut file = std::fs::File::create(
crate_config
.crate_dir
.join(
crate_config
.dioxus_config
.application
.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from("dist")),
)
.join(crate_config.dioxus_config.application.out_dir.clone())
.join("index.html"),
)?;
file.write_all(temp.as_bytes())?;

View file

@ -19,11 +19,7 @@ impl Clean {
return custom_error!("Cargo clean failed.");
}
let out_dir = crate_config
.dioxus_config
.application
.out_dir
.unwrap_or_else(|| PathBuf::from("dist"));
let out_dir = crate_config.dioxus_config.application.out_dir;
if crate_config.crate_dir.join(&out_dir).is_dir() {
remove_dir_all(crate_config.crate_dir.join(&out_dir))?;
}

View file

@ -58,14 +58,9 @@ impl Serve {
pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> {
let serve_html = gen_page(&crate_config.dioxus_config, true);
let dist_path = crate_config.crate_dir.join(
crate_config
.dioxus_config
.application
.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from("dist")),
);
let dist_path = crate_config
.crate_dir
.join(crate_config.dioxus_config.application.out_dir.clone());
if !dist_path.is_dir() {
create_dir_all(&dist_path)?;
}

View file

@ -86,33 +86,33 @@ fn acquire_dioxus_toml(dir: &Path) -> Option<PathBuf> {
impl Default for DioxusConfig {
fn default() -> Self {
let name = "name";
let name = default_name();
Self {
application: ApplicationConfig {
name: name.into(),
default_platform: Platform::Web,
out_dir: Some(PathBuf::from("dist")),
asset_dir: Some(PathBuf::from("public")),
name: name.clone(),
default_platform: default_platform(),
out_dir: out_dir_default(),
asset_dir: asset_dir_default(),
tools: None,
tools: Default::default(),
sub_package: None,
},
web: WebConfig {
app: WebAppConfig {
title: Some("dioxus | ⛺".into()),
title: default_title(),
base_path: None,
},
proxy: Some(vec![]),
proxy: vec![],
watcher: WebWatcherConfig {
watch_path: Some(vec![PathBuf::from("src")]),
reload_html: Some(false),
index_on_404: Some(true),
watch_path: watch_path_default(),
reload_html: false,
index_on_404: true,
},
resource: WebResourceConfig {
dev: WebDevResourceConfig {
style: Some(vec![]),
script: Some(vec![]),
style: vec![],
script: vec![],
},
style: Some(vec![]),
script: Some(vec![]),
@ -136,20 +136,44 @@ impl Default for DioxusConfig {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationConfig {
#[serde(default = "default_name")]
pub name: String,
#[serde(default = "default_platform")]
pub default_platform: Platform,
pub out_dir: Option<PathBuf>,
pub asset_dir: Option<PathBuf>,
#[serde(default = "out_dir_default")]
pub out_dir: PathBuf,
#[serde(default = "asset_dir_default")]
pub asset_dir: PathBuf,
pub tools: Option<HashMap<String, toml::Value>>,
#[serde(default)]
pub tools: HashMap<String, toml::Value>,
#[serde(default)]
pub sub_package: Option<String>,
}
fn default_name() -> String {
"name".into()
}
fn default_platform() -> Platform {
Platform::Web
}
fn asset_dir_default() -> PathBuf {
PathBuf::from("public")
}
fn out_dir_default() -> PathBuf {
PathBuf::from("dist")
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebConfig {
#[serde(default)]
pub app: WebAppConfig,
pub proxy: Option<Vec<WebProxyConfig>>,
#[serde(default)]
pub proxy: Vec<WebProxyConfig>,
pub watcher: WebWatcherConfig,
pub resource: WebResourceConfig,
#[serde(default)]
@ -158,10 +182,24 @@ pub struct WebConfig {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebAppConfig {
pub title: Option<String>,
#[serde(default = "default_title")]
pub title: String,
pub base_path: Option<String>,
}
impl Default for WebAppConfig {
fn default() -> Self {
Self {
title: default_title(),
base_path: None,
}
}
}
fn default_title() -> String {
"dioxus | ⛺".into()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebProxyConfig {
pub backend: String,
@ -169,9 +207,16 @@ pub struct WebProxyConfig {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebWatcherConfig {
pub watch_path: Option<Vec<PathBuf>>,
pub reload_html: Option<bool>,
pub index_on_404: Option<bool>,
#[serde(default = "watch_path_default")]
pub watch_path: Vec<PathBuf>,
#[serde(default)]
pub reload_html: bool,
#[serde(default = "true_bool")]
pub index_on_404: bool,
}
fn watch_path_default() -> Vec<PathBuf> {
vec![PathBuf::from("src")]
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -183,8 +228,10 @@ pub struct WebResourceConfig {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebDevResourceConfig {
pub style: Option<Vec<PathBuf>>,
pub script: Option<Vec<PathBuf>>,
#[serde(default)]
pub style: Vec<PathBuf>,
#[serde(default)]
pub script: Vec<PathBuf>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -238,17 +285,11 @@ impl CrateConfig {
let workspace_dir = meta.workspace_root;
let target_dir = meta.target_directory;
let out_dir = match dioxus_config.application.out_dir {
Some(ref v) => crate_dir.join(v),
None => crate_dir.join("dist"),
};
let out_dir = crate_dir.join(&dioxus_config.application.out_dir);
let cargo_def = &crate_dir.join("Cargo.toml");
let asset_dir = match dioxus_config.application.asset_dir {
Some(ref v) => crate_dir.join(v),
None => crate_dir.join("public"),
};
let asset_dir = crate_dir.join(&dioxus_config.application.asset_dir);
let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap();
@ -577,3 +618,7 @@ impl Default for WebviewInstallMode {
Self::OfflineInstaller { silent: false }
}
}
fn true_bool() -> bool {
true
}

View file

@ -5,10 +5,7 @@ use dioxus_core::Template;
use dioxus_html::HtmlCtx;
use dioxus_rsx::hot_reload::*;
use notify::{RecommendedWatcher, Watcher};
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};
use tokio::sync::broadcast::Sender;
mod output;
@ -25,13 +22,7 @@ async fn setup_file_watcher<F: Fn() -> Result<BuildResult> + Send + 'static>(
let mut last_update_time = chrono::Local::now().timestamp();
// file watcher: check file change
let allow_watch_path = config
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or_else(|| vec![PathBuf::from("src")]);
let allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone();
let watcher_config = config.clone();
let mut watcher = notify::recommended_watcher(move |info: notify::Result<notify::Event>| {
@ -87,13 +78,7 @@ async fn setup_file_watcher_hot_reload<F: Fn() -> Result<BuildResult> + Send + '
web_info: Option<WebServerInfo>,
) -> Result<RecommendedWatcher> {
// file watcher: check file change
let allow_watch_path = config
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or_else(|| vec![PathBuf::from("src")]);
let allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone();
let watcher_config = config.clone();
let mut last_update_time = chrono::Local::now().timestamp();

View file

@ -46,19 +46,13 @@ pub fn print_console_info(
} else {
"Default"
};
let url_rewrite = if config
.dioxus_config
.web
.watcher
.index_on_404
.unwrap_or(false)
{
let url_rewrite = if config.dioxus_config.web.watcher.index_on_404 {
"True"
} else {
"False"
};
let proxies = config.dioxus_config.web.proxy.as_ref();
let proxies = &config.dioxus_config.web.proxy;
if options.changed.is_empty() {
println!(
@ -106,12 +100,10 @@ pub fn print_console_info(
println!();
println!("\t> Profile : {}", profile.green());
println!("\t> Hot Reload : {}", hot_reload.cyan());
if let Some(proxies) = proxies {
if !proxies.is_empty() {
println!("\t> Proxies :");
for proxy in proxies {
println!("\t\t- {}", proxy.backend.blue());
}
if !proxies.is_empty() {
println!("\t> Proxies :");
for proxy in proxies {
println!("\t\t- {}", proxy.backend.blue());
}
}
println!("\t> Index Template : {}", custom_html_file.green());

View file

@ -322,12 +322,7 @@ async fn setup_router(
.override_response_header(HeaderName::from_static("cross-origin-opener-policy"), coop)
.and_then(
move |response: Response<ServeFileSystemResponseBody>| async move {
let response = if file_service_config
.dioxus_config
.web
.watcher
.index_on_404
.unwrap_or(false)
let response = if file_service_config.dioxus_config.web.watcher.index_on_404
&& response.status() == StatusCode::NOT_FOUND
{
let body = Full::from(
@ -359,7 +354,7 @@ async fn setup_router(
let mut router = Router::new().route("/_dioxus/ws", get(ws_handler));
// Setup proxy
for proxy_config in config.dioxus_config.web.proxy.unwrap_or_default() {
for proxy_config in config.dioxus_config.web.proxy {
router = proxy::add_proxy(router, &proxy_config)?;
}
@ -476,13 +471,7 @@ fn build(config: &CrateConfig, reload_tx: &Sender<()>) -> Result<BuildResult> {
let result = builder::build(config, true)?;
// change the websocket reload state to true;
// the page will auto-reload.
if config
.dioxus_config
.web
.watcher
.reload_html
.unwrap_or(false)
{
if config.dioxus_config.web.watcher.reload_html {
let _ = Serve::regen_dev_page(config);
}
let _ = reload_tx.send(());