coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 1bde715086916133a351063f244377ed4f5d43dd
parent c580ff64f04f9bafbb9454696b1ec766d7855287
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Fri, 21 Jun 2024 13:29:59 -0700

Implement run command for Unix platforms

Diffstat:
Msrc/core.rs | 30++++++++++++++++++++++++------
Msrc/utils.rs | 16++++++++++++++++
2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/core.rs b/src/core.rs @@ -1,8 +1,9 @@ use std::env::set_current_dir; use std::path::Path; -use super::manifest::{CopyLinkOptions, Manifest, parse_manifest_file}; +use super::manifest::{CopyLinkOptions, RunOptions, Manifest, parse_manifest_file +}; use super::tags::tags_match; -use super::utils::{copy_file, link_file}; +use super::utils::{copy_file, link_file, run_script}; /// Execute the steps in a coliru manifest file according to a set of tag rules pub fn execute_manifest_file(path: &Path, tag_rules: Vec<String>, dry_run: bool, @@ -35,6 +36,7 @@ fn execute_manifest(manifest: Manifest, tag_rules: Vec<String>, dry_run: bool, } else { execute_links(&step.link, dry_run); } + execute_runs(&step.run, dry_run); } } @@ -55,16 +57,32 @@ fn execute_copies(copies: &[CopyLinkOptions], dry_run: bool) { } /// Execute the link commands specified in a coliru manifest step -fn execute_links(copies: &[CopyLinkOptions], dry_run: bool) { - for copy in copies { - print!(" Link {} to {}", copy.src, copy.dst); +fn execute_links(links: &[CopyLinkOptions], dry_run: bool) { + for link in links { + print!(" Link {} to {}", link.src, link.dst); + if dry_run { + println!(" (skipped due to --dry-run)"); + return; + } + println!(""); + + if let Err(why) = link_file(&link.src, &link.dst) { + eprintln!(" Error: {}", why); + } + } +} + +/// Execute the run commands specified in a coliru manifest step +fn execute_runs(runs: &[RunOptions], dry_run: bool) { + for run in runs { + print!(" Run {}", run.src); if dry_run { println!(" (skipped due to --dry-run)"); return; } println!(""); - if let Err(why) = link_file(&copy.src, &copy.dst) { + if let Err(why) = run_script(&run.src) { eprintln!(" Error: {}", why); } } diff --git a/src/utils.rs b/src/utils.rs @@ -4,6 +4,7 @@ use std::fs; #[cfg(target_family = "unix")] use std::os::unix::fs::symlink; use std::path::PathBuf; +use std::process::Command; /// Copies the contents of a local file to another local file. @@ -46,3 +47,18 @@ fn prepare_path(path: &str) -> Result<PathBuf> { } Ok(_dst) } + +/// Execute a local shell script +/// +/// Uses "sh -c" on Unix and is not yet implemented on Windows. +pub fn run_script(path: &str) -> Result<()> { + if cfg!(target_family = "unix") { + Command::new("sh") + .arg("-c") + .arg(fs::canonicalize(path)?) + .status()?; + } else { + unimplemented!(); + } + Ok(()) +}