feat: support desktop

This commit is contained in:
mrxiaozhuox committed 2022-01-24 11:55:27 +08:00
1 parent bb69a04045
commit d695777402
4 files changed
+110 -80

No files matched your search

+80 -1
View File
@@ -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"));
+2 -78
View File
@@ -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)?;
}
}
+4
View File
@@ -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<String>,
}
/// Ensure the given value for `--public-url` is formatted correctly.
+24 -1
View File
@@ -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