mod.rs 9.3 KB

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