Skip to main content

lux_cli/
pin.rs

1use clap::Args;
2use itertools::Itertools;
3use lux_lib::config::Config;
4use lux_lib::drivers::sync::{Sync, SyncMode, TargetSet};
5use lux_lib::lockfile::PinnedState;
6use lux_lib::lua;
7use lux_lib::operations::pin;
8use lux_lib::package;
9use lux_lib::rockspec::lua_dependency;
10use lux_lib::tree::RockMatches;
11use lux_lib::workspace::Workspace;
12
13use miette::miette;
14use miette::Context;
15use miette::Result;
16
17#[derive(Args)]
18pub struct ChangePin {
19    /// Installed package or dependency to pin.
20    /// If pinning a dependency in a project, this should
21    /// be the package name.
22    package_req: Vec<package::Req>,
23
24    /// Pin a development dependency.
25    /// Also called `dev`.
26    #[arg(short, long, alias = "dev", visible_short_aliases = ['d', 'b'])]
27    build: Option<Vec<package::Name>>,
28
29    /// Pin a test dependency.
30    #[arg(short, long)]
31    test: Option<Vec<package::Name>>,
32
33    /// Project to modify.
34    #[arg(short, long, visible_short_alias = 'p')]
35    pub(crate) package: Option<package::Name>,
36}
37
38pub async fn set_pinned_state(data: ChangePin, config: Config, pin: PinnedState) -> Result<()> {
39    match Workspace::current()? {
40        Some(mut workspace) => {
41            let project = workspace.single_member_or_select_mut(&data.package)?;
42
43            if data
44                .package_req
45                .iter()
46                .any(|pkg| !pkg.version_req().is_any())
47            {
48                return Err(miette!(
49                    help = "if pinning a dependency in a project, specify the package name",
50                    "cannot pin project dependencies using version constraints."
51                ));
52            }
53            let packages = data
54                .package_req
55                .iter()
56                .map(|pkg| pkg.name())
57                .cloned()
58                .collect_vec();
59            if !packages.is_empty() {
60                project
61                    .set_pinned_state(
62                        lua_dependency::Kind::Regular(packages.iter().collect()),
63                        pin,
64                    )
65                    .await?;
66            }
67            let build_packages = data.build.unwrap_or_default();
68            if !build_packages.is_empty() {
69                project
70                    .set_pinned_state(
71                        lua_dependency::Kind::Build(build_packages.iter().collect()),
72                        pin,
73                    )
74                    .await?;
75            }
76            let test_packages = data.test.unwrap_or_default();
77            if !test_packages.is_empty() {
78                project
79                    .set_pinned_state(
80                        lua_dependency::Kind::Test(test_packages.iter().collect()),
81                        pin,
82                    )
83                    .await?;
84            }
85            Sync::new(&workspace, &config)
86                .mode(SyncMode::Frozen)
87                .targets(TargetSet::all(&workspace, !test_packages.is_empty()))
88                .sync()
89                .await
90                .wrap_err("syncing dependencies with the workspace lockfile failed.")?;
91        }
92        None => {
93            let tree = config.user_tree(lua::Version::from(&config)?.clone())?;
94
95            for package in &data.package_req {
96                match tree.match_rocks_and(package, |package| pin != package.pinned())? {
97                    RockMatches::Single(rock) => {
98                        pin::set_state(&rock, &tree, pin)?;
99                    }
100                    RockMatches::Many(_) => {
101                        return Err(miette!(
102                            help = "narrow down the version constraint",
103                            "multiple packages found that match '{}'",
104                            package
105                        ));
106                    }
107                    RockMatches::NotFound(_) => return Err(miette!("rock {} not found!", package)),
108                }
109            }
110        }
111    }
112    Ok(())
113}