mod.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. use crate::server::Platform;
  2. use crate::{
  3. cfg::ConfigOptsServe,
  4. server::{
  5. output::{print_console_info, PrettierOptions},
  6. setup_file_watcher,
  7. },
  8. BuildResult, Result,
  9. };
  10. use dioxus_cli_config::CrateConfig;
  11. use dioxus_hot_reload::HotReloadMsg;
  12. use dioxus_html::HtmlCtx;
  13. use dioxus_rsx::hot_reload::*;
  14. use interprocess_docfix::local_socket::LocalSocketListener;
  15. use std::fs::create_dir_all;
  16. use std::{
  17. process::{Child, Command},
  18. sync::{Arc, Mutex, RwLock},
  19. };
  20. use tokio::sync::broadcast::{self};
  21. #[cfg(feature = "plugin")]
  22. use crate::plugin::PluginManager;
  23. use super::HotReloadState;
  24. pub async fn startup(config: CrateConfig, serve: &ConfigOptsServe) -> Result<()> {
  25. startup_with_platform::<DesktopPlatform>(config, serve).await
  26. }
  27. pub(crate) async fn startup_with_platform<P: Platform + Send + 'static>(
  28. config: CrateConfig,
  29. serve_cfg: &ConfigOptsServe,
  30. ) -> Result<()> {
  31. // ctrl-c shutdown checker
  32. let _crate_config = config.clone();
  33. let _ = ctrlc::set_handler(move || {
  34. #[cfg(feature = "plugin")]
  35. let _ = PluginManager::on_serve_shutdown(&_crate_config);
  36. std::process::exit(0);
  37. });
  38. let hot_reload_state = match config.hot_reload {
  39. true => {
  40. let FileMapBuildResult { map, errors } =
  41. FileMap::<HtmlCtx>::create(config.crate_dir.clone()).unwrap();
  42. for err in errors {
  43. log::error!("{}", err);
  44. }
  45. let file_map = Arc::new(Mutex::new(map));
  46. let hot_reload_tx = broadcast::channel(100).0;
  47. Some(HotReloadState {
  48. messages: hot_reload_tx.clone(),
  49. file_map: file_map.clone(),
  50. })
  51. }
  52. false => None,
  53. };
  54. serve::<P>(config, serve_cfg, hot_reload_state).await?;
  55. Ok(())
  56. }
  57. /// Start the server without hot reload
  58. async fn serve<P: Platform + Send + 'static>(
  59. config: CrateConfig,
  60. serve: &ConfigOptsServe,
  61. hot_reload_state: Option<HotReloadState>,
  62. ) -> Result<()> {
  63. let platform = RwLock::new(P::start(&config, serve)?);
  64. log::info!("🚀 Starting development server...");
  65. // We got to own watcher so that it exists for the duration of serve
  66. // Otherwise full reload won't work.
  67. let _watcher = setup_file_watcher(
  68. {
  69. let config = config.clone();
  70. move || platform.write().unwrap().rebuild(&config)
  71. },
  72. &config,
  73. None,
  74. hot_reload_state.clone(),
  75. )
  76. .await?;
  77. match hot_reload_state {
  78. Some(hot_reload_state) => {
  79. // The open interprocess sockets
  80. start_desktop_hot_reload(hot_reload_state).await?;
  81. }
  82. None => {
  83. std::future::pending::<()>().await;
  84. }
  85. }
  86. Ok(())
  87. }
  88. async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()> {
  89. let metadata = cargo_metadata::MetadataCommand::new()
  90. .no_deps()
  91. .exec()
  92. .unwrap();
  93. let target_dir = metadata.target_directory.as_std_path();
  94. let _ = create_dir_all(target_dir); // `_all` is for good measure and future-proofness.
  95. let path = target_dir.join("dioxusin");
  96. clear_paths(&path);
  97. match LocalSocketListener::bind(path) {
  98. Ok(local_socket_stream) => {
  99. let aborted = Arc::new(Mutex::new(false));
  100. // States
  101. // The open interprocess sockets
  102. let channels = Arc::new(Mutex::new(Vec::new()));
  103. // listen for connections
  104. std::thread::spawn({
  105. let file_map = hot_reload_state.file_map.clone();
  106. let channels = channels.clone();
  107. let aborted = aborted.clone();
  108. move || {
  109. loop {
  110. //accept() will block the thread when local_socket_stream is in blocking mode (default)
  111. match local_socket_stream.accept() {
  112. Ok(mut connection) => {
  113. // send any templates than have changed before the socket connected
  114. let templates: Vec<_> = {
  115. file_map
  116. .lock()
  117. .unwrap()
  118. .map
  119. .values()
  120. .filter_map(|(_, template_slot)| *template_slot)
  121. .collect()
  122. };
  123. for template in templates {
  124. if !send_msg(
  125. HotReloadMsg::UpdateTemplate(template),
  126. &mut connection,
  127. ) {
  128. continue;
  129. }
  130. }
  131. channels.lock().unwrap().push(connection);
  132. println!("Connected to hot reloading 🚀");
  133. }
  134. Err(err) => {
  135. let error_string = err.to_string();
  136. // Filter out any error messages about a operation that may block and an error message that triggers on some operating systems that says "Waiting for a process to open the other end of the pipe" without WouldBlock being set
  137. let display_error = err.kind() != std::io::ErrorKind::WouldBlock
  138. && !error_string.contains("Waiting for a process");
  139. if display_error {
  140. println!("Error connecting to hot reloading: {} (Hot reloading is a feature of the dioxus-cli. If you are not using the CLI, this error can be ignored)", err);
  141. }
  142. }
  143. }
  144. if *aborted.lock().unwrap() {
  145. break;
  146. }
  147. }
  148. }
  149. });
  150. let mut hot_reload_rx = hot_reload_state.messages.subscribe();
  151. while let Ok(template) = hot_reload_rx.recv().await {
  152. let channels = &mut *channels.lock().unwrap();
  153. let mut i = 0;
  154. while i < channels.len() {
  155. let channel = &mut channels[i];
  156. if send_msg(HotReloadMsg::UpdateTemplate(template), channel) {
  157. i += 1;
  158. } else {
  159. channels.remove(i);
  160. }
  161. }
  162. }
  163. }
  164. Err(error) => println!("failed to connect to hot reloading\n{error}"),
  165. }
  166. Ok(())
  167. }
  168. fn clear_paths(file_socket_path: &std::path::Path) {
  169. if cfg!(unix) {
  170. // On unix, if you force quit the application, it can leave the file socket open
  171. // This will cause the local socket listener to fail to open
  172. // We check if the file socket is already open from an old session and then delete it
  173. if file_socket_path.exists() {
  174. let _ = std::fs::remove_file(file_socket_path);
  175. }
  176. }
  177. }
  178. fn send_msg(msg: HotReloadMsg, channel: &mut impl std::io::Write) -> bool {
  179. if let Ok(msg) = serde_json::to_string(&msg) {
  180. if channel.write_all(msg.as_bytes()).is_err() {
  181. return false;
  182. }
  183. if channel.write_all(&[b'\n']).is_err() {
  184. return false;
  185. }
  186. true
  187. } else {
  188. false
  189. }
  190. }
  191. fn start_desktop(
  192. config: &CrateConfig,
  193. skip_assets: bool,
  194. rust_flags: Option<String>,
  195. ) -> Result<(RAIIChild, BuildResult)> {
  196. // Run the desktop application
  197. // Only used for the fullstack platform,
  198. let result = crate::builder::build_desktop(config, true, skip_assets, rust_flags)?;
  199. let active = "DIOXUS_ACTIVE";
  200. let child = RAIIChild(
  201. Command::new(
  202. result
  203. .executable
  204. .clone()
  205. .ok_or(anyhow::anyhow!("No executable found after desktop build"))?,
  206. )
  207. .env(active, "true")
  208. .spawn()?,
  209. );
  210. Ok((child, result))
  211. }
  212. pub(crate) struct DesktopPlatform {
  213. currently_running_child: RAIIChild,
  214. skip_assets: bool,
  215. }
  216. impl DesktopPlatform {
  217. /// `rust_flags` argument is added because it is used by the
  218. /// `DesktopPlatform`'s implementation of the `Platform::start()`.
  219. pub fn start_with_options(
  220. config: &CrateConfig,
  221. serve: &ConfigOptsServe,
  222. rust_flags: Option<String>,
  223. ) -> Result<Self> {
  224. let (child, first_build_result) = start_desktop(config, serve.skip_assets, rust_flags)?;
  225. log::info!("🚀 Starting development server...");
  226. // Print serve info
  227. print_console_info(
  228. config,
  229. PrettierOptions {
  230. changed: vec![],
  231. warnings: first_build_result.warnings,
  232. elapsed_time: first_build_result.elapsed_time,
  233. },
  234. None,
  235. );
  236. Ok(Self {
  237. currently_running_child: child,
  238. skip_assets: serve.skip_assets,
  239. })
  240. }
  241. /// `rust_flags` argument is added because it is used by the
  242. /// `DesktopPlatform`'s implementation of the `Platform::rebuild()`.
  243. pub fn rebuild_with_options(
  244. &mut self,
  245. config: &CrateConfig,
  246. rust_flags: Option<String>,
  247. ) -> Result<BuildResult> {
  248. self.currently_running_child.0.kill()?;
  249. let (child, result) = start_desktop(config, self.skip_assets, rust_flags)?;
  250. self.currently_running_child = child;
  251. Ok(result)
  252. }
  253. }
  254. impl Platform for DesktopPlatform {
  255. fn start(config: &CrateConfig, serve: &ConfigOptsServe) -> Result<Self> {
  256. // See `start_with_options()`'s docs for the explanation why the code
  257. // was moved there.
  258. // Since desktop platform doesn't use `rust_flags`, this argument is
  259. // explicitly set to `None`.
  260. DesktopPlatform::start_with_options(config, serve, None)
  261. }
  262. fn rebuild(&mut self, config: &CrateConfig) -> Result<BuildResult> {
  263. // See `rebuild_with_options()`'s docs for the explanation why the code
  264. // was moved there.
  265. // Since desktop platform doesn't use `rust_flags`, this argument is
  266. // explicitly set to `None`.
  267. DesktopPlatform::rebuild_with_options(self, config, None)
  268. }
  269. }
  270. struct RAIIChild(Child);
  271. impl Drop for RAIIChild {
  272. fn drop(&mut self) {
  273. let _ = self.0.kill();
  274. }
  275. }