Feat: CLI Settings (#2424)
* feat: cli settings * revision: lower msrv * add previous line about hotreload settings just in case * Fix issue with open --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
2ef406982e
commit
9567674a63
13 files changed
+869
-673
No files matched your search
@@ -91,10 +91,16 @@ pub struct ConfigOptsServe {
|
||||
#[clap(flatten)]
|
||||
pub(crate) server_arguments: ServeArguments,
|
||||
|
||||
/// Open the app in the default browser [default: true]
|
||||
#[clap(long, default_value_t = false)]
|
||||
#[serde(default)]
|
||||
pub open: bool,
|
||||
// TODO: Somehow make this default to `true` if the flag was provided. e.g. `dx serve --open`
|
||||
// Currently it requires a value: `dx serve --open true`
|
||||
/// Open the app in the default browser [default: false - unless project or global settings are set]
|
||||
#[clap(long)]
|
||||
pub open: Option<bool>,
|
||||
|
||||
// TODO: See `open` field
|
||||
/// Enable full hot reloading for the app [default: true - unless project or global settings are set]
|
||||
#[clap(long, group = "release-incompatible")]
|
||||
pub hot_reload: Option<bool>,
|
||||
|
||||
/// Build a example [default: ""]
|
||||
#[clap(long)]
|
||||
@@ -136,9 +142,6 @@ pub struct ConfigOptsServe {
|
||||
require_equals(true),
|
||||
action = clap::ArgAction::Set,
|
||||
)]
|
||||
#[clap(group = "release-incompatible")]
|
||||
#[serde(default)]
|
||||
pub hot_reload: bool,
|
||||
|
||||
/// Set cross-origin-policy to same-origin [default: false]
|
||||
#[clap(name = "cross-origin-policy")]
|
||||
|
||||
@@ -24,6 +24,33 @@ pub enum Config {
|
||||
FormatPrint {},
|
||||
/// Create a custom html file.
|
||||
CustomHtml {},
|
||||
|
||||
/// Set global cli settings.
|
||||
SetGlobal { setting: Setting, value: Value },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, clap::ValueEnum)]
|
||||
pub enum Setting {
|
||||
/// Set the value of the always-hot-reload setting.
|
||||
AlwaysHotReload,
|
||||
/// Set the value of the always-open-browser setting.
|
||||
AlwaysOpenBrowser,
|
||||
}
|
||||
|
||||
// NOTE: Unsure of an alternative to get the desired behavior with clap, if it exists.
|
||||
#[derive(Debug, Clone, Deserialize, clap::ValueEnum)]
|
||||
pub enum Value {
|
||||
True,
|
||||
False,
|
||||
}
|
||||
|
||||
impl From<Value> for bool {
|
||||
fn from(value: Value) -> Self {
|
||||
match value {
|
||||
Value::True => true,
|
||||
Value::False => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -62,6 +89,20 @@ impl Config {
|
||||
file.write_all(content.as_bytes())?;
|
||||
tracing::info!("🚩 Create custom html file done.");
|
||||
}
|
||||
Config::SetGlobal { setting, value } => {
|
||||
let mut global_settings =
|
||||
dioxus_cli_config::CliSettings::from_global().unwrap_or_default();
|
||||
|
||||
match setting {
|
||||
Setting::AlwaysHotReload => {
|
||||
global_settings.always_hot_reload = Some(value.into())
|
||||
}
|
||||
Setting::AlwaysOpenBrowser => {
|
||||
global_settings.always_open_browser = Some(value.into())
|
||||
}
|
||||
}
|
||||
global_settings.save().unwrap();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,11 +16,26 @@ pub struct Serve {
|
||||
impl Serve {
|
||||
pub fn serve(self, bin: Option<PathBuf>) -> Result<()> {
|
||||
let mut crate_config = dioxus_cli_config::CrateConfig::new(bin)?;
|
||||
let serve_cfg = self.serve.clone();
|
||||
let mut serve_cfg = self.serve.clone();
|
||||
|
||||
// change the release state.
|
||||
let hot_reload = self.serve.hot_reload || crate_config.dioxus_config.application.hot_reload;
|
||||
crate_config.with_hot_reload(hot_reload);
|
||||
// Handle cli settings
|
||||
let cli_settings = crate_config.dioxus_config.cli_settings.clone().unwrap();
|
||||
|
||||
if serve_cfg.hot_reload.is_none() {
|
||||
// we're going to override the hot_reload setting in the project's cfg based on settings
|
||||
//
|
||||
// let hot_reload = self.serve.hot_reload || crate_config.dioxus_config.application.hot_reload;
|
||||
|
||||
let value = cli_settings.always_hot_reload.unwrap_or(true);
|
||||
serve_cfg.hot_reload = Some(value);
|
||||
crate_config.with_hot_reload(value);
|
||||
}
|
||||
|
||||
if serve_cfg.open.is_none() {
|
||||
serve_cfg.open = Some(cli_settings.always_open_browser.unwrap_or(false));
|
||||
}
|
||||
|
||||
// Set config settings
|
||||
crate_config.with_cross_origin_policy(self.serve.cross_origin_policy);
|
||||
crate_config.with_release(self.serve.release);
|
||||
crate_config.with_verbose(self.serve.verbose);
|
||||
|
||||
@@ -108,7 +108,7 @@ impl Platform for FullstackPlatform {
|
||||
let desktop =
|
||||
DesktopPlatform::start_with_options(build_result, &desktop_config, serve, desktop_env)?;
|
||||
|
||||
if serve.open {
|
||||
if serve.open.unwrap_or_default() {
|
||||
crate::server::web::open_browser(
|
||||
config,
|
||||
serve
|
||||
|
||||
@@ -90,7 +90,15 @@ pub fn serve(
|
||||
let router = setup_router(config.clone(), hot_reload_state).await?;
|
||||
|
||||
// Start server
|
||||
start_server(ip, port, router, opts.open, rustls_config, &config).await?;
|
||||
start_server(
|
||||
ip,
|
||||
port,
|
||||
router,
|
||||
opts.open.unwrap_or_default(),
|
||||
rustls_config,
|
||||
&config,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user