Skip to content

Commit

Permalink
Modifying build script to copy frontend to OUT_DIR and avoid building…
Browse files Browse the repository at this point in the history
… the frontend.
  • Loading branch information
TalRoni committed Sep 8, 2023
1 parent bee9594 commit 933730d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = """
Crates Registry is a tool for serve crates and rustup installation in offline networks.
The project is based on panamax project and cargo-http-registry and use theis code.
"""
include = ["/frontend/build", "/src", "build.rs"]

[dependencies]
anyhow = "1.0"
Expand Down
56 changes: 22 additions & 34 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
use std::process::Command;
use std::path::{Path, PathBuf};

#[cfg(target_os = "windows")]
fn build_frontend() {
Command::new("powershell")
.arg("-Command")
.arg("yarn install")
.current_dir("frontend")
.output()
.expect("Failed to install frontend dependencies (windows target)");
Command::new("powershell")
.arg("-Command")
.arg("yarn build")
.current_dir("frontend")
.output()
.expect("Failed to build the frontend (windows target)");
}

#[cfg(target_os = "linux")]
fn build_frontend() {
Command::new("yarn")
.arg("install")
.current_dir("frontend")
.output()
.expect("Failed to install frontend dependencies (linux target)");
Command::new("yarn")
.arg("build")
.current_dir("frontend")
.output()
.expect("Failed to build the frontend (linux target)");
/// Copy files from source to destination recursively.
pub fn copy_folder(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> std::io::Result<()> {
std::fs::create_dir_all(&destination)?;
for entry in std::fs::read_dir(source)? {
let entry = entry?;
let filetype = entry.file_type()?;
if filetype.is_dir() {
copy_folder(entry.path(), destination.as_ref().join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}

fn main() {
if std::env::var("SKIP_BUILDING_FRONTEND").is_ok() {
return;
println!("cargo:rerun-if-changed=frontend/build");
let frontend_dist_folder = PathBuf::from("frontend/build");
if !frontend_dist_folder.exists() {
eprintln!("You must build the Frontend first. please see the relevant script command in `frontend/package.json`.");
}
println!("cargo:rerun-if-changed=frontend/src");
eprintln!("Require yarn.");
build_frontend()

let frontend_dist_folder_out =
PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("frontend_dist_folder");
copy_folder(frontend_dist_folder, frontend_dist_folder_out).unwrap();
}
2 changes: 1 addition & 1 deletion src/serve_frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use warp::Filter;
use crate::serve::ServerError;
use crate::unpack;

static FRONTEND: Dir<'_> = include_dir!("frontend/build/");
static FRONTEND: Dir<'_> = include_dir!("$OUT_DIR/frontend_dist_folder/");

fn available_platforms(root: &Path) -> Result<Vec<String>> {
Ok(std::fs::read_dir(root.join("rustup").join("dist"))?
Expand Down

0 comments on commit 933730d

Please sign in to comment.