Skip to content

Commit

Permalink
feat: Add get_shim_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin Blackman committed Jan 1, 2024
1 parent cadddd7 commit fd60a88
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ cached which are configured in `Cargo.toml`.
`run-bin` can also be used as a library and paired nicely with your `build.rs` or any other scripts. The following
example demos having `dprint` configured within `[package.metadata.bin]`, and executing `dprint --help`.

```toml
[package.metadata.bin]
dprint = { version = "0.40.2" }
```

```rust
use anyhow::Result;
use cargo_run_bin::{binary, metadata};

fn run_dprint() -> Result<()> {
fn main() -> Result<()> {
let binary_package = metadata::get_binary_packages()?
.iter()
.find(|e| e.package == "dprint")
Expand All @@ -116,6 +121,34 @@ fn run_dprint() -> Result<()> {
}
```

Using `binary::run` is optional. You can recreate it and make changes to your liking using `std::process`, with shims included!

```rust
use std::process;

use anyhow::Result;
use cargo_run_bin::{binary, metadata, shims};

fn main() -> Result<()> {
let binary_package = metadata::get_binary_packages()?
.iter()
.find(|e| e.package == "dprint")
.unwrap()
.to_owned();
let bin_path = binary::install(binary_package)?;

let mut shell_paths = shims::get_shim_paths()?;
shell_paths.push(env::var("PATH").unwrap_or("".to_string()));

process::Command::new(bin_path)
.args(["--help"])
.env("PATH", shell_paths.join(":"))
.spawn();

return Ok(());
}
```

## [License](./LICENSE)

MIT.
27 changes: 3 additions & 24 deletions src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use which::which;

use crate::cargo_config;
use crate::metadata;
use crate::shims;

/// INTERNAL: Install binary with cargo install.
pub fn cargo_install(
Expand Down Expand Up @@ -174,30 +175,8 @@ pub fn run(bin_path: String, args: Vec<String>) -> Result<()> {
final_args.append(&mut args.clone());
}

let mut system_shell_paths = env::var("PATH")
.unwrap_or("".to_string())
.split(':')
.map(|e| return e.to_string())
.collect::<Vec<String>>();

let project_root = metadata::get_project_root()?;
let mut shell_paths = vec![];

let runbin = project_root
.join(".bin/.shims")
.to_string_lossy()
.to_string();
if !system_shell_paths.contains(&runbin) {
shell_paths.push(runbin);
}

// https://github.com/dustinblackman/cargo-gha
let gha = project_root.join(".gha/.shims");
if gha.exists() && !system_shell_paths.contains(&gha.to_string_lossy().to_string()) {
shell_paths.push(gha.to_string_lossy().to_string());
}

shell_paths.append(&mut system_shell_paths);
let mut shell_paths = shims::get_shim_paths()?;
shell_paths.push(env::var("PATH").unwrap_or("".to_string()));

let spawn = process::Command::new(bin_path.clone())
.stdout(process::Stdio::inherit())
Expand Down
36 changes: 35 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
//! `build.rs` or any other scripts. The following example demos having `dprint`
//! configured within `[package.metadata.bin]`, and executing `dprint --help`.
//!
//! ```toml
//! [package.metadata.bin]
//! dprint = { version = "0.40.2" }
//! ```
//!
//! ```rust
//! use anyhow::Result;
//! use cargo_run_bin::{binary, metadata};
//!
//! fn run_dprint() -> Result<()> {
//! fn main() -> Result<()> {
//! let binary_package = metadata::get_binary_packages()?
//! .iter()
//! .find(|e| e.package == "dprint")
Expand All @@ -32,6 +37,35 @@
//! return Ok(());
//! }
//! ```
//!
//! Using `binary::run` is optional. You can recreate it and make changes to
//! your liking using `std::process`, with shims included!
//!
//! ```rust
//! use std::process;
//!
//! use anyhow::Result;
//! use cargo_run_bin::{binary, metadata, shims};
//!
//! fn main() -> Result<()> {
//! let binary_package = metadata::get_binary_packages()?
//! .iter()
//! .find(|e| e.package == "dprint")
//! .unwrap()
//! .to_owned();
//! let bin_path = binary::install(binary_package)?;
//!
//! let mut shell_paths = shims::get_shim_paths()?;
//! shell_paths.push(env::var("PATH").unwrap_or("".to_string()));
//!
//! process::Command::new(bin_path)
//! .args(["--help"])
//! .env("PATH", shell_paths.join(":"))
//! .spawn();
//!
//! return Ok(());
//! }
//! ```
#![deny(clippy::implicit_return)]
#![allow(clippy::needless_return)]
Expand Down
29 changes: 29 additions & 0 deletions src/shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,32 @@ pub fn sync() -> Result<()> {

return Ok(());
}

/// Return an array of entries that can be added to PATH to provide shims to
/// other configured run-bin packages. Results be empty if entries already exist
/// in PATH.
pub fn get_shim_paths() -> Result<Vec<String>> {
let mut shim_paths = vec![];
let system_shell_paths = env::var("PATH")
.unwrap_or("".to_string())
.split(':')
.map(|e| return e.to_string())
.collect::<Vec<String>>();

let project_root = metadata::get_project_root()?;
let runbin = project_root
.join(".bin/.shims")
.to_string_lossy()
.to_string();

if !system_shell_paths.contains(&runbin) {
shim_paths.push(runbin);
}

let gha = project_root.join(".gha/.shims");
if gha.exists() && !system_shell_paths.contains(&gha.to_string_lossy().to_string()) {
shim_paths.push(gha.to_string_lossy().to_string());
}

return Ok(shim_paths);
}

0 comments on commit fd60a88

Please sign in to comment.