Skip to content

Commit

Permalink
Move PIXI_BIN_NAME to pixi_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zbowling committed Dec 30, 2024
1 parent 9e86416 commit 53ee1f4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/pixi_manifest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ indexmap = { workspace = true }
itertools = { workspace = true }
pep440_rs = { workspace = true }
pep508_rs = { workspace = true }
pixi_utils = { workspace = true }
pixi_consts = { workspace = true }
pixi_spec = { workspace = true }
regex = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/pixi_manifest/src/manifests/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Manifest {
let (Some(name), spec) = spec.clone().into_nameless() else {
miette::bail!(
"{} does not support wildcard dependencies",
consts::PIXI_BIN_NAME.as_str()
pixi_utils::executable_name()
);
};
let spec = PixiSpec::from_nameless_matchspec(spec, channel_config);
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fd-lock = { workspace = true }
fs-err = { workspace = true }
indicatif = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
miette = { workspace = true }
pep508_rs = { workspace = true }
pixi_config = { workspace = true }
Expand Down
19 changes: 18 additions & 1 deletion crates/pixi_utils/src/executable_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
use std::path::Path;
use lazy_static::lazy_static;
use std::{ffi::OsStr, path::Path};

lazy_static! {
/// The name of the binary.
pub static ref PIXI_BIN_NAME: String = std::env::args().next()
.as_ref()
.map(Path::new)
.and_then(Path::file_stem)
.and_then(OsStr::to_str)
.map(String::from).unwrap_or("pixi".to_string());
}

/// Returns the name of the binary. Since this is used in errors, it resolves to "pixi" if it can't
/// find the resolve name rather than return an error itself.
pub fn executable_name() -> &'static str {
PIXI_BIN_NAME.as_str()
}

/// Strips known Windows executable extensions from a file name.
pub(crate) fn strip_windows_executable_extension(file_name: String) -> String {
Expand Down
4 changes: 3 additions & 1 deletion crates/pixi_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ mod prefix_guard;
pub mod reqwest;

mod executable_utils;
pub use executable_utils::{executable_from_path, is_binary_folder, strip_executable_extension};
pub use executable_utils::{
executable_from_path, executable_name, is_binary_folder, strip_executable_extension,
};

pub use cache::EnvironmentHash;
pub use prefix_guard::{PrefixGuard, WriteGuard};
28 changes: 10 additions & 18 deletions src/cli/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn execute(args: Args) -> miette::Result<()> {
/// Generate the completion script using clap_complete for a specified shell.
fn get_completion_script(shell: Shell) -> String {
let mut buf = vec![];
let bin_name: &str = pixi_consts::consts::PIXI_BIN_NAME.as_str();
let bin_name: &str = pixi_utils::executable_name();
clap_complete::generate(shell, &mut CommandArgs::command(), bin_name, &mut buf);
String::from_utf8(buf).expect("clap_complete did not generate a valid UTF8 script")
}
Expand All @@ -92,7 +92,7 @@ fn replace_bash_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
// NOTE THIS IS FORMATTED BY HAND
// Replace the '-' with '__' since that's what clap's generator does as well for Bash Shell completion.
let bin_name: &str = pixi_consts::consts::PIXI_BIN_NAME.as_str();
let bin_name: &str = pixi_utils::executable_name();
let clap_name = bin_name.replace("-", "__");
let pattern = format!(r#"(?s){}__run\).*?opts="(.*?)".*?(if.*?fi)"#, &clap_name);
let replacement = r#"CLAP_NAME__run)
Expand Down Expand Up @@ -121,7 +121,7 @@ fn replace_zsh_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
// NOTE THIS IS FORMATTED BY HAND
let pattern = r"(?ms)(\(run\))(?:.*?)(_arguments.*?)(\*::task)";
let bin_name: &str = pixi_consts::consts::PIXI_BIN_NAME.as_str();
let bin_name: &str = pixi_utils::executable_name();
let replacement = r#"$1
local tasks
tasks=("$${(@s/ /)$$(BIN_NAME task list --machine-readable 2> /dev/null)}")
Expand All @@ -139,7 +139,7 @@ $2::task"#;

fn replace_fish_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
let bin_name = pixi_consts::consts::PIXI_BIN_NAME.as_str();
let bin_name = pixi_utils::executable_name();
let addition = format!("complete -c {} -n \"__fish_seen_subcommand_from run\" -f -a \"(string split ' ' ({} task list --machine-readable 2> /dev/null))\"", bin_name, bin_name);
let new_script = format!("{}{}\n", script, addition);
let pattern = r#"-n "__fish_seen_subcommand_from run""#;
Expand All @@ -153,7 +153,7 @@ fn replace_fish_completion(script: &str) -> Cow<str> {
fn replace_nushell_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
// NOTE THIS IS FORMATTED BY HAND
let bin_name = pixi_consts::consts::PIXI_BIN_NAME.as_str();
let bin_name = pixi_utils::executable_name();
let pattern = format!(
r#"(#.*\n export extern "{} run".*\n.*...task: string)([^\]]*--environment\(-e\): string)"#,
bin_name
Expand Down Expand Up @@ -216,7 +216,7 @@ _arguments "${_arguments_options[@]}" \
"#;
let result = replace_zsh_completion(script);
let replacement = format!("{} task list", pixi_consts::consts::PIXI_BIN_NAME.as_str());
let replacement = format!("{} task list", pixi_utils::executable_name());
insta::with_settings!({filters => vec![
(replacement.as_str(), "pixi task list"),
]}, {
Expand Down Expand Up @@ -273,13 +273,8 @@ _arguments "${_arguments_options[@]}" \
;;
"#;
let result = replace_bash_completion(script);
let replacement = format!("{} task list", pixi_consts::consts::PIXI_BIN_NAME.as_str());
let zsh_arg_name = format!(
"{}__",
pixi_consts::consts::PIXI_BIN_NAME
.as_str()
.replace("-", "__")
);
let replacement = format!("{} task list", pixi_utils::executable_name());
let zsh_arg_name = format!("{}__", pixi_utils::executable_name().replace("-", "__"));
println!("{}", result);
insta::with_settings!({filters => vec![
(replacement.as_str(), "pixi task list"),
Expand Down Expand Up @@ -318,11 +313,8 @@ _arguments "${_arguments_options[@]}" \
--help(-h) # Print help (see more with '--help')
]"#;
let result = replace_nushell_completion(script);
let replacement = format!("{} run", pixi_consts::consts::PIXI_BIN_NAME.as_str());
let nu_complete_run = format!(
"nu-complete {} run",
pixi_consts::consts::PIXI_BIN_NAME.as_str()
);
let replacement = format!("{} run", pixi_utils::executable_name());
let nu_complete_run = format!("nu-complete {} run", pixi_utils::executable_name());
println!("{}", result);
insta::with_settings!({filters => vec![
(replacement.as_str(), "[PIXI RUN]"),
Expand Down
2 changes: 1 addition & 1 deletion src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Project {
"could not find {} or {} which is configured to use {}",
consts::PROJECT_MANIFEST,
consts::PYPROJECT_MANIFEST,
consts::PIXI_BIN_NAME.as_str()
pixi_utils::executable_name()
);
}

Expand Down

0 comments on commit 53ee1f4

Please sign in to comment.