feat: commit code
This commit is contained in:
1 parent
98134cfe1c
commit
526a88cab1
5 files changed
+190
-75
No files matched your search
+47
-26
@@ -4,19 +4,26 @@ use crate::{
|
||||
tools::Tool,
|
||||
DioxusConfig,
|
||||
};
|
||||
use cargo_metadata::Message;
|
||||
use loading::Loading;
|
||||
use cargo_metadata::{diagnostic::Diagnostic, Message};
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use serde::Serialize;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{copy, create_dir_all, remove_dir_all, File},
|
||||
io::Read,
|
||||
panic,
|
||||
path::PathBuf,
|
||||
process::Command,
|
||||
time::Duration,
|
||||
};
|
||||
use wasm_bindgen_cli_support::Bindgen;
|
||||
|
||||
pub fn build(config: &CrateConfig, quiet: bool) -> Result<()> {
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct BuildResult {
|
||||
pub warnings: Vec<Diagnostic>,
|
||||
pub elapsed_time: u128,
|
||||
}
|
||||
|
||||
pub fn build(config: &CrateConfig, quiet: bool) -> Result<BuildResult> {
|
||||
// [1] Build the project with cargo, generating a wasm32-unknown-unknown target (is there a more specific, better target to leverage?)
|
||||
// [2] Generate the appropriate build folders
|
||||
// [3] Wasm-bindgen the .wasm fiile, and move it into the {builddir}/modules/xxxx/xxxx_bg.wasm
|
||||
@@ -78,13 +85,7 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<()> {
|
||||
ExecutableType::Example(name) => cmd.arg("--example").arg(name),
|
||||
};
|
||||
|
||||
prettier_build(cmd).unwrap();
|
||||
|
||||
// if !output.status.success() {
|
||||
// log::error!("Build failed!");
|
||||
// let reason = String::from_utf8_lossy(&output.stderr).to_string();
|
||||
// return Err(Error::BuildFailed(reason));
|
||||
// }
|
||||
let warning_messages = prettier_build(cmd)?;
|
||||
|
||||
// [2] Establish the output directory structure
|
||||
let bindgen_outdir = out_dir.join("assets").join("dioxus");
|
||||
@@ -122,8 +123,7 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<()> {
|
||||
.unwrap();
|
||||
});
|
||||
if bindgen_result.is_err() {
|
||||
log::error!("Bindgen build failed! \nThis is probably due to the Bindgen version, dioxus-cli using `0.2.79` Bindgen crate.");
|
||||
return Ok(());
|
||||
return Err(Error::BuildFailed("Bindgen build failed! \nThis is probably due to the Bindgen version, dioxus-cli using `0.2.79` Bindgen crate.".to_string()));
|
||||
}
|
||||
|
||||
// check binaryen:wasm-opt tool
|
||||
@@ -193,8 +193,10 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<()> {
|
||||
}
|
||||
|
||||
let t_end = std::time::Instant::now();
|
||||
log::info!("🏁 Done in {}ms!", (t_end - t_start).as_millis());
|
||||
Ok(())
|
||||
Ok(BuildResult {
|
||||
warnings: warning_messages,
|
||||
elapsed_time: (t_end - t_start).as_millis()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> {
|
||||
@@ -327,27 +329,46 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prettier_build(mut cmd: Command) -> anyhow::Result<()> {
|
||||
let loading = Loading::default();
|
||||
let mut record: HashMap<String, String> = HashMap::new();
|
||||
fn prettier_build(mut cmd: Command) -> anyhow::Result<Vec<Diagnostic>> {
|
||||
let mut warning_messages: Vec<Diagnostic> = vec![];
|
||||
|
||||
let pb = ProgressBar::new_spinner();
|
||||
pb.enable_steady_tick(Duration::from_millis(200));
|
||||
pb.set_style(
|
||||
ProgressStyle::with_template("{spinner:.dim.bold} {wide_msg}")
|
||||
.unwrap()
|
||||
.tick_chars("/|\\- "),
|
||||
);
|
||||
pb.set_message("Start to build project");
|
||||
|
||||
let mut command = cmd.spawn()?;
|
||||
let reader = std::io::BufReader::new(command.stdout.take().unwrap());
|
||||
for message in cargo_metadata::Message::parse_stream(reader) {
|
||||
match message.unwrap() {
|
||||
Message::CompilerMessage(_msg) => {
|
||||
// println!("{:#?}", msg);
|
||||
Message::CompilerMessage(msg) => {
|
||||
let message = msg.message;
|
||||
match message.level {
|
||||
cargo_metadata::diagnostic::DiagnosticLevel::Error => {
|
||||
log::error!("Build Failed: {}", message.message);
|
||||
std::process::exit(1);
|
||||
}
|
||||
cargo_metadata::diagnostic::DiagnosticLevel::Warning => {
|
||||
warning_messages.push(message.clone());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Message::CompilerArtifact(artifact) => {
|
||||
record.insert(artifact.package_id.to_string(), artifact.target.name);
|
||||
pb.set_message(format!("Compiling {} ", artifact.package_id));
|
||||
pb.tick();
|
||||
}
|
||||
Message::BuildScriptExecuted(script) => {
|
||||
let package_id = script.package_id.to_string();
|
||||
if let Some(name) = record.get(&package_id) {
|
||||
loading.text(format!("[{}] build done", name));
|
||||
}
|
||||
let _package_id = script.package_id.to_string();
|
||||
}
|
||||
Message::BuildFinished(finished) => {
|
||||
if finished.success {
|
||||
pb.finish_and_clear();
|
||||
println!("Done in");
|
||||
} else {
|
||||
std::process::exit(1);
|
||||
}
|
||||
@@ -355,7 +376,7 @@ fn prettier_build(mut cmd: Command) -> anyhow::Result<()> {
|
||||
_ => (), // Unknown message
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(warning_messages)
|
||||
}
|
||||
|
||||
pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
|
||||
|
||||
+15
-21
@@ -38,31 +38,25 @@ impl Serve {
|
||||
.clone()
|
||||
});
|
||||
|
||||
match platform.as_str() {
|
||||
"web" => {
|
||||
crate::builder::build(&crate_config, false)?;
|
||||
}
|
||||
"desktop" => {
|
||||
crate::builder::build_desktop(&crate_config, true)?;
|
||||
if platform.as_str() == "desktop" {
|
||||
crate::builder::build_desktop(&crate_config, true)?;
|
||||
|
||||
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(crate_config.out_dir.join(file).to_str().unwrap())
|
||||
.stdout(Stdio::inherit())
|
||||
.output()?;
|
||||
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(crate_config.out_dir.join(file).to_str().unwrap())
|
||||
.stdout(Stdio::inherit())
|
||||
.output()?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
_ => {
|
||||
return custom_error!("Unsoppurt platform target.");
|
||||
}
|
||||
return Ok(());
|
||||
} else if platform != "web" {
|
||||
return custom_error!("Unsoppurt platform target.");
|
||||
}
|
||||
|
||||
// generate dev-index page
|
||||
|
||||
+69
-17
@@ -6,6 +6,7 @@ use axum::{
|
||||
routing::{get, get_service},
|
||||
Router,
|
||||
};
|
||||
use cargo_metadata::diagnostic::Diagnostic;
|
||||
use colored::Colorize;
|
||||
use dioxus::rsx_interpreter::SetRsxMessage;
|
||||
use notify::{RecommendedWatcher, Watcher};
|
||||
@@ -15,7 +16,7 @@ use std::{net::UdpSocket, path::PathBuf, process::Command, sync::Arc};
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::services::fs::{ServeDir, ServeFileSystemResponseBody};
|
||||
|
||||
use crate::{builder, serve::Serve, CrateConfig, Result};
|
||||
use crate::{builder, serve::Serve, BuildResult, CrateConfig, Result};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
mod hot_reload;
|
||||
@@ -27,9 +28,9 @@ pub struct BuildManager {
|
||||
}
|
||||
|
||||
impl BuildManager {
|
||||
fn rebuild(&self) -> Result<()> {
|
||||
fn rebuild(&self) -> Result<BuildResult> {
|
||||
log::info!("🪁 Rebuild project");
|
||||
builder::build(&self.config, true)?;
|
||||
let result = builder::build(&self.config, true)?;
|
||||
// change the websocket reload state to true;
|
||||
// the page will auto-reload.
|
||||
if self
|
||||
@@ -43,7 +44,7 @@ impl BuildManager {
|
||||
let _ = Serve::regen_dev_page(&self.config);
|
||||
}
|
||||
let _ = self.reload_tx.send(());
|
||||
Ok(())
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +62,8 @@ pub async fn startup(port: u16, config: CrateConfig) -> Result<()> {
|
||||
}
|
||||
|
||||
pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> {
|
||||
let first_build_result = crate::builder::build(&config, false)?;
|
||||
|
||||
log::info!("🚀 Starting development server...");
|
||||
|
||||
let dist_path = config.out_dir.clone();
|
||||
@@ -159,9 +162,22 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> {
|
||||
}
|
||||
}
|
||||
if needs_rebuild {
|
||||
print_console_info(port, &config, evt.paths);
|
||||
if let Err(err) = build_manager.rebuild() {
|
||||
log::error!("{}", err);
|
||||
match build_manager.rebuild() {
|
||||
Ok(res) => {
|
||||
print_console_info(
|
||||
port,
|
||||
&config,
|
||||
PrettierOptions {
|
||||
changed: evt.paths,
|
||||
warnings: res.warnings,
|
||||
elapsed_time: res.elapsed_time,
|
||||
},
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("{}", err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !messages.is_empty() {
|
||||
@@ -183,7 +199,15 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> {
|
||||
}
|
||||
|
||||
// start serve dev-server at 0.0.0.0:8080
|
||||
print_console_info(port, &config, vec![]);
|
||||
print_console_info(
|
||||
port,
|
||||
&config,
|
||||
PrettierOptions {
|
||||
changed: vec![],
|
||||
warnings: first_build_result.warnings,
|
||||
elapsed_time: first_build_result.elapsed_time,
|
||||
},
|
||||
);
|
||||
|
||||
let file_service_config = config.clone();
|
||||
let file_service = ServiceBuilder::new()
|
||||
@@ -246,6 +270,8 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> {
|
||||
}
|
||||
|
||||
pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> {
|
||||
let first_build_result = crate::builder::build(&config, false)?;
|
||||
|
||||
log::info!("🚀 Starting development server...");
|
||||
|
||||
let dist_path = config.out_dir.clone();
|
||||
@@ -276,10 +302,20 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> {
|
||||
let mut watcher = RecommendedWatcher::new(move |info: notify::Result<notify::Event>| {
|
||||
let config = watcher_config.clone();
|
||||
if info.is_ok() {
|
||||
print_console_info(port, &config, info.unwrap().paths);
|
||||
if chrono::Local::now().timestamp() > last_update_time {
|
||||
match build_manager.rebuild() {
|
||||
Ok(_) => last_update_time = chrono::Local::now().timestamp(),
|
||||
Ok(res) => {
|
||||
last_update_time = chrono::Local::now().timestamp();
|
||||
print_console_info(
|
||||
port,
|
||||
&config,
|
||||
PrettierOptions {
|
||||
changed: info.unwrap().paths,
|
||||
warnings: res.warnings,
|
||||
elapsed_time: res.elapsed_time,
|
||||
},
|
||||
);
|
||||
}
|
||||
Err(e) => log::error!("{}", e),
|
||||
}
|
||||
}
|
||||
@@ -297,8 +333,16 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> {
|
||||
}
|
||||
|
||||
// start serve dev-server at 0.0.0.0
|
||||
print_console_info(port, &config, vec![]);
|
||||
|
||||
print_console_info(
|
||||
port,
|
||||
&config,
|
||||
PrettierOptions {
|
||||
changed: vec![],
|
||||
warnings: first_build_result.warnings,
|
||||
elapsed_time: first_build_result.elapsed_time,
|
||||
},
|
||||
);
|
||||
|
||||
let file_service_config = config.clone();
|
||||
let file_service = ServiceBuilder::new()
|
||||
.and_then(
|
||||
@@ -355,7 +399,14 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_console_info(port: u16, config: &CrateConfig, changed: Vec<PathBuf>) {
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PrettierOptions {
|
||||
changed: Vec<PathBuf>,
|
||||
warnings: Vec<Diagnostic>,
|
||||
elapsed_time: u128,
|
||||
}
|
||||
|
||||
fn print_console_info(port: u16, config: &CrateConfig, options: PrettierOptions) {
|
||||
print!(
|
||||
"{}",
|
||||
String::from_utf8_lossy(
|
||||
@@ -413,7 +464,7 @@ fn print_console_info(port: u16, config: &CrateConfig, changed: Vec<PathBuf>) {
|
||||
"False"
|
||||
};
|
||||
|
||||
if changed.len() <= 0 {
|
||||
if options.changed.len() <= 0 {
|
||||
println!(
|
||||
"{} @ v{}\n",
|
||||
"Dioxus".bold().green(),
|
||||
@@ -422,7 +473,9 @@ fn print_console_info(port: u16, config: &CrateConfig, changed: Vec<PathBuf>) {
|
||||
} else {
|
||||
println!(
|
||||
"Project Reloaded: {}\n",
|
||||
format!("Changed {} files.", changed.len()).purple().bold()
|
||||
format!("Changed {} files.", options.changed.len())
|
||||
.purple()
|
||||
.bold()
|
||||
);
|
||||
}
|
||||
println!(
|
||||
@@ -446,12 +499,11 @@ fn print_console_info(port: u16, config: &CrateConfig, changed: Vec<PathBuf>) {
|
||||
println!("\t> URL Rewrite [index_on_404] : {}", url_rewrite.purple());
|
||||
println!("");
|
||||
|
||||
if changed.len() <= 0 {
|
||||
if options.changed.len() <= 0 {
|
||||
log::info!("{}", "Server startup completed.".green().bold());
|
||||
} else {
|
||||
log::info!("{}", "Project rebuild completed.".green().bold());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn get_ip() -> Option<String> {
|
||||
|
||||
Reference in new issue
Block a user