Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/commands/default_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::fs;
use crate::versions::LocalVersionStore;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
Expand All @@ -23,7 +24,7 @@ impl DefaultCmd {
);
return Ok(());
}
Some(ref v) => fs::resolve_local_version(v)?,
Some(ref v) => LocalVersionStore::from_env()?.resolve_cli(v)?,
None => {
if !std::io::stdin().is_terminal() {
match fs::get_default_version()? {
Expand All @@ -33,7 +34,7 @@ impl DefaultCmd {
return Ok(());
}

let items = fs::get_aliased_versions()?;
let items = LocalVersionStore::from_env()?.aliased_cli_items()?;
if items.is_empty() {
eprintln!("{} No PHP versions are currently installed.", "💡".yellow());
return Ok(());
Expand Down
5 changes: 3 additions & 2 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ impl Env {
// shell starts on it instead of the system PHP. Must precede wrapper_fn:
// its PATH is a snapshot taken before the wrapper adds $PVM_DIR/bin.
if let Some(version) = crate::fs::get_default_version()? {
if crate::fs::is_version_installed(&version)? {
let bin_dir = crate::fs::get_version_bin_dir(&version)?;
let store = crate::versions::LocalVersionStore::from_env()?;
if store.has_cli(&version) {
let bin_dir = store.bin_dir(&version);
println!(
"{}",
s.set_env_var(MULTISHELL_PATH_VAR, &bin_dir.to_string_lossy())
Expand Down
7 changes: 4 additions & 3 deletions src/commands/exec_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fs;
use crate::versions::LocalVersionStore;
use anyhow::{Context, Result};
use clap::Parser;

Expand All @@ -15,8 +15,9 @@ pub struct Exec {

impl Exec {
pub async fn call(self) -> Result<()> {
let version = fs::resolve_local_version(&self.version)?;
let bin_dir = fs::get_version_bin_dir(&version)?;
let store = LocalVersionStore::from_env()?;
let version = store.resolve_cli(&self.version)?;
let bin_dir = store.bin_dir(&version);

// Prepend the version's bin dir so its binaries shadow any other PHP
// already on PATH, for this single child process only.
Expand Down
24 changes: 13 additions & 11 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::PHP_VERSION_FILE;
use crate::{fs, network, prompt};
use crate::versions::{self, LocalVersionStore, MinorVersion};
use crate::{network, prompt};
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
Expand Down Expand Up @@ -38,14 +39,14 @@ impl Init {

// Distinct major.minor lines: locally installed first (newest first,
// marked), then the remote-only ones (newest first).
let mut seen = HashSet::new();
let mut seen: HashSet<MinorVersion> = HashSet::new();
let mut options = Vec::new(); // what gets written to .php-version
let mut displays = Vec::new(); // what the picker shows

for v in fs::list_installed_versions()?.iter().rev() {
if let Some(mm) = fs::minor_of(v)
&& seen.insert(mm.clone())
{
let store = LocalVersionStore::from_env()?;
for line in store.minor_lines()?.into_iter().rev() {
if seen.insert(line.minor) {
let mm = line.minor.to_string();
displays.push(format!("{} {}", mm, "(installed)".green()));
options.push(mm);
}
Expand All @@ -54,11 +55,12 @@ impl Init {
println!("{} Fetching remotely available PHP versions...", "↻".blue());
match network::get_available_versions().await {
Ok(remote) => {
for (v, _) in remote.iter().rev() {
// Start from newest
if let Some(mm) = fs::minor_of(v)
&& seen.insert(mm.clone())
{
for line in versions::group_by_minor(remote.iter().map(|(v, _)| v.as_str()))
.into_iter()
.rev()
{
if seen.insert(line.minor) {
let mm = line.minor.to_string();
displays.push(mm.clone());
options.push(mm);
}
Expand Down
46 changes: 22 additions & 24 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::constants::MULTISHELL_PATH_VAR;
use crate::versions::{self, LocalVersionStore};
use crate::{fs, network};
use anyhow::Result;
use clap::Parser;
Expand Down Expand Up @@ -31,8 +32,9 @@ pub async fn execute_install_with(
packages: &[String],
assume_yes: bool,
) -> Result<Option<String>> {
let versions_dir = fs::get_versions_dir()?;
std::fs::create_dir_all(&versions_dir)?;
let store = LocalVersionStore::from_env()?;
let versions_dir = store.versions_dir();
std::fs::create_dir_all(versions_dir)?;

println!(
"{} Resolving latest patch for PHP {}...",
Expand All @@ -52,7 +54,7 @@ pub async fn execute_install_with(
anyhow::bail!("No packages found for PHP {}", resolved_version);
}

let already_installed = fs::get_installed_packages(&resolved_version);
let already_installed = store.installed_packages(&resolved_version);
if !already_installed.is_empty() {
println!(
"{} PHP {} is already installed [{}]",
Expand Down Expand Up @@ -143,14 +145,14 @@ pub async fn execute_install_with(
)?;

if use_now {
let v = crate::fs::resolve_local_version(&resolved_version)?;
let bin_dir = crate::fs::get_version_bin_dir(&v)?;
let v = store.resolve_cli(&resolved_version)?;
let bin_dir = store.bin_dir(&v);
let s = crate::shell::detect_shell();
let export_str1 = s.set_env_var(MULTISHELL_PATH_VAR, &bin_dir.to_string_lossy());
let export_str2 = s.path(&bin_dir, &crate::fs::path_without_versions()?);
let export_str2 = s.path(&bin_dir, &fs::path_without_versions()?);

let env_file = crate::fs::get_env_update_path()?;
crate::fs::write_env_file_locked(&env_file, &format!("{}\n{}", export_str1, export_str2))?;
let env_file = fs::get_env_update_path()?;
fs::write_env_file_locked(&env_file, &format!("{}\n{}", export_str1, export_str2))?;

// Note: process-global env is intentionally NOT mutated here. std::env::set_var
// is unsound in a multi-threaded tokio runtime, and the wrapper sources env_file
Expand Down Expand Up @@ -257,36 +259,32 @@ async fn pick_and_install(packages: &[String], assume_yes: bool) -> Result<()> {
return Ok(());
}

let installed = fs::list_installed_versions().unwrap_or_default();
let installed = LocalVersionStore::from_env()
.and_then(|s| s.list_all())
.unwrap_or_default();

let mut display_items = Vec::new();
let mut target_versions = Vec::new(); // Parallel array tying display index to actual version string

// Build "Quick Select" aliases
let mut minors = std::collections::BTreeMap::new();
let mut highest_overall = None;

for (v, _) in &versions_info {
highest_overall = Some(v.clone());
if let Some(minor) = fs::minor_of(v) {
minors.insert(minor, v.clone()); // BTreeMap iterates ascending, keeps latest
}
}

if let Some(highest) = highest_overall {
if let Some((highest, _)) = versions_info.last() {
display_items.push(format!("latest ({})", highest).bold().to_string());
target_versions.push(highest);
target_versions.push(highest.clone());
}

// Add them in reverse order (newest minor first) for the quick select
for (minor, highest_patch) in minors.iter().rev() {
for line in versions::group_by_minor(versions_info.iter().map(|(v, _)| v.as_str()))
.into_iter()
.rev()
{
let highest_patch = line.latest().to_string();
display_items.push(
format!("{} ({})", minor, highest_patch)
format!("{} ({})", line.minor, highest_patch)
.bold()
.cyan()
.to_string(),
);
target_versions.push(highest_patch.clone());
target_versions.push(highest_patch);
}

display_items.push("---".dimmed().to_string());
Expand Down
24 changes: 18 additions & 6 deletions src/commands/ls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::commands::use_cmd::{ActivateOpts, activate, pick_installed_version};
use crate::commands::use_cmd::{ActivateOpts, activate, pick_version_from_items};
use crate::fs;
use crate::versions::LocalVersionStore;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
Expand All @@ -11,15 +12,23 @@ pub struct Ls;

impl Ls {
pub async fn call(self) -> Result<()> {
let items = fs::get_aliased_versions()?;
let store = LocalVersionStore::from_env()?;
let items = store.aliased_items()?;
if items.is_empty() {
println!("No PHP versions installed.");
return Ok(());
}

// Scripts and pipes get the plain list; a terminal gets a picker that
// switches the shell to the selected version (Esc just leaves).
if !(std::io::stdin().is_terminal() && std::io::stdout().is_terminal()) {
// Scripts and pipes get the plain list (all packages); a terminal gets
// a picker over CLI-capable versions that switches the shell on Enter.
let is_tty = std::io::stdin().is_terminal() && std::io::stdout().is_terminal();
let cli_items = if is_tty {
store.aliased_cli_items()?
} else {
Vec::new()
};

if !is_tty || cli_items.is_empty() {
let current = fs::get_current_version();
for item in items {
let pkgs_str = item.packages.join(", ");
Expand All @@ -38,7 +47,10 @@ impl Ls {
}

let current = fs::get_current_version();
match pick_installed_version("Installed PHP versions — Enter switches, Esc exits")? {
match pick_version_from_items(
"Installed PHP versions — Enter switches, Esc exits",
&cli_items,
)? {
Some(version) if version == current => {
eprintln!("{} PHP {} is already active.", "✓".green(), version.bold());
Ok(())
Expand Down
10 changes: 6 additions & 4 deletions src/commands/ls_remote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{fs, network};
use crate::network;
use crate::versions::{self, LocalVersionStore};
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
Expand All @@ -15,16 +16,17 @@ impl LsRemote {
let mut versions_info = network::get_available_versions().await?;

if let Some(prefix) = &self.version_prefix {
let prefix_dot = format!("{}.", prefix);
versions_info.retain(|(v, _)| v == prefix || v.starts_with(&prefix_dot));
versions_info.retain(|(v, _)| versions::matches_prefix(v, prefix));
}

if versions_info.is_empty() {
println!("{} No remote versions found.", "💡".yellow());
return Ok(());
}

let installed = fs::list_installed_versions().unwrap_or_default();
let installed = LocalVersionStore::from_env()
.and_then(|s| s.list_all())
.unwrap_or_default();

// Ascending order: the newest versions print last, right above the prompt.
for (v, pkgs) in &versions_info {
Expand Down
31 changes: 13 additions & 18 deletions src/commands/prune.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::fs;
use crate::versions::LocalVersionStore;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use std::collections::BTreeMap;
use std::io::IsTerminal;

/// Remove superseded patch versions
Expand All @@ -15,26 +15,21 @@ pub struct Prune {

impl Prune {
pub async fn call(self) -> Result<()> {
let installed = fs::list_installed_versions()?;
let store = LocalVersionStore::from_env()?;
let current = fs::get_current_version();

// Keeper per major.minor line: the newest patch. The installed list
// is semver-sorted ascending, so the last insert per minor wins.
let mut keepers: BTreeMap<String, String> = BTreeMap::new();
for version in &installed {
if let Some(minor) = fs::minor_of(version) {
keepers.insert(minor, version.clone());
}
}

// Candidates: every installed patch that is not its minor's keeper,
// except the currently active version.
let candidates: Vec<(String, String)> = installed
.iter()
.filter_map(|version| {
let keeper = keepers.get(&fs::minor_of(version)?)?;
(version != keeper && *version != current)
.then(|| (version.clone(), keeper.clone()))
let candidates: Vec<(String, String)> = store
.minor_lines()?
.into_iter()
.flat_map(|line| {
let keeper = line.latest().to_string();
line.superseded()
.iter()
.filter(|version| **version != current)
.map(|version| (version.clone(), keeper.clone()))
.collect::<Vec<_>>()
})
.collect();

Expand Down Expand Up @@ -63,7 +58,7 @@ impl Prune {
return Ok(());
}

let versions_dir = fs::get_versions_dir()?;
let versions_dir = store.versions_dir();
for (version, _) in &candidates {
let dest = versions_dir.join(version);
std::fs::remove_dir_all(&dest)
Expand Down
8 changes: 5 additions & 3 deletions src/commands/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::fs;
use crate::versions::LocalVersionStore;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
Expand All @@ -17,10 +18,11 @@ pub struct Uninstall {

impl Uninstall {
pub async fn call(self) -> Result<()> {
let store = LocalVersionStore::from_env()?;
let version = match self.version {
Some(ref v) => fs::resolve_local_version(v)?,
Some(ref v) => store.resolve_any(v)?,
None => {
let items = fs::get_aliased_versions()?;
let items = store.aliased_items()?;
if items.is_empty() {
println!("{} No PHP versions are currently installed.", "💡".yellow());
return Ok(());
Expand All @@ -43,7 +45,7 @@ impl Uninstall {
}
};

let dest = fs::get_versions_dir()?.join(&version);
let dest = store.versions_dir().join(&version);
if !dest.exists() {
anyhow::bail!("PHP {} is not installed locally.", version);
}
Expand Down
Loading