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 which command #341

Open
wants to merge 1 commit into
base: master
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
6 changes: 4 additions & 2 deletions rocks-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use uninstall::Uninstall;
use update::Update;
use upload::Upload;
use url::Url;
use which::Which;

pub mod build;
pub mod check;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub mod unpack;
pub mod update;
pub mod upload;
pub mod utils;
pub mod which;

/// A fast and efficient Lua package manager.
#[derive(Parser)]
Expand Down Expand Up @@ -180,6 +182,6 @@ pub enum Commands {
Update(Update),
/// Upload a rockspec to the public rocks repository.
Upload(Upload),
/// [UNIMPLEMENTED] Tell which file corresponds to a given module name.
Which,
/// Tell which file corresponds to a given module name.
Which(Which),
}
4 changes: 2 additions & 2 deletions rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rocks::{
doc, download, fetch, format, info, install, install_lua, install_rockspec, list, outdated,
path, pin, project, purge, remove, run, run_lua, search, test, uninstall, unpack, update,
upload::{self},
Cli, Commands,
which, Cli, Commands,
};
use rocks_lib::{
config::ConfigBuilder,
Expand Down Expand Up @@ -83,6 +83,6 @@ async fn main() {
Commands::Uninstall(uninstall_data) => {
uninstall::uninstall(uninstall_data, config).await.unwrap()
}
Commands::Which => unimplemented!(),
Commands::Which(which_args) => which::which(which_args, config).unwrap(),
}
}
19 changes: 19 additions & 0 deletions rocks-bin/src/which.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clap::Args;
use eyre::Result;
use rocks_lib::{config::Config, package::PackageReq, rockspec::LuaModule, which};

#[derive(Args)]
pub struct Which {
/// The module to search for.
module: LuaModule,
/// Only search in these packages.
packages: Option<Vec<PackageReq>>,
}

pub fn which(args: Which, config: Config) -> Result<()> {
let path = which::Which::new(args.module, &config)
.packages(args.packages.unwrap_or_default())
.search()?;
print!("{}", path.display());
Ok(())
}
14 changes: 7 additions & 7 deletions rocks-lib/resources/test/sample-tree/5.1/lock.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version": "1.0.0",
"rocks": {
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5": {
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4": {
"name": "neorg",
"version": "8.0.0-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"constraint": null,
"binaries": [],
Expand All @@ -16,7 +16,7 @@
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0": {
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769": {
"name": "lua-cjson",
"version": "2.1.0-1",
"pinned": false,
Expand All @@ -29,12 +29,12 @@
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0": {
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d": {
"name": "neorg",
"version": "8.8.1-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"binaries": [],
"constraint": null,
Expand All @@ -46,7 +46,7 @@
}
},
"entrypoints": [
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5",
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0"
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4",
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d"
]
}
1 change: 1 addition & 0 deletions rocks-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod remote_package_db;
pub mod rockspec;
pub mod tree;
pub mod upload;
pub mod which;

pub(crate) mod remote_package_source;

Expand Down
13 changes: 13 additions & 0 deletions rocks-lib/src/lockfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,19 @@ impl<P: LockfilePermissions> Lockfile<P> {
.cloned()
}

/// Find all rocks that match the requirement
pub(crate) fn find_rocks(&self, req: &PackageReq) -> Vec<LocalPackageId> {
match self.list().get(req.name()) {
Some(packages) => packages
.iter()
.rev()
.filter(|package| req.version_req().matches(package.version()))
.map(|package| package.id())
.collect_vec(),
None => Vec::default(),
}
}

/// Validate the integrity of an installed package with the entry in this lockfile.
pub(crate) fn validate_integrity(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
---
source: rocks-lib/src/lockfile/mod.rs
assertion_line: 865
expression: lockfile
---
{
"version": "1.0.0",
"rocks": {
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0": {
"name": "lua-cjson",
"version": "2.1.0-1",
"pinned": false,
"dependencies": [],
"constraint": null,
"binaries": [],
"source": "luarocks_rockspec+https://luarocks.org/",
"hashes": {
"rockspec": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"49667dff980cc30d5c45496ad9b2af4cecf4259f3dab55a76f6aa7ab6cdeadb7": {
"name": "test1",
"version": "0.1.0-1",
Expand All @@ -33,25 +21,27 @@ expression: lockfile
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"a9f137d1dad1af603e33935a3f8722dfbb2aebeac03bec5ed0b6e9cc5828c7f3": {
"name": "test2",
"version": "0.1.0-1",
"pinned": true,
"dependencies": [],
"constraint": ">=1.0.0",
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4": {
"name": "neorg",
"version": "8.0.0-1",
"pinned": false,
"dependencies": [
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"constraint": null,
"binaries": [],
"source": "test+foo_bar",
"source": "luarocks_rockspec+https://luarocks.org/",
"hashes": {
"rockspec": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0": {
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d": {
"name": "neorg",
"version": "8.8.1-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"constraint": null,
"binaries": [],
Expand All @@ -61,13 +51,24 @@ expression: lockfile
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5": {
"name": "neorg",
"version": "8.0.0-1",
"a9f137d1dad1af603e33935a3f8722dfbb2aebeac03bec5ed0b6e9cc5828c7f3": {
"name": "test2",
"version": "0.1.0-1",
"pinned": true,
"dependencies": [],
"constraint": ">=1.0.0",
"binaries": [],
"source": "test+foo_bar",
"hashes": {
"rockspec": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769": {
"name": "lua-cjson",
"version": "2.1.0-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
],
"dependencies": [],
"constraint": null,
"binaries": [],
"source": "luarocks_rockspec+https://luarocks.org/",
Expand All @@ -78,7 +79,7 @@ expression: lockfile
}
},
"entrypoints": [
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0",
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5"
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4",
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d"
]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
---
source: rocks-lib/src/lockfile/mod.rs
assertion_line: 817
expression: lockfile
---
{
"version": "1.0.0",
"rocks": {
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0": {
"name": "lua-cjson",
"version": "2.1.0-1",
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4": {
"name": "neorg",
"version": "8.0.0-1",
"pinned": false,
"dependencies": [],
"dependencies": [
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"constraint": null,
"binaries": [],
"source": "luarocks_rockspec+https://luarocks.org/",
Expand All @@ -18,12 +21,12 @@ expression: lockfile
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0": {
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d": {
"name": "neorg",
"version": "8.8.1-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769"
],
"constraint": null,
"binaries": [],
Expand All @@ -33,13 +36,11 @@ expression: lockfile
"source": "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
}
},
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5": {
"name": "neorg",
"version": "8.0.0-1",
"e7c4c9fcd3cb6fe33c1c0a2ab8cdbbe8ee81d11334277d1f5631c80317770769": {
"name": "lua-cjson",
"version": "2.1.0-1",
"pinned": false,
"dependencies": [
"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"
],
"dependencies": [],
"constraint": null,
"binaries": [],
"source": "luarocks_rockspec+https://luarocks.org/",
Expand All @@ -50,7 +51,7 @@ expression: lockfile
}
},
"entrypoints": [
"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0",
"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5"
"51c01417a2b1c1269e2ce71e688feb25bb7ea36c090b78f2e07df991ea8f42e4",
"760d318b1b0581e098a6c97ca1504a7bb2c15f259909c89ec4c5ad7fa46a155d"
]
}
14 changes: 11 additions & 3 deletions rocks-lib/src/rockspec/build/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ pub struct LuaModule(String);

impl LuaModule {
pub fn to_lua_path(&self) -> PathBuf {
self.to_pathbuf(".lua")
self.to_file_path(".lua")
}

pub fn to_lua_init_path(&self) -> PathBuf {
self.to_path_buf().join("init.lua")
}

pub fn to_lib_path(&self) -> PathBuf {
self.to_pathbuf(&format!(".{}", lua_lib_extension()))
self.to_file_path(&format!(".{}", lua_lib_extension()))
}

fn to_path_buf(&self) -> PathBuf {
PathBuf::from(self.0.replace('.', std::path::MAIN_SEPARATOR_STR))
}

fn to_pathbuf(&self, extension: &str) -> PathBuf {
fn to_file_path(&self, extension: &str) -> PathBuf {
PathBuf::from(self.0.replace('.', std::path::MAIN_SEPARATOR_STR) + extension)
}

Expand Down
23 changes: 6 additions & 17 deletions rocks-lib/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,12 @@ impl Tree {
}

pub fn match_rocks(&self, req: &PackageReq) -> io::Result<RockMatches> {
match self.list()?.get(req.name()) {
Some(packages) => {
let mut found_packages = packages
.iter()
.rev()
.filter(|package| req.version_req().matches(package.version()))
.map(|package| package.id())
.collect_vec();

Ok(match found_packages.len() {
0 => RockMatches::NotFound(req.clone()),
1 => RockMatches::Single(found_packages.pop().unwrap()),
2.. => RockMatches::Many(found_packages),
})
}
None => Ok(RockMatches::NotFound(req.clone())),
}
let mut found_packages = self.lockfile()?.find_rocks(req);
Ok(match found_packages.len() {
0 => RockMatches::NotFound(req.clone()),
1 => RockMatches::Single(found_packages.pop().unwrap()),
2.. => RockMatches::Many(found_packages),
})
}

pub fn match_rocks_and<F>(&self, req: &PackageReq, filter: F) -> io::Result<RockMatches>
Expand Down
Loading
Loading