Skip to main content

lux_cli/
outdated.rs

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