From c484a918b573f79d515ef5198486ce0e9085e553 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Mon, 3 Aug 2026 17:29:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20build.rs=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9E=84=E5=BB=BA=E5=89=8D=E7=AB=AF=EF=BC=8C?= =?UTF-8?q?cargo=20build=20=E6=97=B6=E8=87=AA=E5=8A=A8=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=20npm=20install=20+=20npm=20run=20build?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 +- build.rs | 63 +++++++++++++++++++++++++++++++++++++++ build.sh | 10 +------ scripts/build-release.ps1 | 22 ++++---------- 4 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 build.rs diff --git a/Makefile b/Makefile index 6469a5a..6d9f67d 100644 --- a/Makefile +++ b/Makefile @@ -28,10 +28,9 @@ dev-frontend: @echo "Starting frontend dev server..." cd web && npm run dev -# Build for production +# Build for production (frontend is built automatically by cargo via build.rs) build: @echo "Building PicoBot Web UI..." - cd web && npm run build cargo build --release @echo "Build complete!" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..4f00a0b --- /dev/null +++ b/build.rs @@ -0,0 +1,63 @@ +use std::path::Path; +use std::process::Command; + +fn run_npm(args: &[&str], web_dir: &Path) { + let is_windows = cfg!(target_os = "windows"); + + if is_windows { + let mut cmd_args = vec!["/c", "npm"]; + cmd_args.extend_from_slice(args); + let status = Command::new("cmd") + .args(&cmd_args) + .current_dir(web_dir) + .status() + .unwrap_or_else(|e| panic!("failed to spawn npm {}: {}", args.join(" "), e)); + + if !status.success() { + panic!("npm {} failed with status {}", args.join(" "), status); + } + } else { + let status = Command::new("npm") + .args(args) + .current_dir(web_dir) + .status() + .unwrap_or_else(|e| panic!("failed to spawn npm {}: {}", args.join(" "), e)); + + if !status.success() { + panic!("npm {} failed with status {}", args.join(" "), status); + } + } +} + +fn main() { + println!("cargo:rerun-if-env-changed=SKIP_FRONTEND_BUILD"); + + let web_dir = Path::new("web"); + if web_dir.exists() { + println!("cargo:rerun-if-changed=web/src"); + println!("cargo:rerun-if-changed=web/index.html"); + println!("cargo:rerun-if-changed=web/package.json"); + println!("cargo:rerun-if-changed=web/vite.config.ts"); + println!("cargo:rerun-if-changed=web/tailwind.config.js"); + println!("cargo:rerun-if-changed=web/postcss.config.js"); + println!("cargo:rerun-if-changed=web/tsconfig.json"); + } + + if std::env::var("SKIP_FRONTEND_BUILD").is_ok() { + println!("cargo:warning=SKIP_FRONTEND_BUILD is set, skipping frontend build"); + return; + } + + if !web_dir.exists() { + println!("cargo:warning=web/ directory not found, skipping frontend build"); + return; + } + + println!("cargo:warning=building frontend (npm install)..."); + run_npm(&["install"], web_dir); + + println!("cargo:warning=building frontend (npm run build)..."); + run_npm(&["run", "build"], web_dir); + + println!("cargo:warning=frontend build complete, output in static/"); +} diff --git a/build.sh b/build.sh index f57e693..c23dafc 100644 --- a/build.sh +++ b/build.sh @@ -12,15 +12,7 @@ if [ ! -f "Cargo.toml" ]; then fi echo "" -echo "Step 1: Building frontend..." -echo "----------------------------------------" -cd web -npm install -npm run build -cd .. - -echo "" -echo "Step 2: Building Rust backend..." +echo "Building PicoBot (frontend is built automatically by cargo via build.rs)..." echo "----------------------------------------" cargo build --release diff --git a/scripts/build-release.ps1 b/scripts/build-release.ps1 index b6b24a4..dd259cb 100644 --- a/scripts/build-release.ps1 +++ b/scripts/build-release.ps1 @@ -14,18 +14,8 @@ $DistDir = "$Root\dist\$PackageName" Write-Host "=== PicoBot v$Version Build ===" -ForegroundColor Cyan -# Step 1: build frontend -Write-Host "[1/4] Building web frontend..." -ForegroundColor Yellow -Push-Location "$Root\web" -try { - npm run build - if ($LASTEXITCODE -ne 0) { throw "web build failed" } -} finally { - Pop-Location -} - -# Step 2: build Rust backend -Write-Host "[2/4] Building Rust release..." -ForegroundColor Yellow +# Build Rust backend (frontend is built automatically via build.rs) +Write-Host "[1/3] Building PicoBot (frontend auto-built by cargo)..." -ForegroundColor Yellow Push-Location $Root try { cargo build --release @@ -34,16 +24,16 @@ try { Pop-Location } -# Step 3: assemble dist directory -Write-Host "[3/4] Assembling package..." -ForegroundColor Yellow +# Assemble dist directory +Write-Host "[2/3] Assembling package..." -ForegroundColor Yellow New-Item -ItemType Directory -Force -Path $DistDir | Out-Null # exe + package contents Copy-Item "$Root\target\release\picobot.exe" $DistDir Copy-Item "$Root\package\*" $DistDir -# Step 4: zip -Write-Host "[4/4] Creating zip..." -ForegroundColor Yellow +# Create zip +Write-Host "[3/3] Creating zip..." -ForegroundColor Yellow $ZipPath = "$Root\dist\$PackageName.zip" if (Test-Path $ZipPath) { Remove-Item $ZipPath } Compress-Archive -Path $DistDir -DestinationPath $ZipPath