mise 2026.9.3

Dev tools, env vars, and tasks in one CLI
use eyre::Result;
use std::collections::BTreeMap;
use tabled::{Table, Tabled};

use crate::config::Config;
use crate::parallel;
use crate::plugins::PluginType;
use crate::plugins::core::CORE_PLUGINS;
use crate::registry::full_to_url;
use crate::toolset::install_state;
use crate::ui::table;

/// List installed external plugins
///
/// Use `--core` for built-in runtimes or `--core --user` for both groups. `--outdated`
/// queries Git remotes for plugin updates; it does not compare installed tool versions.
/// Use `mise plugins ls-remote` for registry plugin sources and `mise ls` for tools.
#[derive(Debug, usage_rs::Args)]
#[usage(
    visible_alias = "list",
    example(
        r###"mise plugins ls
mise plugins ls --urls
mise plugins ls --core --user
mise plugins ls --outdated"###
    ),
    verbatim_doc_comment
)]
pub(super) struct PluginsLs {
    /// List all available remote plugins
    /// Same as `mise plugins ls-remote`
    #[usage(short, long, hide = true, verbatim_doc_comment)]
    pub all: bool,

    /// Only show built-in (core) plugins
    /// These are hidden by default
    #[usage(short, long, verbatim_doc_comment, conflicts = "all")]
    pub core: bool,

    /// Show plugins with available updates
    /// Checks the remote for newer versions and only displays plugins that are outdated
    #[usage(short, long, verbatim_doc_comment)]
    pub outdated: bool,

    /// Show the git url for each plugin
    /// e.g.: https://github.com/mise-plugins/vfox-cmake.git
    #[usage(short, long, alias = "url", verbatim_doc_comment)]
    pub urls: bool,

    /// Show the git refs for each plugin
    /// e.g.: main 1234abc
    #[usage(long, hide = true, verbatim_doc_comment)]
    pub refs: bool,

    /// List installed plugins
    ///
    /// This is the default behavior but can be used with --core
    /// to show core and user plugins
    #[usage(long, verbatim_doc_comment, conflicts = "all")]
    pub user: bool,
}

impl PluginsLs {
    pub(super) async fn run(self, config: &Config) -> Result<()> {
        let mut plugins: BTreeMap<_, _> = install_state::list_plugins()
            .iter()
            .map(|(k, p)| (k.clone(), (*p, None)))
            .collect();

        if self.core {
            for p in CORE_PLUGINS.keys() {
                miseprintln!("{p}");
                plugins.remove(p);
            }
            if !self.user {
                return Ok(());
            }
        }

        if self.all {
            for (name, backends) in &config.shorthands {
                for full in backends {
                    let plugin_type = PluginType::from_full(full)?;
                    plugins.insert(name.clone(), (plugin_type, Some(full_to_url(full))));
                }
            }
        }

        let plugins = plugins
            .into_iter()
            .map(|(short, (pt, url))| {
                let plugin = pt.plugin(short.clone());
                if let Some(url) = url {
                    plugin.set_remote_url(url);
                }
                (short, plugin)
            })
            .collect::<BTreeMap<_, _>>();

        if self.outdated {
            let installed: Vec<_> = plugins
                .into_iter()
                .filter(|(_, p)| p.is_installed())
                .collect();
            let results = parallel::parallel(installed, |(name, p)| async move {
                tokio::task::spawn_blocking(move || {
                    let local_sha = p.current_sha_short().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let remote_sha = p.remote_sha().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let remote_url = p.get_remote_url().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let abbrev_ref = p.current_abbrev_ref().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    (name, local_sha, remote_sha, remote_url, abbrev_ref)
                })
                .await
                .map_err(|e| eyre::eyre!(e))
            })
            .await?;
            let mut data: Vec<_> = results
                .into_iter()
                .filter_map(|(name, local_sha, remote_sha, remote_url, abbrev_ref)| {
                    let local_sha = local_sha?;
                    let remote_sha = remote_sha?;
                    if remote_sha.starts_with(&local_sha) {
                        return None;
                    }
                    Some(OutdatedRow {
                        plugin: name,
                        url: remote_url.unwrap_or_default(),
                        ref_: abbrev_ref.unwrap_or_default(),
                        local: local_sha,
                        remote: remote_sha.chars().take(7).collect(),
                    })
                })
                .collect();
            data.sort_by(|a, b| a.plugin.cmp(&b.plugin));
            if data.is_empty() {
                info!("All plugins are up to date");
            } else {
                let mut table = Table::new(data);
                table::print(&mut table, false)?;
            }
        } else if self.urls || self.refs {
            let data = plugins
                .into_iter()
                .map(|(name, p)| {
                    let remote_url = p.get_remote_url().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let abbrev_ref = p.current_abbrev_ref().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let sha_short = p.current_sha_short().unwrap_or_else(|e| {
                        warn!("{name}: {e:?}");
                        None
                    });
                    let mut row = Row {
                        plugin: name,
                        url: remote_url.unwrap_or_default(),
                        ref_: String::new(),
                        sha: String::new(),
                    };
                    if p.is_installed() {
                        row.ref_ = abbrev_ref.unwrap_or_default();
                        row.sha = sha_short.unwrap_or_default();
                    }
                    row
                })
                .collect::<Vec<_>>();
            let mut table = Table::new(data);
            table::print(&mut table, false)?;
        } else {
            hint!("registry", "see available plugins with", "mise registry");
            for tool in plugins.values() {
                miseprintln!("{tool}");
            }
        }
        Ok(())
    }
}

#[derive(Tabled)]
#[tabled(rename_all = "PascalCase")]
struct Row {
    plugin: String,
    url: String,
    ref_: String,
    sha: String,
}

#[derive(Tabled)]
#[tabled(rename_all = "PascalCase")]
struct OutdatedRow {
    plugin: String,
    url: String,
    ref_: String,
    local: String,
    remote: String,
}