From 73766f391926e6c30631a17cb91e3e50aed7cdf3 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Wed, 4 Dec 2024 17:06:28 +0100 Subject: [PATCH] feat(build/builtin): install `src/bin` scripts to `bin` --- .../src/bin/hello | 2 ++ rocks-lib/src/build/builtin.rs | 23 +++++++++++++++++-- rocks-lib/src/build/mod.rs | 4 ++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 rocks-lib/resources/test/sample-project-no-build-spec/src/bin/hello diff --git a/rocks-lib/resources/test/sample-project-no-build-spec/src/bin/hello b/rocks-lib/resources/test/sample-project-no-build-spec/src/bin/hello new file mode 100644 index 0000000..626c6f3 --- /dev/null +++ b/rocks-lib/resources/test/sample-project-no-build-spec/src/bin/hello @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +echo "Hello" diff --git a/rocks-lib/src/build/builtin.rs b/rocks-lib/src/build/builtin.rs index f4a372d..92ba17f 100644 --- a/rocks-lib/src/build/builtin.rs +++ b/rocks-lib/src/build/builtin.rs @@ -40,7 +40,7 @@ impl Build for BuiltinBuildSpec { progress.map(|p| p.set_position(modules.len() as u64)); - for (counter, (destination_path, module_type)) in modules.iter().enumerate() { + for (destination_path, module_type) in modules.iter() { match module_type { ModuleSpec::SourcePath(source) => { if source.extension().map(|ext| ext == "c").unwrap_or(false) { @@ -96,7 +96,13 @@ impl Build for BuiltinBuildSpec { )? } } - progress.map(|p| p.set_position(counter as u64)); + } + + for relative_path in autodetect_bin_scripts(build_dir).iter() { + let source = build_dir.join("src").join("bin").join(relative_path); + let target = output_paths.bin.join(relative_path); + std::fs::create_dir_all(target.parent().unwrap())?; + std::fs::copy(source, target)?; } Ok(()) @@ -166,3 +172,16 @@ fn autodetect_modules( }) .collect() } + +fn autodetect_bin_scripts(build_dir: &Path) -> Vec { + WalkDir::new(build_dir.join("src").join("bin")) + .into_iter() + .filter_map(|file| file.ok()) + .filter(|file| file.clone().into_path().is_file()) + .map(|file| { + let diff = pathdiff::diff_paths(build_dir.join(file.into_path()), build_dir) + .expect("failed to autodetect bin scripts"); + diff.components().skip(2).collect::() + }) + .collect() +} diff --git a/rocks-lib/src/build/mod.rs b/rocks-lib/src/build/mod.rs index 4028a94..319a007 100644 --- a/rocks-lib/src/build/mod.rs +++ b/rocks-lib/src/build/mod.rs @@ -299,5 +299,9 @@ mod tests { let foo_bar_baz = foo_bar_dir.child("baz.lua"); foo_bar_baz.assert(predicate::path::is_file()); foo_bar_baz.assert(predicate::str::contains("return true")); + let bin_file = dest_dir.child("bin").child("hello"); + bin_file.assert(predicate::path::is_file()); + bin_file.assert(predicate::str::contains("#!/usr/bin/env bash")); + bin_file.assert(predicate::str::contains("echo \"Hello\"")); } }