|
@@ -1,6 +1,6 @@
|
|
|
use crate::helpers::{
|
|
|
app_paths::{app_dir, tauri_dir},
|
|
|
- config::{get as get_config, reload as reload_config, Config},
|
|
|
+ config::{get as get_config, reload as reload_config, ConfigHandle},
|
|
|
manifest::rewrite_manifest,
|
|
|
Logger, TauriHtml,
|
|
|
};
|
|
@@ -52,13 +52,19 @@ impl Dev {
|
|
|
pub fn run(self) -> crate::Result<()> {
|
|
|
let logger = Logger::new("tauri:dev");
|
|
|
let tauri_path = tauri_dir();
|
|
|
- let config = get_config(self.config.as_deref())?;
|
|
|
- let mut config_mut = config.clone();
|
|
|
+ let merge_config = self.config.clone();
|
|
|
+ let config = get_config(merge_config.as_deref())?;
|
|
|
let mut _guard = None;
|
|
|
let mut process: Arc<SharedChild>;
|
|
|
- let new_dev_path: String;
|
|
|
|
|
|
- if let Some(before_dev) = &config.build.before_dev_command {
|
|
|
+ if let Some(before_dev) = &config
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .as_ref()
|
|
|
+ .unwrap()
|
|
|
+ .build
|
|
|
+ .before_dev_command
|
|
|
+ {
|
|
|
let mut cmd: Option<&str> = None;
|
|
|
let mut args: Vec<&str> = vec![];
|
|
|
for token in before_dev.split(' ') {
|
|
@@ -77,10 +83,17 @@ impl Dev {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- let running_dev_server = config.build.dev_path.starts_with("http");
|
|
|
-
|
|
|
- if running_dev_server {
|
|
|
- let dev_path = Url::parse(&config.build.dev_path)?;
|
|
|
+ let running_dev_server = config
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .as_ref()
|
|
|
+ .unwrap()
|
|
|
+ .build
|
|
|
+ .dev_path
|
|
|
+ .starts_with("http");
|
|
|
+
|
|
|
+ let new_dev_path = if running_dev_server {
|
|
|
+ let dev_path = Url::parse(&config.lock().unwrap().as_ref().unwrap().build.dev_path)?;
|
|
|
let dev_port = dev_path.port().unwrap_or(80);
|
|
|
|
|
|
let timeout = Duration::from_secs(3);
|
|
@@ -105,27 +118,33 @@ impl Dev {
|
|
|
|
|
|
logger.log(format!("starting dev proxy on port {}", proxy_port));
|
|
|
let config_ = config.clone();
|
|
|
- std::thread::spawn(move || proxy_dev_server(&config_, &proxy_path, proxy_port));
|
|
|
+ std::thread::spawn(move || proxy_dev_server(config_, &proxy_path, proxy_port));
|
|
|
|
|
|
- new_dev_path = format!(
|
|
|
+ format!(
|
|
|
"http://{}:{}",
|
|
|
dev_path.host_str().expect("failed to read dev_path host"),
|
|
|
proxy_port
|
|
|
- );
|
|
|
+ )
|
|
|
} else {
|
|
|
- new_dev_path = tauri_dir()
|
|
|
- .join(&config.build.dev_path)
|
|
|
+ tauri_dir()
|
|
|
+ .join(&config.lock().unwrap().as_ref().unwrap().build.dev_path)
|
|
|
.to_string_lossy()
|
|
|
- .to_string();
|
|
|
- }
|
|
|
+ .to_string()
|
|
|
+ };
|
|
|
|
|
|
- config_mut.build.dev_path = new_dev_path.clone();
|
|
|
+ (*config.lock().unwrap()).as_mut().unwrap().build.dev_path = new_dev_path.to_string();
|
|
|
|
|
|
set_var("TAURI_DIR", &tauri_path);
|
|
|
- set_var("TAURI_DIST_DIR", tauri_path.join(&config.build.dist_dir));
|
|
|
- set_var("TAURI_CONFIG", serde_json::to_string(&config_mut)?);
|
|
|
+ set_var(
|
|
|
+ "TAURI_DIST_DIR",
|
|
|
+ tauri_path.join(&config.lock().unwrap().as_ref().unwrap().build.dist_dir),
|
|
|
+ );
|
|
|
+ set_var(
|
|
|
+ "TAURI_CONFIG",
|
|
|
+ serde_json::to_string(&*config.lock().unwrap())?,
|
|
|
+ );
|
|
|
|
|
|
- rewrite_manifest(&config)?;
|
|
|
+ rewrite_manifest(config.clone())?;
|
|
|
|
|
|
let (child_wait_tx, child_wait_rx) = channel();
|
|
|
let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));
|
|
@@ -134,13 +153,20 @@ impl Dev {
|
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
- let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
|
|
|
+ let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
|
|
|
watcher.watch(tauri_path.join("src"), RecursiveMode::Recursive)?;
|
|
|
watcher.watch(tauri_path.join("Cargo.toml"), RecursiveMode::Recursive)?;
|
|
|
watcher.watch(tauri_path.join("tauri.conf.json"), RecursiveMode::Recursive)?;
|
|
|
if !running_dev_server {
|
|
|
watcher.watch(
|
|
|
- config_mut.build.dev_path.to_string(),
|
|
|
+ config
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .as_ref()
|
|
|
+ .unwrap()
|
|
|
+ .build
|
|
|
+ .dev_path
|
|
|
+ .to_string(),
|
|
|
RecursiveMode::Recursive,
|
|
|
)?;
|
|
|
}
|
|
@@ -156,16 +182,19 @@ impl Dev {
|
|
|
};
|
|
|
|
|
|
if let Some(event_path) = event_path {
|
|
|
- let _ = child_wait_tx.send(true);
|
|
|
- process.kill()?;
|
|
|
if event_path.file_name() == Some(OsStr::new("tauri.conf.json")) {
|
|
|
- config_mut = reload_config(Some(&serde_json::to_string(&config_mut)?))?.clone();
|
|
|
- rewrite_manifest(&config_mut)?;
|
|
|
- config_mut.build.dev_path = new_dev_path.clone();
|
|
|
- set_var("TAURI_CONFIG", serde_json::to_string(&config_mut)?);
|
|
|
+ reload_config(merge_config.as_deref())?;
|
|
|
+ (*config.lock().unwrap()).as_mut().unwrap().build.dev_path = new_dev_path.to_string();
|
|
|
+ rewrite_manifest(config.clone())?;
|
|
|
+ set_var("TAURI_CONFIG", serde_json::to_string(&*config)?);
|
|
|
+ } else {
|
|
|
+ // When tauri.conf.json is changed, rewrite_manifest will be called
|
|
|
+ // which will trigger the watcher again
|
|
|
+ // So the app should only be started when a file other than tauri.conf.json is changed
|
|
|
+ let _ = child_wait_tx.send(true);
|
|
|
+ process.kill()?;
|
|
|
+ process = self.start_app(child_wait_rx.clone());
|
|
|
}
|
|
|
-
|
|
|
- process = self.start_app(child_wait_rx.clone());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -196,7 +225,7 @@ impl Dev {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-fn proxy_dev_server(config: &Config, dev_path: &Url, dev_port: u16) -> crate::Result<()> {
|
|
|
+fn proxy_dev_server(config: ConfigHandle, dev_path: &Url, dev_port: u16) -> crate::Result<()> {
|
|
|
let server_url = format!(
|
|
|
"{}:{}",
|
|
|
dev_path.host_str().expect("failed to read dev_path host"),
|
|
@@ -218,6 +247,8 @@ fn proxy_dev_server(config: &Config, dev_path: &Url, dev_port: u16) -> crate::Re
|
|
|
}
|
|
|
|
|
|
if request_url == "/" {
|
|
|
+ let config_guard = config.lock().unwrap();
|
|
|
+ let config = config_guard.as_ref().unwrap();
|
|
|
let response = request_builder.send()?.text()?;
|
|
|
let tauri_html = TauriHtml::new(&config.build.dist_dir, response)
|
|
|
.global_tauri(config.build.with_global_tauri)
|