fix normal desktop serve

This commit is contained in:
Evan Almloff committed 2023-07-18 12:40:47 -07:00
1 parent 2c1c147828
commit 98e2b4acbd
2 files changed
+99 -98

No files matched your search

+79 -86
View File
@@ -247,128 +247,121 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<BuildResult> {
pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result<BuildResult> { pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result<BuildResult> {
log::info!("🚅 Running build [Desktop] command..."); log::info!("🚅 Running build [Desktop] command...");
let t_start = std::time::Instant::now();
let ignore_files = build_assets(config)?; let ignore_files = build_assets(config)?;
let mut cmd = Command::new("cargo"); let mut cmd = subprocess::Exec::cmd("cargo")
cmd.current_dir(&config.crate_dir) .cwd(&config.crate_dir)
.arg("build") .arg("build")
.stdout(std::process::Stdio::inherit()) .arg("--message-format=json");
.stderr(std::process::Stdio::inherit());
if config.release { if config.release {
cmd.arg("--release"); cmd = cmd.arg("--release");
} }
if config.verbose { if config.verbose {
cmd.arg("--verbose"); cmd = cmd.arg("--verbose");
} }
if config.custom_profile.is_some() { if config.custom_profile.is_some() {
let custom_profile = config.custom_profile.as_ref().unwrap(); let custom_profile = config.custom_profile.as_ref().unwrap();
cmd.arg("--profile"); cmd = cmd.arg("--profile").arg(custom_profile);
cmd.arg(custom_profile);
} }
if config.features.is_some() { if config.features.is_some() {
let features_str = config.features.as_ref().unwrap().join(" "); let features_str = config.features.as_ref().unwrap().join(" ");
cmd.arg("--features"); cmd = cmd.arg("--features").arg(features_str);
cmd.arg(features_str);
} }
match &config.executable { let cmd = match &config.executable {
crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name),
crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name),
crate::ExecutableType::Example(name) => cmd.arg("--example").arg(name), crate::ExecutableType::Example(name) => cmd.arg("--example").arg(name),
}; };
let output = cmd.output()?; let warning_messages = prettier_build(cmd)?;
if !output.status.success() { let release_type = match config.release {
return Err(Error::BuildFailed("Program build failed.".into())); true => "release",
} false => "debug",
};
if output.status.success() { let file_name: String;
let release_type = match config.release { let mut res_path = match &config.executable {
true => "release", crate::ExecutableType::Binary(name) | crate::ExecutableType::Lib(name) => {
false => "debug", file_name = name.clone();
}; config.target_dir.join(release_type).join(name)
let file_name: String;
let mut res_path = match &config.executable {
crate::ExecutableType::Binary(name) | crate::ExecutableType::Lib(name) => {
file_name = name.clone();
config.target_dir.join(release_type).join(name)
}
crate::ExecutableType::Example(name) => {
file_name = name.clone();
config
.target_dir
.join(release_type)
.join("examples")
.join(name)
}
};
let target_file = if cfg!(windows) {
res_path.set_extension("exe");
format!("{}.exe", &file_name)
} else {
file_name
};
if !config.out_dir.is_dir() {
create_dir_all(&config.out_dir)?;
} }
copy(res_path, &config.out_dir.join(target_file))?; crate::ExecutableType::Example(name) => {
file_name = name.clone();
config
.target_dir
.join(release_type)
.join("examples")
.join(name)
}
};
// this code will copy all public file to the output dir let target_file = if cfg!(windows) {
if config.asset_dir.is_dir() { res_path.set_extension("exe");
let copy_options = fs_extra::dir::CopyOptions { format!("{}.exe", &file_name)
overwrite: true, } else {
skip_exist: false, file_name
buffer_size: 64000, };
copy_inside: false,
content_only: false,
depth: 0,
};
for entry in std::fs::read_dir(&config.asset_dir)? { if !config.out_dir.is_dir() {
let path = entry?.path(); create_dir_all(&config.out_dir)?;
if path.is_file() { }
std::fs::copy(&path, &config.out_dir.join(path.file_name().unwrap()))?; copy(res_path, &config.out_dir.join(target_file))?;
} else {
match fs_extra::dir::copy(&path, &config.out_dir, &copy_options) { // this code will copy all public file to the output dir
Ok(_) => {} if config.asset_dir.is_dir() {
Err(e) => { let copy_options = fs_extra::dir::CopyOptions {
log::warn!("Error copying dir: {}", e); overwrite: true,
} skip_exist: false,
buffer_size: 64000,
copy_inside: false,
content_only: false,
depth: 0,
};
for entry in std::fs::read_dir(&config.asset_dir)? {
let path = entry?.path();
if path.is_file() {
std::fs::copy(&path, &config.out_dir.join(path.file_name().unwrap()))?;
} else {
match fs_extra::dir::copy(&path, &config.out_dir, &copy_options) {
Ok(_) => {}
Err(e) => {
log::warn!("Error copying dir: {}", e);
} }
for ignore in &ignore_files { }
let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); for ignore in &ignore_files {
let ignore = config.out_dir.join(ignore); let ignore = ignore.strip_prefix(&config.asset_dir).unwrap();
if ignore.is_file() { let ignore = config.out_dir.join(ignore);
std::fs::remove_file(ignore)?; if ignore.is_file() {
} std::fs::remove_file(ignore)?;
} }
} }
} }
} }
log::info!(
"🚩 Build completed: [./{}]",
config
.dioxus_config
.application
.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from("dist"))
.display()
);
} }
log::info!(
"🚩 Build completed: [./{}]",
config
.dioxus_config
.application
.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from("dist"))
.display()
);
println!("build desktop done"); println!("build desktop done");
Ok(BuildResult { Ok(BuildResult {
warnings: vec![], warnings: warning_messages,
elapsed_time: 0, elapsed_time: (t_start - std::time::Instant::now()).as_millis(),
}) })
} }
+20 -12
View File
@@ -29,8 +29,8 @@ use notify::{RecommendedWatcher, Watcher};
use std::{ use std::{
net::UdpSocket, net::UdpSocket,
path::PathBuf, path::PathBuf,
process::{Command, Stdio}, process::{Child, Command, Stdio},
sync::{Arc, Mutex}, sync::{Arc, Mutex, RwLock},
}; };
use tokio::sync::broadcast::{self, Sender}; use tokio::sync::broadcast::{self, Sender};
use tower::ServiceBuilder; use tower::ServiceBuilder;
@@ -66,7 +66,8 @@ pub async fn startup(config: CrateConfig) -> Result<()> {
/// Start the server without hot reload /// Start the server without hot reload
pub async fn serve_default(config: CrateConfig) -> Result<()> { pub async fn serve_default(config: CrateConfig) -> Result<()> {
let first_build_result = start_desktop(&config)?; let (child, first_build_result) = start_desktop(&config)?;
let currently_running_child: RwLock<Child> = RwLock::new(child);
log::info!("🚀 Starting development server..."); log::info!("🚀 Starting development server...");
@@ -75,7 +76,14 @@ pub async fn serve_default(config: CrateConfig) -> Result<()> {
let _watcher = setup_file_watcher( let _watcher = setup_file_watcher(
{ {
let config = config.clone(); let config = config.clone();
move || start_desktop(&config)
move || {
let mut current_child = currently_running_child.write().unwrap();
current_child.kill()?;
let (child, result) = start_desktop(&config)?;
*current_child = child;
Ok(result)
}
}, },
&config, &config,
None, None,
@@ -93,6 +101,8 @@ pub async fn serve_default(config: CrateConfig) -> Result<()> {
None, None,
); );
std::future::pending::<()>().await;
Ok(()) Ok(())
} }
@@ -100,7 +110,7 @@ pub async fn serve_default(config: CrateConfig) -> Result<()> {
/// Start dx serve with hot reload /// Start dx serve with hot reload
pub async fn serve_hot_reload(config: CrateConfig) -> Result<()> { pub async fn serve_hot_reload(config: CrateConfig) -> Result<()> {
let first_build_result = start_desktop(&config)?; let (_, first_build_result) = start_desktop(&config)?;
println!("🚀 Starting development server..."); println!("🚀 Starting development server...");
@@ -137,7 +147,7 @@ pub async fn serve_hot_reload(config: CrateConfig) -> Result<()> {
for channel in &mut *channels.lock().unwrap() { for channel in &mut *channels.lock().unwrap() {
send_msg(HotReloadMsg::Shutdown, channel); send_msg(HotReloadMsg::Shutdown, channel);
} }
start_desktop(&config) Ok(start_desktop(&config)?.1)
} }
}, },
None, None,
@@ -243,7 +253,7 @@ fn send_msg(msg: HotReloadMsg, channel: &mut impl std::io::Write) -> bool {
} }
} }
pub fn start_desktop(config: &CrateConfig) -> Result<BuildResult> { pub fn start_desktop(config: &CrateConfig) -> Result<(Child, BuildResult)> {
// Run the desktop application // Run the desktop application
let result = crate::builder::build_desktop(config, true)?; let result = crate::builder::build_desktop(config, true)?;
@@ -255,11 +265,9 @@ pub fn start_desktop(config: &CrateConfig) -> Result<BuildResult> {
if cfg!(windows) { if cfg!(windows) {
file.set_extension("exe"); file.set_extension("exe");
} }
Command::new(file.to_str().unwrap()) let child = Command::new(file.to_str().unwrap()).spawn()?;
.stdout(Stdio::inherit())
.spawn()?; Ok((child, result))
} }
} }
Ok(result)
} }