Skip to content

Commit

Permalink
feat(build/builtin): install src/bin scripts to bin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Dec 13, 2024
1 parent c4684e9 commit 73766f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo "Hello"
23 changes: 21 additions & 2 deletions rocks-lib/src/build/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -166,3 +172,16 @@ fn autodetect_modules(
})
.collect()
}

fn autodetect_bin_scripts(build_dir: &Path) -> Vec<PathBuf> {
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::<PathBuf>()
})
.collect()
}
4 changes: 4 additions & 0 deletions rocks-lib/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\""));
}
}

0 comments on commit 73766f3

Please sign in to comment.