Simplify cli-config, hotreload -> devtools (drop to 0 deps, fast compile times) (#2975)
* simplify cli-config crate * clean up configs * add devtools crate, update cargo imports * fix serve addr, fix websocket proxy issue * add comment about opt profiles * rename hot-reload to devtools
This commit is contained in:
1 parent
8203ee571f
commit
3fb1f739d1
58 files changed
+1131
-1654
No files matched your search
@@ -1,99 +1,73 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
|
||||
#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
mod config;
|
||||
pub use config::*;
|
||||
pub const CLI_ENABLED_ENV: &str = "DIOXUS_CLI_ENABLED";
|
||||
pub const SERVER_IP_ENV: &str = "IP";
|
||||
pub const SERVER_PORT_ENV: &str = "PORT";
|
||||
pub const DEVSERVER_RAW_ADDR_ENV: &str = "DIOXUS_DEVSERVER_ADDR";
|
||||
pub const ALWAYS_ON_TOP_ENV: &str = "DIOXUS_ALWAYS_ON_TOP";
|
||||
pub const ASSET_ROOT_ENV: &str = "DIOXUS_ASSET_ROOT";
|
||||
pub const APP_TITLE_ENV: &str = "DIOXUS_APP_TITLE";
|
||||
pub const OUT_DIR: &str = "DIOXUS_OUT_DIR";
|
||||
|
||||
mod bundle;
|
||||
pub use bundle::*;
|
||||
/// todo: this is not implemented but we're going to reserve this
|
||||
///
|
||||
/// technically this is only passed on "launch" so if you close the app, this will be lost
|
||||
pub const IOS_DEVSERVER_ADDR_ENV: &str = "SIMCTL_CHILD_DIOXUS_DEVSERVER_ADDR";
|
||||
|
||||
mod serve;
|
||||
pub use serve::*;
|
||||
|
||||
mod build_info;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod __private {
|
||||
use crate::DioxusConfig;
|
||||
|
||||
pub(crate) const DIOXUS_CLI_VERSION: &str = "DIOXUS_CLI_VERSION";
|
||||
pub(crate) const CONFIG_ENV: &str = "DIOXUS_CONFIG";
|
||||
pub(crate) const CONFIG_BASE_PATH_ENV: &str = "DIOXUS_CONFIG_BASE_PATH";
|
||||
|
||||
pub fn save_config(config: &DioxusConfig, cli_version: &str) -> CrateConfigDropGuard {
|
||||
std::env::set_var(CONFIG_ENV, serde_json::to_string(config).unwrap());
|
||||
std::env::set_var(
|
||||
CONFIG_BASE_PATH_ENV,
|
||||
config.web.app.base_path.clone().unwrap_or_default(),
|
||||
);
|
||||
std::env::set_var(DIOXUS_CLI_VERSION, cli_version);
|
||||
CrateConfigDropGuard
|
||||
}
|
||||
|
||||
/// A guard that removes the config from the environment when dropped.
|
||||
pub struct CrateConfigDropGuard;
|
||||
|
||||
impl Drop for CrateConfigDropGuard {
|
||||
fn drop(&mut self) {
|
||||
std::env::remove_var(CONFIG_ENV);
|
||||
std::env::remove_var(CONFIG_BASE_PATH_ENV);
|
||||
std::env::remove_var(DIOXUS_CLI_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "read-config")]
|
||||
/// The environment variable that stores the CLIs serve configuration.
|
||||
/// We use this to communicate between the CLI and the server for fullstack applications.
|
||||
pub const SERVE_ENV: &str = "DIOXUS_SERVE_CONFIG";
|
||||
/// Get the address of the devserver for use over a raw socket
|
||||
///
|
||||
/// This is not a websocket! There's no protocol!
|
||||
pub fn devserver_raw_addr() -> Option<SocketAddr> {
|
||||
std::env::var(DEVSERVER_RAW_ADDR_ENV)
|
||||
.map(|s| s.parse().ok())
|
||||
.ok()
|
||||
.flatten()
|
||||
}
|
||||
|
||||
/// An error that occurs when the dioxus CLI was not used to build the application.
|
||||
#[derive(Debug)]
|
||||
pub struct DioxusCLINotUsed;
|
||||
|
||||
impl std::fmt::Display for DioxusCLINotUsed {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("dioxus CLI was not used to build the application")
|
||||
}
|
||||
pub fn devserver_ws_endpoint() -> Option<String> {
|
||||
let addr = devserver_raw_addr()?;
|
||||
Some(format!("ws://{addr}/_dioxus"))
|
||||
}
|
||||
|
||||
impl std::error::Error for DioxusCLINotUsed {}
|
||||
pub fn server_ip() -> Option<IpAddr> {
|
||||
std::env::var(SERVER_IP_ENV)
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
}
|
||||
|
||||
#[cfg(feature = "read-config")]
|
||||
/// The current crate's configuration.
|
||||
pub static CURRENT_CONFIG: once_cell::sync::Lazy<
|
||||
Result<crate::config::DioxusConfig, DioxusCLINotUsed>,
|
||||
> = once_cell::sync::Lazy::new(|| {
|
||||
CURRENT_CONFIG_JSON
|
||||
.ok_or_else(|| {
|
||||
tracing::warn!("A library is trying to access the crate's configuration, but the dioxus CLI was not used to build the application.");
|
||||
DioxusCLINotUsed
|
||||
}).and_then(
|
||||
|config|
|
||||
match serde_json::from_str(config) {
|
||||
Ok(config) => Ok(config),
|
||||
Err(err) => {
|
||||
let mut cli_version = crate::build_info::PKG_VERSION.to_string();
|
||||
pub fn server_port() -> Option<u16> {
|
||||
std::env::var(SERVER_PORT_ENV)
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
}
|
||||
|
||||
if let Some(hash) = crate::build_info::GIT_COMMIT_HASH_SHORT {
|
||||
let hash = &hash.trim_start_matches('g')[..4];
|
||||
cli_version.push_str(&format!("-{hash}"));
|
||||
}
|
||||
pub fn fullstack_address_or_localhost() -> SocketAddr {
|
||||
let ip = server_ip().unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
|
||||
let port = server_port().unwrap_or(8080);
|
||||
SocketAddr::new(ip, port)
|
||||
}
|
||||
|
||||
let dioxus_version = std::option_env!("DIOXUS_CLI_VERSION").unwrap_or("unknown");
|
||||
pub fn app_title() -> Option<String> {
|
||||
std::env::var(APP_TITLE_ENV).ok()
|
||||
}
|
||||
|
||||
tracing::warn!("Failed to parse the CLI config file. This is likely caused by a mismatch between the version of the CLI and the dioxus version.\nCLI version: {cli_version}\nDioxus version: {dioxus_version}\nSerialization error: {err}");
|
||||
Err(DioxusCLINotUsed)
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
pub fn always_on_top() -> Option<bool> {
|
||||
std::env::var(ALWAYS_ON_TOP_ENV)
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
}
|
||||
|
||||
#[cfg(feature = "read-config")]
|
||||
/// The current crate's configuration.
|
||||
pub const CURRENT_CONFIG_JSON: Option<&str> = std::option_env!("DIOXUS_CONFIG");
|
||||
pub fn is_cli_enabled() -> bool {
|
||||
std::env::var(CLI_ENABLED_ENV).is_ok()
|
||||
}
|
||||
|
||||
#[cfg(feature = "read-config")]
|
||||
/// The current crate's configuration.
|
||||
pub const BASE_PATH: Option<&str> = std::option_env!("DIOXUS_CONFIG_BASE_PATH");
|
||||
pub fn base_path() -> Option<PathBuf> {
|
||||
std::env::var("DIOXUS_ASSET_ROOT").ok().map(PathBuf::from)
|
||||
}
|
||||
|
||||
pub fn out_dir() -> Option<PathBuf> {
|
||||
std::env::var(OUT_DIR).ok().map(PathBuf::from)
|
||||
}
|
||||
Reference in new issue
Block a user