lux_lib/operations/
pin.rs1use crate::package;
2use std::io;
3
4use crate::{
5 fs,
6 lockfile::{FlushLockfileError, LockedPackageId, PinnedState},
7 tree::{self, InstallTree, Tree},
8};
9use fs_extra::dir::CopyOptions;
10use itertools::Itertools;
11use miette::Diagnostic;
12use thiserror::Error;
13
14#[derive(Error, Debug, Diagnostic)]
17pub enum Error {
18 #[error("package with ID '{0}' not found in the lockfile")]
19 #[diagnostic(help("this is probably a bug"))]
20 PackageNotFound(LockedPackageId),
21 #[error("rock {rock} is already {}pinned!", if *.pin_state == PinnedState::Unpinned { "un" } else { "" })]
22 PinStateUnchanged {
23 pin_state: PinnedState,
24 rock: package::Spec,
25 },
26 #[error("cannot change the pin state of '{rock}', since a second version of '{rock}' is already installed with 'pin: {}'", .pin_state.as_bool())]
27 PinStateConflict {
28 pin_state: PinnedState,
29 rock: package::Spec,
30 },
31 #[error(transparent)]
32 #[diagnostic(transparent)]
33 FlushLockfile(#[from] FlushLockfileError),
34 #[error(transparent)]
35 #[diagnostic(transparent)]
36 Tree(#[from] tree::Error),
37 #[error("failed to move the old package")]
38 #[diagnostic(help("make sure Lux has write access to the install directory"))]
39 MoveItemsFailure(#[from] fs_extra::error::Error),
40 #[error("cannot change pin state of {rock}, because it is not an entrypoint")]
41 #[diagnostic(help(
42 "Lux does not allow pinning dependencies, as doing so could break the version requirement in a future update."
43 ))]
44 NotAnEntrypoint { rock: package::Spec },
45 #[error(transparent)]
46 #[diagnostic(transparent)]
47 Fs(#[from] fs::Error),
48}
49
50pub fn set_state(package_id: &LockedPackageId, tree: &Tree, pin: PinnedState) -> Result<(), Error> {
51 let lockfile = tree.lockfile()?;
52 let package = lockfile
53 .get(package_id)
54 .ok_or_else(|| Error::PackageNotFound(package_id.clone()))?
55 .clone();
56
57 if !lockfile.is_entrypoint(&package.id()) {
58 return Err(Error::NotAnEntrypoint {
59 rock: package.to_package(),
60 });
61 }
62
63 if pin == package.pinned() {
64 return Err(Error::PinStateUnchanged {
65 pin_state: package.pinned(),
66 rock: package.to_package(),
67 });
68 }
69
70 let old_package = package.clone();
71 let layout = tree.layout_for(&package.spec);
72 let items = fs::sync::read_dir(&layout.root)?
73 .filter_map(Result::ok)
74 .map(|dir| dir.path())
75 .collect_vec();
76
77 let package = package.repin(pin);
78
79 if lockfile.get(&package.id()).is_some() {
80 return Err(Error::PinStateConflict {
81 pin_state: package.pinned(),
82 rock: package.to_package(),
83 });
84 }
85
86 fs::sync::create_dir_all(&layout.root)?;
87
88 fs_extra::move_items(&items, layout.root, &CopyOptions::new())?;
89
90 lockfile.map_then_flush(|lockfile| {
91 let binaries = lockfile.binaries(&old_package.id()).cloned();
92 lockfile.remove(&old_package);
93 lockfile.add_entrypoint(&package);
94 if let Some(binaries) = binaries {
95 lockfile.set_binaries(&package, binaries);
96 }
97
98 Ok::<_, io::Error>(())
99 })?;
100
101 Ok(())
102}