mod.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. use crate::{
  2. server::{
  3. output::{print_console_info, PrettierOptions},
  4. setup_file_watcher,
  5. },
  6. BuildResult, CrateConfig, Result,
  7. };
  8. use dioxus_hot_reload::HotReloadMsg;
  9. use dioxus_html::HtmlCtx;
  10. use dioxus_rsx::hot_reload::*;
  11. use interprocess_docfix::local_socket::LocalSocketListener;
  12. use std::{
  13. process::{Child, Command},
  14. sync::{Arc, Mutex, RwLock},
  15. };
  16. use tokio::sync::broadcast::{self};
  17. #[cfg(feature = "plugin")]
  18. use plugin::PluginManager;
  19. use super::HotReloadState;
  20. pub async fn startup(config: CrateConfig) -> Result<()> {
  21. // ctrl-c shutdown checker
  22. let _crate_config = config.clone();
  23. let _ = ctrlc::set_handler(move || {
  24. #[cfg(feature = "plugin")]
  25. let _ = PluginManager::on_serve_shutdown(&_crate_config);
  26. std::process::exit(0);
  27. });
  28. let hot_reload_state = match config.hot_reload {
  29. true => {
  30. let FileMapBuildResult { map, errors } =
  31. FileMap::<HtmlCtx>::create(config.crate_dir.clone()).unwrap();
  32. for err in errors {
  33. log::error!("{}", err);
  34. }
  35. let file_map = Arc::new(Mutex::new(map));
  36. let hot_reload_tx = broadcast::channel(100).0;
  37. clear_paths();
  38. Some(HotReloadState {
  39. messages: hot_reload_tx.clone(),
  40. file_map: file_map.clone(),
  41. })
  42. }
  43. false => None,
  44. };
  45. serve(config, hot_reload_state).await?;
  46. Ok(())
  47. }
  48. /// Start the server without hot reload
  49. pub async fn serve(config: CrateConfig, hot_reload_state: Option<HotReloadState>) -> Result<()> {
  50. let (child, first_build_result) = start_desktop(&config)?;
  51. let currently_running_child: RwLock<Child> = RwLock::new(child);
  52. log::info!("🚀 Starting development server...");
  53. // We got to own watcher so that it exists for the duration of serve
  54. // Otherwise full reload won't work.
  55. let _watcher = setup_file_watcher(
  56. {
  57. let config = config.clone();
  58. move || {
  59. let mut current_child = currently_running_child.write().unwrap();
  60. log::trace!("Killing old process");
  61. current_child.kill()?;
  62. let (child, result) = start_desktop(&config)?;
  63. *current_child = child;
  64. Ok(result)
  65. }
  66. },
  67. &config,
  68. None,
  69. hot_reload_state.clone(),
  70. )
  71. .await?;
  72. // Print serve info
  73. print_console_info(
  74. &config,
  75. PrettierOptions {
  76. changed: vec![],
  77. warnings: first_build_result.warnings,
  78. elapsed_time: first_build_result.elapsed_time,
  79. },
  80. None,
  81. );
  82. match hot_reload_state {
  83. Some(hot_reload_state) => {
  84. start_desktop_hot_reload(hot_reload_state).await?;
  85. }
  86. None => {
  87. std::future::pending::<()>().await;
  88. }
  89. }
  90. Ok(())
  91. }
  92. async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()> {
  93. match LocalSocketListener::bind("@dioxusin") {
  94. Ok(local_socket_stream) => {
  95. let aborted = Arc::new(Mutex::new(false));
  96. // States
  97. // The open interprocess sockets
  98. let channels = Arc::new(Mutex::new(Vec::new()));
  99. // listen for connections
  100. std::thread::spawn({
  101. let file_map = hot_reload_state.file_map.clone();
  102. let channels = channels.clone();
  103. let aborted = aborted.clone();
  104. let _ = local_socket_stream.set_nonblocking(true);
  105. move || {
  106. loop {
  107. match local_socket_stream.accept() {
  108. Ok(mut connection) => {
  109. // send any templates than have changed before the socket connected
  110. let templates: Vec<_> = {
  111. file_map
  112. .lock()
  113. .unwrap()
  114. .map
  115. .values()
  116. .filter_map(|(_, template_slot)| *template_slot)
  117. .collect()
  118. };
  119. for template in templates {
  120. if !send_msg(
  121. HotReloadMsg::UpdateTemplate(template),
  122. &mut connection,
  123. ) {
  124. continue;
  125. }
  126. }
  127. channels.lock().unwrap().push(connection);
  128. println!("Connected to hot reloading 🚀");
  129. }
  130. Err(err) => {
  131. if err.kind() != std::io::ErrorKind::WouldBlock {
  132. 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);
  133. }
  134. }
  135. }
  136. if *aborted.lock().unwrap() {
  137. break;
  138. }
  139. }
  140. }
  141. });
  142. let mut hot_reload_rx = hot_reload_state.messages.subscribe();
  143. while let Ok(template) = hot_reload_rx.recv().await {
  144. let channels = &mut *channels.lock().unwrap();
  145. let mut i = 0;
  146. while i < channels.len() {
  147. let channel = &mut channels[i];
  148. if send_msg(HotReloadMsg::UpdateTemplate(template), channel) {
  149. i += 1;
  150. } else {
  151. channels.remove(i);
  152. }
  153. }
  154. }
  155. }
  156. Err(error) => println!("failed to connect to hot reloading\n{error}"),
  157. }
  158. Ok(())
  159. }
  160. fn clear_paths() {
  161. if cfg!(target_os = "macos") {
  162. // On unix, if you force quit the application, it can leave the file socket open
  163. // This will cause the local socket listener to fail to open
  164. // We check if the file socket is already open from an old session and then delete it
  165. let paths = ["./dioxusin", "./@dioxusin"];
  166. for path in paths {
  167. let path = std::path::PathBuf::from(path);
  168. if path.exists() {
  169. let _ = std::fs::remove_file(path);
  170. }
  171. }
  172. }
  173. }
  174. fn send_msg(msg: HotReloadMsg, channel: &mut impl std::io::Write) -> bool {
  175. if let Ok(msg) = serde_json::to_string(&msg) {
  176. if channel.write_all(msg.as_bytes()).is_err() {
  177. return false;
  178. }
  179. if channel.write_all(&[b'\n']).is_err() {
  180. return false;
  181. }
  182. true
  183. } else {
  184. false
  185. }
  186. }
  187. pub fn start_desktop(config: &CrateConfig) -> Result<(Child, BuildResult)> {
  188. // Run the desktop application
  189. log::trace!("Building application");
  190. let result = crate::builder::build_desktop(config, true)?;
  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. log::trace!("Running application from {:?}", file);
  200. let child = Command::new(file.to_str().unwrap()).spawn()?;
  201. Ok((child, result))
  202. }
  203. }
  204. }