From d6957774026620c8649e92202e312432580c771c Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Mon, 24 Jan 2022 11:55:27 +0800 Subject: [PATCH] feat: support `desktop` --- src/builder.rs | 81 +++++++++++++++++++++++++++++++++++++++++++- src/cli/build/mod.rs | 80 ++----------------------------------------- src/cli/cfg.rs | 4 +++ src/cli/serve/mod.rs | 25 +++++++++++++- 4 files changed, 110 insertions(+), 80 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 0ecde96cd..415e56480 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -3,7 +3,11 @@ use crate::{ error::{Error, Result}, DioxusConfig, }; -use std::process::Command; +use std::{ + fs::{copy, create_dir_all, remove_dir_all}, + path::PathBuf, + process::Command, +}; use wasm_bindgen_cli_support::Bindgen; pub fn build(config: &CrateConfig) -> Result<()> { @@ -116,6 +120,81 @@ pub fn build(config: &CrateConfig) -> Result<()> { Ok(()) } +pub fn build_desktop(config: &CrateConfig) -> Result<()> { + log::info!("🚅 Running build [Desktop] command..."); + + let mut cmd = Command::new("cargo"); + cmd.current_dir(&config.crate_dir) + .arg("build") + .stdout(std::process::Stdio::inherit()) + .stderr(std::process::Stdio::inherit()); + + if config.release { + cmd.arg("--release"); + } + + match &config.executable { + crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), + crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), + crate::ExecutableType::Example(name) => cmd.arg("--example").arg(name), + }; + + let output = cmd.output()?; + + if output.status.success() { + if config.out_dir.is_dir() { + remove_dir_all(&config.out_dir)?; + } + + let release_type = match config.release { + true => "release", + false => "debug", + }; + + 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(format!("{}", release_type)) + .join(format!("{}", name)) + } + crate::ExecutableType::Example(name) => { + file_name = name.clone(); + config + .target_dir + .join(format!("{}", release_type)) + .join("examples") + .join(format!("{}", name)) + } + }; + + let target_file; + if cfg!(windows) { + res_path.set_extension("exe"); + target_file = format!("{}.exe", &file_name); + } else { + target_file = file_name.clone(); + } + create_dir_all(&config.out_dir)?; + copy(res_path, &config.out_dir.join(target_file))?; + + log::info!( + "🚩 Build completed: [./{}]", + config + .dioxus_config + .application + .out_dir + .clone() + .unwrap_or(PathBuf::from("dist")) + .display() + ); + } + + Ok(()) +} + pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { let mut html = String::from(include_str!("./assets/index.html")); diff --git a/src/cli/build/mod.rs b/src/cli/build/mod.rs index c1639e815..f018165ef 100644 --- a/src/cli/build/mod.rs +++ b/src/cli/build/mod.rs @@ -1,12 +1,6 @@ -use std::{ - fs::{copy, create_dir_all}, - io::Write, - path::PathBuf, - process::Command, -}; +use std::{io::Write, path::PathBuf}; use crate::{cfg::ConfigOptsBuild, gen_page}; -use std::fs::remove_dir_all; use structopt::StructOpt; /// Build the Rust WASM app and all of its assets. @@ -30,77 +24,7 @@ impl Build { if self.build.platform.is_some() { if self.build.platform.unwrap().to_uppercase() == "DESKTOP" { - log::info!("🚅 Running build [Desktop] command..."); - - let mut cmd = Command::new("cargo"); - cmd.current_dir(&crate_config.crate_dir) - .arg("build") - .stdout(std::process::Stdio::inherit()) - .stderr(std::process::Stdio::inherit()); - - if self.build.release { - cmd.arg("--release"); - } - - match &crate_config.executable { - crate::ExecutableType::Binary(name) => cmd.arg("--bin").arg(name), - crate::ExecutableType::Lib(name) => cmd.arg("--lib").arg(name), - crate::ExecutableType::Example(name) => cmd.arg("--example").arg(name), - }; - - let output = cmd.output()?; - - if output.status.success() { - if crate_config.out_dir.is_dir() { - remove_dir_all(&crate_config.out_dir)?; - } - - let release_type = match crate_config.release { - true => "release", - false => "debug", - }; - - let file_name: String; - let mut res_path = match &crate_config.executable { - crate::ExecutableType::Binary(name) | crate::ExecutableType::Lib(name) => { - file_name = name.clone(); - crate_config - .target_dir - .join(format!("{}", release_type)) - .join(format!("{}", name)) - } - crate::ExecutableType::Example(name) => { - file_name = name.clone(); - crate_config - .target_dir - .join(format!("{}", release_type)) - .join("examples") - .join(format!("{}", name)) - } - }; - - let target_file; - if cfg!(windows) { - res_path.set_extension("exe"); - target_file = format!("{}.exe", &file_name); - } else { - target_file = file_name.clone(); - } - create_dir_all(&crate_config.out_dir)?; - copy(res_path, &crate_config.out_dir.join(target_file))?; - - log::info!( - "🏛 Build completed: [:{}]", - &crate_config - .dioxus_config - .application - .out_dir - .unwrap_or(PathBuf::from("dist")) - .display() - ); - } - - return Ok(()); + crate::builder::build_desktop(&crate_config)?; } } diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index 0734f362d..454da71fe 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -38,6 +38,10 @@ pub struct ConfigOptsServe { #[structopt(long)] #[serde(default)] pub release: bool, + + /// Build platform: support Web & Desktop [default: "web"] + #[structopt(long)] + pub platform: Option, } /// Ensure the given value for `--public-url` is formatted correctly. diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 8b002e1fb..ef413a63e 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -1,5 +1,5 @@ use crate::{cfg::ConfigOptsServe, gen_page, server, CrateConfig}; -use std::{io::Write, path::PathBuf}; +use std::{io::Write, path::PathBuf, process::Command}; use structopt::StructOpt; /// Run the WASM project on dev-server @@ -21,6 +21,29 @@ impl Serve { crate_config.as_example(self.serve.example.unwrap()); } + if self.serve.platform.is_some() { + if self.serve.platform.unwrap().to_uppercase() == "DESKTOP" { + crate::builder::build_desktop(&crate_config)?; + + match &crate_config.executable { + crate::ExecutableType::Binary(name) + | crate::ExecutableType::Lib(name) + | crate::ExecutableType::Example(name) => { + let mut file = crate_config.out_dir.join(name); + if cfg!(windows) { + file.set_extension("exe"); + } + Command::new(format!( + "{}", + crate_config.out_dir.join(file).to_str().unwrap() + )) + .output()?; + } + } + return Ok(()); + } + } + crate::builder::build(&crate_config).expect("build failed"); // generate dev-index page