From 66b832045ad436c4577990ef138407331d38d056 Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Mon, 24 Jan 2022 16:18:16 +0800 Subject: [PATCH] feat: add `config` command --- Dioxus.toml => src/assets/dioxus.toml | 18 +++++------ src/cli/config/mod.rs | 43 +++++++++++++++++++++++++++ src/cli/create/mod.rs | 5 ---- src/cli/mod.rs | 7 +++-- src/config.rs | 4 ++- src/main.rs | 6 ++++ 6 files changed, 63 insertions(+), 20 deletions(-) rename Dioxus.toml => src/assets/dioxus.toml (79%) create mode 100644 src/cli/config/mod.rs diff --git a/Dioxus.toml b/src/assets/dioxus.toml similarity index 79% rename from Dioxus.toml rename to src/assets/dioxus.toml index 86f52c349..717b070e2 100644 --- a/Dioxus.toml +++ b/src/assets/dioxus.toml @@ -1,7 +1,7 @@ [application] # App (Project) Name -name = "dioxus-cli" +name = "{{project-name}}" # Dioxus App Support Platform # desktop, web, mobile, ssr @@ -12,31 +12,27 @@ out_dir = "dist" [web.app] +# HTML title tag content +title = "dioxus | ⛺" + # resource (static) file folder public_dir = "public" [web.watcher] - watch_path = ["src"] # include `assets` in web platform [web.resource] # CSS style file -style = [ - "./my-style.css" -] +style = [] # Javascript code file -script = [ - "./app.js" -] +script = [] [web.resource.dev] # Javascript code file # serve: [dev-server] only -script = [ - "./debug.js" -] \ No newline at end of file +script = [] \ No newline at end of file diff --git a/src/cli/config/mod.rs b/src/cli/config/mod.rs new file mode 100644 index 000000000..5e903dc3a --- /dev/null +++ b/src/cli/config/mod.rs @@ -0,0 +1,43 @@ +use std::{fs::File, io::Write}; + +use serde::Deserialize; +use structopt::StructOpt; + +/// Build the Rust WASM app and all of its assets. +#[derive(Clone, Debug, Deserialize, StructOpt)] +#[structopt(name = "config")] +pub enum Config { + /// Init `Dioxus.toml` for project/folder. + Init { + /// Init project name + name: String, + + /// Cover old config + #[structopt(long)] + #[serde(default)] + force: bool, + }, +} + +impl Config { + pub fn config(self) -> anyhow::Result<()> { + let crate_root = crate::cargo::crate_root()?; + match self { + Config::Init { name, force } => { + let conf_path = crate_root.join("Dioxus.toml"); + if conf_path.is_file() && force == false { + log::warn!( + "config file `Dioxus.toml` already exist, use `--force` to overwrite it." + ); + return Ok(()); + } + let mut file = File::create(conf_path)?; + let content = String::from(include_str!("../../assets/dioxus.toml")) + .replace("{{project-name}}", &name); + file.write_all(content.as_bytes())?; + log::info!("🚩 Init config file completed."); + } + } + Ok(()) + } +} diff --git a/src/cli/create/mod.rs b/src/cli/create/mod.rs index 7cb2edbbe..42364ddab 100644 --- a/src/cli/create/mod.rs +++ b/src/cli/create/mod.rs @@ -22,11 +22,6 @@ pub struct Create { /// Template path #[structopt(default_value = "gh:dioxuslabs/dioxus-template", long)] template: String, - - /// Create a `lib` project, include a `hello-world` example. - #[structopt(long)] - #[serde(default)] - pub lib: bool, } impl Create { diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 91d561463..4a72e00ac 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -3,6 +3,7 @@ use structopt::StructOpt; pub mod build; pub mod cfg; pub mod clean; +pub mod config; pub mod create; pub mod serve; pub mod translate; @@ -36,8 +37,8 @@ pub enum Commands { Serve(serve::Serve), /// Init a new project for Dioxus. Create(create::Create), - // /// Clean output artifacts. + /// Clean output artifacts. Clean(clean::Clean), - // /// Trunk config controls. - // Config(config::Config), + /// Dioxus config file controls. + Config(config::Config), } diff --git a/src/config.rs b/src/config.rs index fa52ddcbc..fb26b50bf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,9 @@ impl DioxusConfig { let crate_dir = crate::cargo::crate_root()?; if !crate_dir.join("Dioxus.toml").is_file() { - return Err(crate::error::Error::Unique("Config file: `Dioxus.toml` not found.".into())); + return Err(crate::error::Error::Unique( + "Config file: `Dioxus.toml` not found.".into(), + )); } let mut dioxus_conf_file = File::open(crate_dir.join("Dioxus.toml"))?; diff --git a/src/main.rs b/src/main.rs index 20536048b..1ef3a08f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,12 @@ async fn main() -> Result<()> { log::error!("create error: {}", e); } } + + Commands::Config(opts) => { + if let Err(e) = opts.config() { + log::error!("config error: {}", e); + } + } } Ok(())