فهرست منبع

feat: commit prettier_build

YuKun Liu 3 سال پیش
والد
کامیت
dfe571df1a
3فایلهای تغییر یافته به همراه38 افزوده شده و 49 حذف شده
  1. 1 1
      Cargo.toml
  2. 37 21
      src/builder.rs
  3. 0 27
      src/cargo.rs

+ 1 - 1
Cargo.toml

@@ -36,7 +36,7 @@ regex = "1.5.4"
 chrono = "0.4.19"
 anyhow = "1.0.53"
 hyper = "0.14.17"
-indicatif = "0.16.2"
+loading = "0.3.0"
 
 axum = { version = "0.5.1", features = ["ws", "headers"] }
 tower-http = { version = "0.2.2", features = ["fs", "trace"] }

+ 37 - 21
src/builder.rs

@@ -2,11 +2,14 @@ use crate::{
     config::{CrateConfig, ExecutableType},
     error::{Error, Result},
     tools::Tool,
-    CargoFormatInfo, DioxusConfig,
+    DioxusConfig,
 };
+use cargo_metadata::Message;
+use loading::Loading;
 use std::{
+    collections::HashMap,
     fs::{copy, create_dir_all, remove_dir_all, File},
-    io::{BufRead, Read},
+    io::Read,
     panic,
     path::PathBuf,
     process::Command,
@@ -75,25 +78,7 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result<()> {
         ExecutableType::Example(name) => cmd.arg("--example").arg(name),
     };
 
-    let mut child = cmd.spawn()?;
-    let out = child.stdout.take().unwrap();
-    let mut out = std::io::BufReader::new(out);
-    let mut content = String::new();
-    while let Ok(_) = out.read_line(&mut content) {
-        // 进程退出后结束循环
-        if let Ok(Some(_)) = child.try_wait() {
-            break;
-        }
-        let content = content.split('\n').collect::<Vec<&str>>();
-        for json in content {
-            let d = serde_json::from_str::<CargoFormatInfo>(&json);
-            if let Ok(d) = d {
-                println!("\n{:#?}\n", d);
-            } else {
-                println!("\n\n{:?}\n\n", json);
-            }
-        }
-    }
+    prettier_build(cmd).unwrap();
 
     // if !output.status.success() {
     //     log::error!("Build failed!");
@@ -342,6 +327,37 @@ 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();
+    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::CompilerArtifact(artifact) => {
+                record.insert(artifact.package_id.to_string(), artifact.target.name);
+            }
+            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));
+                }
+            }
+            Message::BuildFinished(finished) => {
+                if finished.success {
+                } else {
+                    std::process::exit(1);
+                }
+            }
+            _ => (), // Unknown message
+        }
+    }
+    Ok(())
+}
+
 pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
     let crate_root = crate::cargo::crate_root().unwrap();
     let custom_html_file = crate_root.join("index.html");

+ 0 - 27
src/cargo.rs

@@ -1,6 +1,4 @@
 //! Utilities for working with cargo and rust files
-use serde::{Serialize, Deserialize};
-
 use crate::error::{Error, Result};
 use std::{
     env, fs,
@@ -92,28 +90,3 @@ impl Metadata {
         Err(Error::CargoError("InvalidOutput".to_string()))
     }
 }
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct CargoFormatInfo {
-    pub reason: CargoFormatReason,
-    #[serde(default)]
-    pub package_id: String,
-    #[serde(default)]
-    manifest_path: String,
-    #[serde(default)]
-    target: Option<serde_json::Value>,
-    #[serde(default)]
-    message: Option<serde_json::Value>,
-}
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub enum CargoFormatReason {
-    #[serde(rename = "compiler-message")]
-    CompilerMessage,
-    #[serde(rename = "compiler-artifact")]
-    CompilerArtifact,
-    #[serde(rename = "build-script-executed")]
-    BuildScriptExecuted,
-    #[serde(rename = "build-finished")]
-    BuildFinished,
-}