Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rocks pin #154

Open
wants to merge 1 commit into
base: push-szzsqpnszvsk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use info::Info;
use install::Install;
use list::ListCmd;
use outdated::Outdated;
use pin::Pin;
use remove::Remove;
use rocks_lib::config::{ConfigBuilder, LuaVersion};
use search::Search;
Expand All @@ -24,6 +25,7 @@ mod install;
mod install_lua;
mod list;
mod outdated;
mod pin;
mod project;
mod purge;
mod remove;
Expand Down Expand Up @@ -129,6 +131,8 @@ enum Commands {
Pack,
/// Return the currently configured package path.
Path,
/// Pin an existing rock, preventing any updates to the package.
Pin(Pin),
/// Remove all installed rocks from a tree.
Purge,
/// Uninstall a rock.
Expand Down Expand Up @@ -197,6 +201,7 @@ async fn main() {
Commands::Remove(remove_args) => remove::remove(remove_args, config).await.unwrap(),
Commands::Update(_update_args) => update::update(config).await.unwrap(),
Commands::Info(info_data) => info::info(info_data, config).await.unwrap(),
Commands::Pin(pin_data) => pin::pin(pin_data, config).unwrap(),
_ => unimplemented!(),
}
}
24 changes: 24 additions & 0 deletions rocks-bin/src/pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap::Args;
use eyre::eyre;
use eyre::Result;
use rocks_lib::operations;
use rocks_lib::{
config::{Config, LuaVersion},
package::PackageReq,
tree::Tree,
};

#[derive(Args)]
pub struct Pin {
package: PackageReq,
}

pub fn pin(data: Pin, config: Config) -> Result<()> {
let tree = Tree::new(config.tree().clone(), LuaVersion::from(&config)?)?;

if let Some(mut rock) = tree.has_rock(&data.package) {
operations::pin(&mut rock, &tree)
} else {
Err(eyre!("Rock {} not found!", data.package))
}
}
10 changes: 2 additions & 8 deletions rocks-bin/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Args;
use eyre::OptionExt;
use eyre::Result;
use indicatif::MultiProgress;
use rocks_lib::config::LuaVersion;
use rocks_lib::{
config::Config,
manifest::{manifest_from_server, ManifestMetadata},
Expand All @@ -14,13 +14,7 @@ use rocks_lib::{
pub struct Update {}

pub async fn update(config: Config) -> Result<()> {
let tree = Tree::new(
config.tree().clone(),
config
.lua_version()
.cloned()
.ok_or_eyre("lua version not supplied!")?,
)?;
let tree = Tree::new(config.tree().clone(), LuaVersion::from(&config)?)?;

let lockfile = tree.lockfile()?;
let rocks = lockfile.rocks();
Expand Down
1 change: 1 addition & 0 deletions rocks-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ infer = "0.16.0"
indicatif = "0.17.8"
sha2 = "0.10.8"
hex = { version = "0.4.3" }
fs_extra = "1.3.0"

[dev-dependencies]
httptest = { version = "0.16.1" }
Expand Down
2 changes: 2 additions & 0 deletions rocks-lib/src/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod download;
mod fetch;
mod install;
mod pin;
mod remove;
mod unpack;
mod update;

pub use download::*;
pub use fetch::*;
pub use install::*;
pub use pin::*;
pub use remove::*;
pub use unpack::*;
pub use update::*;
32 changes: 32 additions & 0 deletions rocks-lib/src/operations/pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use eyre::{bail, Result};
use fs_extra::dir::CopyOptions;
use itertools::Itertools;

use crate::{lockfile::LocalPackage, tree::Tree};

pub fn pin(package: &mut LocalPackage, tree: &Tree) -> Result<()> {
if package.pinned() {
bail!("Rock {} is already pinned!", package.to_package());
}

let mut lockfile = tree.lockfile()?;
let old_package = package.clone();
let items = std::fs::read_dir(tree.root_for(package))?
.filter_map(Result::ok)
.map(|dir| dir.path())
.collect_vec();

package.pinned = true;

let new_root = tree.root_for(package);

std::fs::create_dir_all(&new_root)?;

fs_extra::move_items(&items, new_root, &CopyOptions::new())?;

lockfile.remove(&old_package);
lockfile.add(package);
lockfile.flush()?;

Ok(())
}
Loading