From 939a5a2f37b12df97920a685fce6689ef1a3e4d6 Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Fri, 25 Oct 2024 09:16:21 +0800 Subject: [PATCH 1/4] chore: use build.rs to build frontend resources --- .github/workflows/release.yml | 16 +++-------- cli/build.rs | 52 ++++++++++++++++++++++++++++++++++- cli/src/web.rs | 25 ++++++++++++----- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b2c781c0..1af0fbd9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,18 +24,6 @@ jobs: with: node-version: '18' - - name: Install frontend dependencies - working-directory: ./frontend - run: yarn install - - - name: Build frontend - working-directory: ./frontend - run: | - yarn build - rm -rf ../cli/frontend - mkdir -p ../cli/frontend - mv build ../cli/frontend/ - - uses: dtolnay/rust-toolchain@stable with: toolchain: stable @@ -48,6 +36,8 @@ jobs: sudo mv /tmp/cross /usr/local/bin/cross - name: Build shell: bash + env: + BUILD_FRONTEND: 1 run: | cross build --release --target=${{ matrix.target }} --bin=bendsql - name: Publish Binary @@ -81,6 +71,8 @@ jobs: targets: ${{ matrix.target }} - name: Build shell: bash + env: + BUILD_FRONTEND: 1 run: | cargo build --release --target=${{ matrix.target }} --bin=bendsql - name: Publish Binary diff --git a/cli/build.rs b/cli/build.rs index 2446e795..bb63d300 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{env, error::Error}; +use std::{env, error::Error, process::Command}; use vergen::EmitBuilder; fn main() -> Result<(), Box> { @@ -28,5 +28,55 @@ fn main() -> Result<(), Box> { }; println!("cargo:rustc-env=BENDSQL_BUILD_INFO={}", info); }); + + if env::var("BUILD_FRONTEND").is_ok() { + println!("cargo:warning=Start to build frontend dir via env BUILD_FRONTEND."); + let cwd = env::current_dir().expect("Failed to get current directory"); + println!("cargo:warning=Current Dir {:?}.", cwd.display()); + + env::set_current_dir("../frontend").expect("Failed to change directory to ../frontend"); + // 删除之前的构建目录 + let _ = Command::new("rm") + .arg("-rf") + .arg("../cli/frontend") + .status() + .expect("Failed to remove old frontend directory"); + + // 创建新的构建目录 + let _ = Command::new("mkdir") + .arg("-p") + .arg("../cli/frontend") + .status() + .expect("Failed to create frontend directory"); + + // 设置 Yarn 网络超时 + let _ = Command::new("yarn") + .arg("config") + .arg("set") + .arg("network-timeout") + .arg("600000") + .status() + .expect("Failed to set Yarn network timeout"); + + // 安装依赖并构建项目 + let _ = Command::new("yarn") + .arg("install") + .status() + .expect("Yarn install failed"); + + let _ = Command::new("yarn") + .arg("build") + .status() + .expect("Yarn build failed"); + + // 移动构建结果 + let _ = Command::new("mv") + .arg("build") + .arg("../cli/frontend/") + .status() + .expect("Failed to move build directory"); + + env::set_current_dir(cwd).unwrap(); + } Ok(()) } diff --git a/cli/src/web.rs b/cli/src/web.rs index 9f84ec80..d0ca10dc 100644 --- a/cli/src/web.rs +++ b/cli/src/web.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::env; + use actix_web::middleware::Logger; use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; use anyhow::Result; @@ -59,14 +61,23 @@ pub async fn start_server_and_open_browser<'a>(explain_result: String) -> Result start_server(port, explain_result.to_string()).await; }); + let url = format!("http://0.0.0.0:{}", port); + println!("Started a new server at: {url}"); + // Open the browser in a separate task - tokio::spawn(async move { - if webbrowser::open(&format!("http://127.0.0.1:{}", port)).is_ok() { - // eprintln!("Browser opened successfully at http://127.0.0.1:{}", port); - } else { - println!("Failed to open browser."); - } - }); + println!( + "env {} | {}", + env::var("SSH_CLIENT").is_ok(), + env::var("SSH_TTY").is_ok() + ); + let in_sshmode = env::var("SSH_CLIENT").is_ok() || env::var("SSH_TTY").is_ok(); + if !in_sshmode { + tokio::spawn(async move { + if !webbrowser::open(&format!("http://127.0.0.1:{}", port)).is_ok() { + println!("Failed to open browser."); + } + }); + } // Continue with the rest of the code server.await.expect("Server task failed"); From c21884e316d1f98ed1fbffbecfd624cbd58eaf0f Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Fri, 25 Oct 2024 09:17:58 +0800 Subject: [PATCH 2/4] chore: use build.rs to build frontend resources --- cli/build.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index bb63d300..6b017cf6 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -35,21 +35,20 @@ fn main() -> Result<(), Box> { println!("cargo:warning=Current Dir {:?}.", cwd.display()); env::set_current_dir("../frontend").expect("Failed to change directory to ../frontend"); - // 删除之前的构建目录 + // Clean old frontend directory let _ = Command::new("rm") .arg("-rf") .arg("../cli/frontend") .status() .expect("Failed to remove old frontend directory"); - // 创建新的构建目录 + // Mkdir new dir let _ = Command::new("mkdir") .arg("-p") .arg("../cli/frontend") .status() .expect("Failed to create frontend directory"); - // 设置 Yarn 网络超时 let _ = Command::new("yarn") .arg("config") .arg("set") @@ -58,7 +57,6 @@ fn main() -> Result<(), Box> { .status() .expect("Failed to set Yarn network timeout"); - // 安装依赖并构建项目 let _ = Command::new("yarn") .arg("install") .status() From 409fdb0c00168ca8dfe14ae5a4afcfabdc1501b3 Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Fri, 25 Oct 2024 09:20:20 +0800 Subject: [PATCH 3/4] chore: use build.rs to build frontend resources --- cli/src/display.rs | 2 +- cli/src/web.rs | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/cli/src/display.rs b/cli/src/display.rs index e3ffd7b4..91cdea99 100644 --- a/cli/src/display.rs +++ b/cli/src/display.rs @@ -153,7 +153,7 @@ impl<'a> FormatDisplay<'a> { eprintln!("Failed to start server: {}", e); } }); - + println!(""); return Ok(()); } diff --git a/cli/src/web.rs b/cli/src/web.rs index d0ca10dc..224ed044 100644 --- a/cli/src/web.rs +++ b/cli/src/web.rs @@ -64,12 +64,7 @@ pub async fn start_server_and_open_browser<'a>(explain_result: String) -> Result let url = format!("http://0.0.0.0:{}", port); println!("Started a new server at: {url}"); - // Open the browser in a separate task - println!( - "env {} | {}", - env::var("SSH_CLIENT").is_ok(), - env::var("SSH_TTY").is_ok() - ); + // Open the browser in a separate task if not in ssh mode let in_sshmode = env::var("SSH_CLIENT").is_ok() || env::var("SSH_TTY").is_ok(); if !in_sshmode { tokio::spawn(async move { From 20f878286eeba92618cf2d12d04f9274b44b7d64 Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Fri, 25 Oct 2024 09:36:01 +0800 Subject: [PATCH 4/4] chore: use build.rs to build frontend resources --- cli/src/display.rs | 2 +- cli/src/web.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/src/display.rs b/cli/src/display.rs index 91cdea99..95ba60f2 100644 --- a/cli/src/display.rs +++ b/cli/src/display.rs @@ -153,7 +153,7 @@ impl<'a> FormatDisplay<'a> { eprintln!("Failed to start server: {}", e); } }); - println!(""); + println!(); return Ok(()); } diff --git a/cli/src/web.rs b/cli/src/web.rs index 224ed044..8caafb55 100644 --- a/cli/src/web.rs +++ b/cli/src/web.rs @@ -68,8 +68,8 @@ pub async fn start_server_and_open_browser<'a>(explain_result: String) -> Result let in_sshmode = env::var("SSH_CLIENT").is_ok() || env::var("SSH_TTY").is_ok(); if !in_sshmode { tokio::spawn(async move { - if !webbrowser::open(&format!("http://127.0.0.1:{}", port)).is_ok() { - println!("Failed to open browser."); + if let Err(e) = webbrowser::open(&format!("http://127.0.0.1:{}", port)) { + println!("Failed to open browser, {} ", e); } }); }