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: add rocks list command #26

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 54 additions & 0 deletions Cargo.lock

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

45 changes: 45 additions & 0 deletions rocks-bin/src/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clap::Args;
use eyre::{OptionExt as _, Result};
use itertools::Itertools as _;
use rocks_lib::{config::Config, tree::Tree};
use text_trees::{FormatCharacters, StringTreeNode, TreeFormatting};

#[derive(Args)]
pub struct List {
vhyrro marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long)]
outdated: bool,
#[arg(long)]
porcelain: bool,
}

pub fn list(list_data: List, config: &Config) -> Result<()> {
vhyrro marked this conversation as resolved.
Show resolved Hide resolved
let tree = Tree::new(
&config.tree,
config
.lua_version
.as_ref()
.ok_or_eyre("lua version not supplied!")?,
)?;
let available_rocks = tree.list();

// TODO(vhyrro): Add `outdated` support.

if list_data.porcelain {
for (name, versions) in available_rocks {
println!("{}: {}", name, versions.join(" "));
vhyrro marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());
for (name, versions) in available_rocks.into_iter().sorted() {
let mut tree = StringTreeNode::new(name.to_owned());

for version in versions {
tree.push(version);
}

println!("{}", tree.to_string_with_format(&formatting)?);
}
}

Ok(())
}
4 changes: 3 additions & 1 deletion rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rocks_lib::config::{Config, LuaVersion};

mod build;
mod download;
mod list;
mod rockspec;
mod search;
mod unpack;
Expand Down Expand Up @@ -108,7 +109,7 @@ enum Commands {
/// Check syntax of a rockspec.
Lint,
/// List currently installed rocks.
List,
List(list::List),
/// Compile package in current directory using a rockspec.
Make,
/// Auto-write a rockspec for a new version of the rock.
Expand Down Expand Up @@ -176,6 +177,7 @@ async fn main() {
rockspec::write_rockspec(rockspec_data).await.unwrap()
}
Commands::Build(build_data) => build::build(build_data, &config).unwrap(),
Commands::List(list_data) => list::list(list_data, &config).unwrap(),
_ => unimplemented!(),
},
None => {
Expand Down
1 change: 1 addition & 0 deletions rocks-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tempdir = "0.3.7"
vfs = "0.12.0"
walkdir = "2.4.0"
zip = "0.6.6"
insta = { version = "1.39.0", features = ["redactions", "yaml"] }

[dev-dependencies]
httptest = { version = "0.15.5" }
Expand Down
26 changes: 26 additions & 0 deletions rocks-lib/src/tree/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::collections::HashMap;

use itertools::Itertools;
use walkdir::WalkDir;

use super::Tree;

impl<'a> Tree<'a> {
pub fn list(&self) -> HashMap<String, Vec<String>> {
WalkDir::new(self.root())
.min_depth(1)
.max_depth(1)
.into_iter()
.map(|rock_directory| {
let rock_dir = rock_directory.unwrap();
let (name, version) = rock_dir
.file_name()
.to_str()
.unwrap()
.split_once('@')
.unwrap();
(name.to_string(), version.to_string())
})
.into_group_map()
}
}
58 changes: 58 additions & 0 deletions rocks-lib/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::PathBuf;
use crate::config::LuaVersion;
use eyre::Result;

mod list;

/// A tree is a collection of files where installed rocks are located.
///
/// `rocks` diverges from the traditional hierarchy employed by luarocks.
Expand All @@ -22,6 +24,7 @@ pub struct Tree<'a> {
}

/// Change-agnostic way of referencing various paths for a rock.
#[derive(Debug, PartialEq)]
pub struct RockLayout {
pub etc: PathBuf,
pub lib: PathBuf,
Expand Down Expand Up @@ -63,3 +66,58 @@ impl<'a> Tree<'a> {
Ok(RockLayout { etc, lib, src })
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use insta::{assert_yaml_snapshot, sorted_redaction};

use crate::{config::LuaVersion, tree::RockLayout};

use super::Tree;

#[test]
fn rock_layout() {
let tree_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-tree");

let tree = Tree::new(&tree_path, &LuaVersion::Lua51).unwrap();

let neorg = tree
.rock(&"neorg".to_string(), &"8.0.0-1".to_string())
.unwrap();

assert_eq!(
neorg,
RockLayout {
etc: tree_path.join("5.1/[email protected]/etc"),
lib: tree_path.join("5.1/[email protected]/lib"),
src: tree_path.join("5.1/[email protected]/src"),
}
);

let lua_cjson = tree
.rock(&"lua-cjson".to_string(), &"2.1.0.9-1".to_string())
.unwrap();

assert_eq!(
lua_cjson,
RockLayout {
etc: tree_path.join("5.1/[email protected]/etc"),
lib: tree_path.join("5.1/[email protected]/lib"),
src: tree_path.join("5.1/[email protected]/src"),
}
);
}

#[test]
fn tree_list() {
let tree_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-tree");

let tree = Tree::new(&tree_path, &LuaVersion::Lua51).unwrap();

assert_yaml_snapshot!(tree.list(), { "." => sorted_redaction() })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: rocks-lib/src/tree/mod.rs
expression: tree.list()
---
lua-cjson:
- 2.1.0.9-1
neorg:
- 8.8.1-1
- 8.0.0-1
Loading