Skip to content

Commit

Permalink
feat: rocks which command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 16, 2025
1 parent c213872 commit fd5b5a5
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 71 deletions.
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

0 comments on commit fd5b5a5

Please sign in to comment.