Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
id: generate_token
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3
with:
app-id: ${{ secrets.BOT_APP_ID }}
client-id: ${{ secrets.BOT_APP_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}

- name: Checkout repository
Expand Down Expand Up @@ -105,7 +105,7 @@ jobs:
id: generate_token
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3
with:
app-id: ${{ secrets.BOT_APP_ID }}
client-id: ${{ secrets.BOT_APP_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}

- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Init {
// Call use programmatically
let use_cmd = crate::commands::use_cmd::Use {
version: Some(selected.clone()),
silent: false,
};
use_cmd.call().await?;
}
Expand Down
29 changes: 26 additions & 3 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ pub struct Install {
}

pub async fn execute_install(version: &str) -> Result<()> {
execute_install_with(version, true).await.map(|_| ())
}

/// `prompt_activation` controls the trailing "Do you want to use PHP X now?" prompt and the
/// resulting env-file write. Callers like `pvm use` set it to `false` because they will fall
/// through to their own activation path with the returned resolved version.
pub async fn execute_install_with(
version: &str,
prompt_activation: bool,
) -> Result<Option<String>> {
let versions_dir = fs::get_versions_dir()?;
std::fs::create_dir_all(&versions_dir)?;

Expand Down Expand Up @@ -50,7 +60,7 @@ pub async fn execute_install(version: &str) -> Result<()> {

if selections.is_empty() {
println!("{} No packages selected. Operation cancelled.", "✗".red());
return Ok(());
return Ok(None);
}

let selected_packages: Vec<String> = selections
Expand Down Expand Up @@ -94,6 +104,18 @@ pub async fn execute_install(version: &str) -> Result<()> {

// Only the cli package places a `php` binary on PATH; without it, switching is meaningless.
let cli_selected = selected_packages.iter().any(|p| p == "cli");

if !prompt_activation {
if !cli_selected {
println!(
"{} The 'cli' package was not selected; this version cannot be activated via PATH.",
"💡".yellow()
);
return Ok(None);
}
return Ok(Some(resolved_version));
}

let use_now = cli_selected
&& dialoguer::Confirm::with_theme(&theme)
.with_prompt(
Expand All @@ -120,20 +142,21 @@ pub async fn execute_install(version: &str) -> Result<()> {
// is unsound in a multi-threaded tokio runtime, and the wrapper sources env_file
// into the parent shell on exit, so subsequent pvm invocations see the new PATH.
println!("{} Switched to PHP {}", "✓".green(), v.bold());
Ok(Some(resolved_version))
} else if !cli_selected {
println!(
"{} The 'cli' package was not selected; this version cannot be activated via PATH.",
"💡".yellow()
);
Ok(None)
} else {
println!(
"{} To use this version later, run `{}`",
"💡".yellow(),
format!("pvm use {}", version).bold()
);
Ok(Some(resolved_version))
}

Ok(())
}

impl Install {
Expand Down
35 changes: 34 additions & 1 deletion src/commands/use_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,45 @@ use std::path::Path;
pub struct Use {
/// The version to use (omit for interactive list)
pub version: Option<String>,

/// Skip interactive prompts when the requested version is missing (used by shell hooks).
#[arg(long, hide = true)]
pub silent: bool,
}

impl Use {
pub async fn call(self) -> Result<()> {
let mut version = match self.version {
Some(ref v) => fs::resolve_local_version(v)?,
Some(ref v) => match fs::try_resolve_local_version(v)? {
Some(resolved) => resolved,
None => {
if self.silent {
return Ok(());
}

let prompt = format!(
"PHP {} is not installed locally. Do you want to install it now?",
v.bold()
);
let install_now = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(&prompt)
.default(true)
.interact_opt()?
.unwrap_or(false);

if !install_now {
eprintln!("{} Operation cancelled.", "✗".red());
return Ok(());
}

// Skip install's own "use now?" prompt — we fall through to
// the activation path below with the freshly installed version.
match crate::commands::install::execute_install_with(v, false).await? {
Some(installed) => installed,
None => return Ok(()),
}
}
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
None => {
let items = fs::get_aliased_versions()?;
if items.is_empty() {
Expand Down
23 changes: 14 additions & 9 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,17 @@ pub fn get_aliased_versions() -> Result<Vec<VersionItem>> {
Ok(items)
}

pub fn resolve_local_version(requested: &str) -> Result<String> {
pub fn try_resolve_local_version(requested: &str) -> Result<Option<String>> {
if requested == "latest" {
return get_aliased_versions()?
return Ok(get_aliased_versions()?
.into_iter()
.find(|item| item.display.starts_with("latest"))
.map(|item| item.version)
.ok_or_else(|| anyhow::anyhow!("No PHP versions are currently installed."));
.map(|item| item.version));
}

let installed = list_installed_versions()?;
if installed.contains(&requested.to_string()) {
return Ok(requested.to_string());
return Ok(Some(requested.to_string()));
}

let prefix = format!("{}.", requested);
Expand All @@ -192,11 +191,17 @@ pub fn resolve_local_version(requested: &str) -> Result<String> {
.filter(|v| v.starts_with(&prefix))
.collect();

if let Some(latest) = matching.last() {
return Ok((*latest).clone());
}
Ok(matching.last().map(|s| (*s).clone()))
}

anyhow::bail!("PHP {} is not installed locally.", requested)
pub fn resolve_local_version(requested: &str) -> Result<String> {
match try_resolve_local_version(requested)? {
Some(v) => Ok(v),
None if requested == "latest" => {
anyhow::bail!("No PHP versions are currently installed.")
}
None => anyhow::bail!("PHP {} is not installed locally.", requested),
}
}

#[cfg(test)]
Expand Down
7 changes: 5 additions & 2 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub async fn run_root_menu() -> Result<()> {

let res = match choice {
0 => {
let cmd = commands::use_cmd::Use { version: None };
let cmd = commands::use_cmd::Use {
version: None,
silent: false,
};
cmd.call().await
}
1 => {
Expand All @@ -46,7 +49,7 @@ pub async fn run_root_menu() -> Result<()> {
cmd.call().await
}
3 => {
let cmd = commands::use_cmd::Use { version: None };
let cmd = commands::ls::Ls;
cmd.call().await
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
4 => {
Expand Down
6 changes: 3 additions & 3 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Shell for Bash {
"
_pvm_cd_hook() {
if [[ -f .php-version ]]; then
pvm use \"$(cat .php-version)\" || true
pvm use --silent \"$(cat .php-version)\" || true
fi
}
if [[ -n \"$BASH_VERSION\" ]]; then
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Shell for Zsh {
"
_pvm_cd_hook() {
if [[ -f .php-version ]]; then
pvm use \"$(cat .php-version)\" || true
pvm use --silent \"$(cat .php-version)\" || true
fi
}
autoload -U add-zsh-hook
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Shell for Fish {
"
function _pvm_cd_hook --on-variable PWD
if test -f .php-version
pvm use (cat .php-version)
pvm use --silent (cat .php-version)
end
end
"
Expand Down
24 changes: 24 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,27 @@ fn test_use_silent_export() {
.success()
.stdout(predicate::str::contains("export PVM_MULTISHELL_PATH").not());
}

#[test]
fn test_use_silent_skips_missing_version() {
let temp_dir = tempfile::tempdir().unwrap();
let env_file = temp_dir.path().join("custom_env_update");

let mut cmd = assert_cmd::cargo::cargo_bin_cmd!("pvm");
cmd.env("PVM_DIR", temp_dir.path());
cmd.env("PVM_UPDATE_MODE", "disabled");
cmd.env("PVM_ENV_UPDATE_PATH", &env_file);
cmd.current_dir(temp_dir.path());
cmd.arg("use").arg("--silent").arg("8.3");

// Silent mode: missing version exits 0 with no output and no env file written.
cmd.assert()
.success()
.stdout(predicate::str::is_empty())
.stderr(predicate::str::is_empty());

assert!(
!env_file.exists(),
"silent mode must not write env file when version is missing"
);
}