Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 11 additions & 7 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ actix-http = "3"
actix-middleware-etag = "0.4.4"
actix-rt = "2"
actix-web = "4"
actix-web-static-files = "4"
actix-web-static-files = { git = "https://github.com/CommanderStorm/actix-web-static-files.git", branch = "11-unable-to-package-because-node_modules"}
anyhow = "1.0"
approx = "0.5.1"
async-trait = "0.1"
Expand Down Expand Up @@ -84,7 +84,7 @@ spreet = { version = "0.11", default-features = false }
sqlite-compressions = { version = "0.3", default-features = false, features = ["bsdiffraw", "gzip"] }
sqlite-hashes = { version = "0.10.6", default-features = false, features = ["md5", "aggregate", "hex"] }
sqlx = { version = "0.8.6", features = ["sqlite", "runtime-tokio"] }
static-files = "0.2"
static-files = { git = "https://github.com/static-files-rs/static-files.git", branch = "11-unable-to-package-because-node_modules"}
subst = { version = "0.3", features = ["yaml"] }
tempfile = "3.20.0"
testcontainers-modules = { version = "0.12.1", features = ["postgres"] }
Expand Down
1 change: 0 additions & 1 deletion martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ walkdir = { workspace = true, optional = true }
xxhash-rust.workspace = true

[build-dependencies]
walkdir = { workspace = true, optional = true }
static-files = { workspace = true, optional = true }

[dev-dependencies]
Expand Down
90 changes: 10 additions & 80 deletions martin/build.rs
Original file line number Diff line number Diff line change
@@ -1,94 +1,16 @@
#[cfg(feature = "webui")]
use std::fs;
#[cfg(feature = "webui")]
use std::path::Path;

#[cfg(feature = "webui")]
fn copy_file_tree(src: &Path, dst: &Path, exclude_dirs: &[&str]) {
assert!(
src.is_dir(),
"source for the copy operation is not an existing directory"
);
let _ = fs::remove_dir_all(dst); // ignore if dir does not exist
fs::create_dir_all(dst).unwrap_or_else(|e| {
panic!(
"failed to create destination directory {}: {e}",
dst.display()
)
});
let excludes = exclude_dirs.iter().map(|v| src.join(v)).collect::<Vec<_>>();

let mut it = walkdir::WalkDir::new(src).follow_links(true).into_iter();
while let Some(entry) = it.next() {
let entry = entry.expect("failed to read directory entry");
if excludes.iter().any(|v| v == entry.path()) {
it.skip_current_dir();
continue;
}

// Get the relative path of the entry
let dst_path = dst.join(
entry
.path()
.strip_prefix(src)
.expect("path is not a prefix of the source directory"),
);

if entry.file_type().is_dir() {
fs::create_dir_all(&dst_path).unwrap_or_else(|e| {
panic!(
"failed to create destination directory {}: {e}",
dst_path.display()
)
});
} else {
fs::copy(entry.path(), &dst_path).unwrap_or_else(|e| {
panic!(
"failed to copy file {} to {}: {e}",
entry.path().display(),
dst_path.display()
)
});
}
}
}

#[cfg(feature = "webui")]
fn webui() {
// rust requires that all changes are done in OUT_DIR.
//
// We thus need to
// - move the frontend code to the OUT_DIR
// - install npm dependencies
// - build the frontend
let martin_ui_dir = std::env::current_dir()
.expect("Unable to get current dir")
.join("martin-ui");
assert!(martin_ui_dir.is_dir(), "martin-ui directory does not exist");

let out_martin_ui_dir = std::env::var("OUT_DIR")
.expect("OUT_DIR environment variable is not set")
.parse::<std::path::PathBuf>()
.expect("OUT_DIR environment variable is not a valid path")
.join("martin-ui");

copy_file_tree(
&martin_ui_dir,
&out_martin_ui_dir,
&["dist", "node_modules"],
);

println!("installing and building in {}", out_martin_ui_dir.display());
static_files::NpmBuild::new(&out_martin_ui_dir)
.install()
.expect("npm install failed")
.run("build")
.expect("npm run build failed")
.target(out_martin_ui_dir.join("dist"))
.to_resource_dir()
.build()
.expect("failed to build webui npm dir");

let target_to_keep = martin_ui_dir.join("dist");
assert!(
!target_to_keep.exists() || target_to_keep.is_dir(),
Expand All @@ -97,8 +19,16 @@ fn webui() {

// TODO: we may need to move index.html one level down per change_detection() docs
static_files::NpmBuild::new(martin_ui_dir)
.target(&target_to_keep)
.change_detection();
.node_modules_strategy(Default::default())
.install()
.expect("npm install failed")
.run("build")
.expect("npm run build failed")
.target(target_to_keep)
.change_detection()
.to_resource_dir()
.build()
.expect("failed to build webui npm dir");
}

fn main() {
Expand Down
Loading