Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use build.rs to build frontend resources #491

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
50 changes: 49 additions & 1 deletion cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
Expand All @@ -28,5 +28,53 @@ fn main() -> Result<(), Box<dyn Error>> {
};
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");
// 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");

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(())
}
2 changes: 1 addition & 1 deletion cli/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<'a> FormatDisplay<'a> {
eprintln!("Failed to start server: {}", e);
}
});

println!();
return Ok(());
}

Expand Down
22 changes: 14 additions & 8 deletions cli/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,14 +61,18 @@ pub async fn start_server_and_open_browser<'a>(explain_result: String) -> Result
start_server(port, explain_result.to_string()).await;
});

// 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.");
}
});
let url = format!("http://0.0.0.0:{}", port);
println!("Started a new server at: {url}");

// 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 {
if let Err(e) = webbrowser::open(&format!("http://127.0.0.1:{}", port)) {
println!("Failed to open browser, {} ", e);
}
});
}

// Continue with the rest of the code
server.await.expect("Server task failed");
Expand Down
Loading