dev.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright 2019-2022 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use crate::{
  5. helpers::{
  6. app_paths::{app_dir, tauri_dir},
  7. command_env,
  8. config::{get as get_config, reload as reload_config, AppUrl, BeforeDevCommand, WindowUrl},
  9. },
  10. interface::{AppInterface, ExitReason, Interface},
  11. CommandExt, Result,
  12. };
  13. use clap::Parser;
  14. use anyhow::{bail, Context};
  15. use log::{error, info, warn};
  16. use once_cell::sync::OnceCell;
  17. use shared_child::SharedChild;
  18. use std::{
  19. env::set_current_dir,
  20. process::{exit, Command, ExitStatus, Stdio},
  21. sync::{
  22. atomic::{AtomicBool, Ordering},
  23. Arc, Mutex,
  24. },
  25. };
  26. static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new();
  27. static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
  28. #[cfg(unix)]
  29. const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh");
  30. pub const TAURI_DEV_WATCHER_GITIGNORE: &[u8] = include_bytes!("../tauri-dev-watcher.gitignore");
  31. #[derive(Debug, Clone, Parser)]
  32. #[clap(about = "Tauri dev", trailing_var_arg(true))]
  33. pub struct Options {
  34. /// Binary to use to run the application
  35. #[clap(short, long)]
  36. pub runner: Option<String>,
  37. /// Target triple to build against
  38. #[clap(short, long)]
  39. pub target: Option<String>,
  40. /// List of cargo features to activate
  41. #[clap(short, long, multiple_occurrences(true), multiple_values(true))]
  42. pub features: Option<Vec<String>>,
  43. /// Exit on panic
  44. #[clap(short, long)]
  45. exit_on_panic: bool,
  46. /// JSON string or path to JSON file to merge with tauri.conf.json
  47. #[clap(short, long)]
  48. pub config: Option<String>,
  49. /// Run the code in release mode
  50. #[clap(long = "release")]
  51. pub release_mode: bool,
  52. /// Command line arguments passed to the runner. Arguments after `--` are passed to the application.
  53. pub args: Vec<String>,
  54. /// Disable the file watcher
  55. #[clap(long)]
  56. pub no_watch: bool,
  57. }
  58. pub fn command(options: Options) -> Result<()> {
  59. let r = command_internal(options);
  60. if r.is_err() {
  61. kill_before_dev_process();
  62. #[cfg(not(debug_assertions))]
  63. let _ = check_for_updates();
  64. }
  65. r
  66. }
  67. fn command_internal(mut options: Options) -> Result<()> {
  68. let tauri_path = tauri_dir();
  69. options.config = if let Some(config) = &options.config {
  70. Some(if config.starts_with('{') {
  71. config.to_string()
  72. } else {
  73. std::fs::read_to_string(&config).with_context(|| "failed to read custom configuration")?
  74. })
  75. } else {
  76. None
  77. };
  78. set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
  79. let config = get_config(options.config.as_deref())?;
  80. if let Some(before_dev) = config
  81. .lock()
  82. .unwrap()
  83. .as_ref()
  84. .unwrap()
  85. .build
  86. .before_dev_command
  87. .clone()
  88. {
  89. let (script, script_cwd, wait) = match before_dev {
  90. BeforeDevCommand::Script(s) if s.is_empty() => (None, None, false),
  91. BeforeDevCommand::Script(s) => (Some(s), None, false),
  92. BeforeDevCommand::ScriptWithOptions { script, cwd, wait } => {
  93. (Some(script), cwd.map(Into::into), wait)
  94. }
  95. };
  96. let cwd = script_cwd.unwrap_or_else(|| app_dir().clone());
  97. if let Some(before_dev) = script {
  98. info!(action = "Running"; "BeforeDevCommand (`{}`)", before_dev);
  99. #[cfg(windows)]
  100. let mut command = {
  101. let mut command = Command::new("cmd");
  102. command
  103. .arg("/S")
  104. .arg("/C")
  105. .arg(&before_dev)
  106. .current_dir(cwd)
  107. .envs(command_env(true));
  108. command
  109. };
  110. #[cfg(not(windows))]
  111. let mut command = {
  112. let mut command = Command::new("sh");
  113. command
  114. .arg("-c")
  115. .arg(&before_dev)
  116. .current_dir(cwd)
  117. .envs(command_env(true));
  118. command
  119. };
  120. if wait {
  121. let status = command.piped().with_context(|| {
  122. format!(
  123. "failed to run `{}` with `{}`",
  124. before_dev,
  125. if cfg!(windows) { "cmd /S /C" } else { "sh -c" }
  126. )
  127. })?;
  128. if !status.success() {
  129. bail!(
  130. "beforeDevCommand `{}` failed with exit code {}",
  131. before_dev,
  132. status.code().unwrap_or_default()
  133. );
  134. }
  135. } else {
  136. command.stdin(Stdio::piped());
  137. command.stdout(os_pipe::dup_stdout()?);
  138. command.stderr(os_pipe::dup_stderr()?);
  139. let child = SharedChild::spawn(&mut command)
  140. .unwrap_or_else(|_| panic!("failed to run `{}`", before_dev));
  141. let child = Arc::new(child);
  142. let child_ = child.clone();
  143. std::thread::spawn(move || {
  144. let status = child_
  145. .wait()
  146. .expect("failed to wait on \"beforeDevCommand\"");
  147. if !(status.success() || KILL_BEFORE_DEV_FLAG.get().unwrap().load(Ordering::Relaxed)) {
  148. error!("The \"beforeDevCommand\" terminated with a non-zero status code.");
  149. exit(status.code().unwrap_or(1));
  150. }
  151. });
  152. BEFORE_DEV.set(Mutex::new(child)).unwrap();
  153. KILL_BEFORE_DEV_FLAG.set(AtomicBool::default()).unwrap();
  154. let _ = ctrlc::set_handler(move || {
  155. kill_before_dev_process();
  156. #[cfg(not(debug_assertions))]
  157. let _ = check_for_updates();
  158. exit(130);
  159. });
  160. }
  161. }
  162. }
  163. if options.runner.is_none() {
  164. options.runner = config
  165. .lock()
  166. .unwrap()
  167. .as_ref()
  168. .unwrap()
  169. .build
  170. .runner
  171. .clone();
  172. }
  173. let mut cargo_features = config
  174. .lock()
  175. .unwrap()
  176. .as_ref()
  177. .unwrap()
  178. .build
  179. .features
  180. .clone()
  181. .unwrap_or_default();
  182. if let Some(features) = &options.features {
  183. cargo_features.extend(features.clone());
  184. }
  185. let mut dev_path = config
  186. .lock()
  187. .unwrap()
  188. .as_ref()
  189. .unwrap()
  190. .build
  191. .dev_path
  192. .clone();
  193. if let AppUrl::Url(WindowUrl::App(path)) = &dev_path {
  194. use crate::helpers::web_dev_server::{start_dev_server, SERVER_URL};
  195. if path.exists() {
  196. let path = path.canonicalize()?;
  197. start_dev_server(path);
  198. dev_path = AppUrl::Url(WindowUrl::External(SERVER_URL.parse().unwrap()));
  199. // TODO: in v2, use an env var to pass the url to the app context
  200. // or better separate the config passed from the cli internally and
  201. // config passed by the user in `--config` into to separate env vars
  202. // and the context merges, the user first, then the internal cli config
  203. if let Some(c) = options.config {
  204. let mut c: tauri_utils::config::Config = serde_json::from_str(&c)?;
  205. c.build.dev_path = dev_path.clone();
  206. options.config = Some(serde_json::to_string(&c).unwrap());
  207. } else {
  208. options.config = Some(format!(
  209. r#"{{ "build": {{ "devPath": "{}" }} }}"#,
  210. SERVER_URL
  211. ))
  212. }
  213. }
  214. }
  215. let config = reload_config(options.config.as_deref())?;
  216. if std::env::var_os("TAURI_SKIP_DEVSERVER_CHECK") != Some("true".into()) {
  217. if let AppUrl::Url(WindowUrl::External(dev_server_url)) = dev_path {
  218. let host = dev_server_url
  219. .host()
  220. .unwrap_or_else(|| panic!("No host name in the URL"));
  221. let port = dev_server_url
  222. .port_or_known_default()
  223. .unwrap_or_else(|| panic!("No port number in the URL"));
  224. let addrs;
  225. let addr;
  226. let addrs = match host {
  227. url::Host::Domain(domain) => {
  228. use std::net::ToSocketAddrs;
  229. addrs = (domain, port).to_socket_addrs()?;
  230. addrs.as_slice()
  231. }
  232. url::Host::Ipv4(ip) => {
  233. addr = (ip, port).into();
  234. std::slice::from_ref(&addr)
  235. }
  236. url::Host::Ipv6(ip) => {
  237. addr = (ip, port).into();
  238. std::slice::from_ref(&addr)
  239. }
  240. };
  241. let mut i = 0;
  242. let sleep_interval = std::time::Duration::from_secs(2);
  243. let max_attempts = 90;
  244. loop {
  245. if std::net::TcpStream::connect(addrs).is_ok() {
  246. break;
  247. }
  248. if i % 3 == 1 {
  249. warn!(
  250. "Waiting for your frontend dev server to start on {}...",
  251. dev_server_url
  252. );
  253. }
  254. i += 1;
  255. if i == max_attempts {
  256. error!(
  257. "Could not connect to `{}` after {}s. Please make sure that is the URL to your dev server.",
  258. dev_server_url, i * sleep_interval.as_secs()
  259. );
  260. exit(1);
  261. }
  262. std::thread::sleep(sleep_interval);
  263. }
  264. }
  265. }
  266. let mut interface = AppInterface::new(config.lock().unwrap().as_ref().unwrap())?;
  267. let exit_on_panic = options.exit_on_panic;
  268. let no_watch = options.no_watch;
  269. interface.dev(options.into(), move |status, reason| {
  270. on_dev_exit(status, reason, exit_on_panic, no_watch)
  271. })
  272. }
  273. fn on_dev_exit(status: ExitStatus, reason: ExitReason, exit_on_panic: bool, no_watch: bool) {
  274. if no_watch
  275. || (!matches!(reason, ExitReason::TriggeredKill)
  276. && (exit_on_panic || matches!(reason, ExitReason::NormalExit)))
  277. {
  278. kill_before_dev_process();
  279. #[cfg(not(debug_assertions))]
  280. let _ = check_for_updates();
  281. exit(status.code().unwrap_or(0));
  282. }
  283. }
  284. #[cfg(not(debug_assertions))]
  285. fn check_for_updates() -> Result<()> {
  286. if std::env::var_os("TAURI_SKIP_UPDATE_CHECK") != Some("true".into()) {
  287. let current_version = crate::info::cli_current_version()?;
  288. let current = semver::Version::parse(&current_version)?;
  289. let upstream_version = crate::info::cli_upstream_version()?;
  290. let upstream = semver::Version::parse(&upstream_version)?;
  291. if current < upstream {
  292. println!(
  293. "🚀 A new version of Tauri CLI is available! [{}]",
  294. upstream.to_string()
  295. );
  296. };
  297. }
  298. Ok(())
  299. }
  300. fn kill_before_dev_process() {
  301. if let Some(child) = BEFORE_DEV.get() {
  302. let child = child.lock().unwrap();
  303. KILL_BEFORE_DEV_FLAG
  304. .get()
  305. .unwrap()
  306. .store(true, Ordering::Relaxed);
  307. #[cfg(windows)]
  308. {
  309. let powershell_path = std::env::var("SYSTEMROOT").map_or_else(
  310. |_| "powershell.exe".to_string(),
  311. |p| format!("{p}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"),
  312. );
  313. let _ = Command::new(powershell_path)
  314. .arg("-NoProfile")
  315. .arg("-Command")
  316. .arg(format!("function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid -ErrorAction SilentlyContinue }}; Kill-Tree {}", child.id()))
  317. .status();
  318. }
  319. #[cfg(unix)]
  320. {
  321. use std::io::Write;
  322. let mut kill_children_script_path = std::env::temp_dir();
  323. kill_children_script_path.push("kill-children.sh");
  324. if !kill_children_script_path.exists() {
  325. if let Ok(mut file) = std::fs::File::create(&kill_children_script_path) {
  326. use std::os::unix::fs::PermissionsExt;
  327. let _ = file.write_all(KILL_CHILDREN_SCRIPT);
  328. let mut permissions = file.metadata().unwrap().permissions();
  329. permissions.set_mode(0o770);
  330. let _ = file.set_permissions(permissions);
  331. }
  332. }
  333. let _ = Command::new(&kill_children_script_path)
  334. .arg(child.id().to_string())
  335. .output();
  336. }
  337. let _ = child.kill();
  338. }
  339. }