Skip to main content

lux_cli/
outdated.rs

1use std::collections::HashMap;
2
3use clap::Args;
4use itertools::Itertools;
5use lux_lib::{config::Config, lua, package::db::PackageDB, workspace::Workspace};
6
7use miette::{IntoDiagnostic, Result};
8use text_trees::{FormatCharacters, StringTreeNode, TreeFormatting};
9
10use crate::args::OutputFormat;
11
12#[derive(Args)]
13pub struct Outdated {
14    #[arg(long, default_value = "text", value_enum, ignore_case = true)]
15    output_format: OutputFormat,
16}
17
18/// List rocks that are outdated
19/// If in a project, this lists rocks in the project tree
20pub async fn outdated(outdated_data: Outdated, config: Config) -> Result<()> {
21    let workspace = Workspace::current()?;
22    // NOTE: We deliberately do not sync the workspace before listing outdated
23    // rocks. `Sync` may install or remove packages as a side effect, which would
24    // make this otherwise read-only command mutate the install tree.
25    let tree = match &workspace {
26        Some(project) => project.tree(&config)?,
27        None => {
28            let lua_version = lua::Version::from(&config)?.clone();
29            config.user_tree(lua_version)?
30        }
31    };
32
33    let package_db = PackageDB::from_config(&config).await?;
34
35    // NOTE: This will display all installed versions and each possible upgrade.
36    // However, this should also take into account dependency constraints made by other rocks.
37    // This will naturally occur with lockfiles and should be accounted for directly in the
38    // `has_update` function.
39    let rock_list = tree.as_rock_list()?;
40    let rock_list = {
41        let mut rocks = Vec::new();
42        for rock in rock_list.iter() {
43            if let Ok(Some(version)) = rock.to_package().has_update(&package_db).await {
44                rocks.push((rock, version));
45            }
46        }
47        rocks
48    };
49
50    let rock_list = rock_list
51        .iter()
52        .sorted_by_key(|(rock, _)| rock.name().to_owned())
53        .into_group_map_by(|(rock, _)| rock.name().to_owned());
54
55    let has_outdated = !rock_list.is_empty();
56
57    match outdated_data.output_format {
58        OutputFormat::Json => {
59            let jsonified_rock_list = rock_list
60                .iter()
61                .map(|(key, values)| {
62                    (
63                        key,
64                        values
65                            .iter()
66                            .map(|(k, v)| (k.version().to_string(), v.to_string()))
67                            .collect::<HashMap<_, _>>(),
68                    )
69                })
70                .collect::<HashMap<_, _>>();
71
72            println!(
73                "{}",
74                serde_json::to_string(&jsonified_rock_list).into_diagnostic()?
75            );
76        }
77        OutputFormat::Text => {
78            let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());
79
80            for (rock_name, updates) in rock_list {
81                let mut tree = StringTreeNode::new(rock_name.to_string());
82
83                for (rock, latest_version) in updates {
84                    tree.push(format!("{} => {}", rock.version(), latest_version));
85                }
86
87                println!(
88                    "{}",
89                    tree.to_string_with_format(&formatting).into_diagnostic()?
90                );
91            }
92        }
93    }
94
95    if has_outdated {
96        println!("\nRun `lx update` to update all outdated rocks.");
97    }
98
99    Ok(())
100}