Skip to content

Commit

Permalink
feat: rocks pack (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Jan 31, 2025
1 parent a845efb commit e7523ed
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 56 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
uses: rhysd/action-setup-vim@v1 # Used by the 'run' integration test
with:
neovim: true
version: v0.10.3
- name: Integration tests
run: |
nix develop .#${{ matrix.lua-version }} --command cargo nextest run --test "*"
Expand Down
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions rocks-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spinners = "4.1.1"
stylua = { version = "2.0.0", features = ["fromstr", "lua52"] }
strum = "0.26.3"
strum_macros = "0.26.4"
tempdir = "0.3.7"
termcolor = "1.4.1"
text_trees = "0.1.2"
tokio = { version = "1.43.0", features = ["full"] }
Expand Down
6 changes: 4 additions & 2 deletions rocks-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use install::Install;
use install_rockspec::InstallRockspec;
use list::ListCmd;
use outdated::Outdated;
use pack::Pack;
use path::Path;
use pin::ChangePin;
use remove::Remove;
Expand Down Expand Up @@ -40,6 +41,7 @@ pub mod install_lua;
pub mod install_rockspec;
pub mod list;
pub mod outdated;
pub mod pack;
pub mod path;
pub mod pin;
pub mod project;
Expand Down Expand Up @@ -157,8 +159,8 @@ pub enum Commands {
New(NewProject),
/// List outdated rocks.
Outdated(Outdated),
/// [UNIMPLEMENTED] Create a rock, packing sources or binaries.
Pack,
/// Create a packed rock for distribution, packing sources or binaries.
Pack(Pack),
/// Return the currently configured package path.
Path(Path),
/// Pin an existing rock, preventing any updates to the package.
Expand Down
4 changes: 2 additions & 2 deletions rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rocks::{
build, check, config,
debug::Debug,
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,
pack, path, pin, project, purge, remove, run, run_lua, search, test, uninstall, unpack, update,
upload::{self},
which, Cli, Commands,
};
Expand Down Expand Up @@ -80,7 +80,7 @@ async fn main() {
Commands::Config(config_cmd) => config::config(config_cmd, config).unwrap(),
Commands::Doc(doc_args) => doc::doc(doc_args, config).await.unwrap(),
Commands::Lint => unimplemented!(),
Commands::Pack => unimplemented!(),
Commands::Pack(pack_args) => pack::pack(pack_args, config).await.unwrap(),
Commands::Uninstall(uninstall_data) => {
uninstall::uninstall(uninstall_data, config).await.unwrap()
}
Expand Down
128 changes: 128 additions & 0 deletions rocks-bin/src/pack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use std::{path::PathBuf, str::FromStr};

use clap::Args;
use eyre::{eyre, OptionExt, Result};
use rocks_lib::{
build::{Build, BuildBehaviour},
config::{Config, LuaVersion},
lua_rockspec::LuaRockspec,
operations::{self, Install},
package::PackageReq,
progress::MultiProgress,
project::{rocks_toml::RocksToml, Project},
tree::Tree,
};
use tempdir::TempDir;

#[derive(Debug, Clone)]
pub enum PackageOrRockspec {
Package(PackageReq),
RockSpec(PathBuf),
}

impl FromStr for PackageOrRockspec {
type Err = eyre::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = PathBuf::from(s);
if path.is_file() {
Ok(Self::RockSpec(path))
} else {
let pkg = PackageReq::from_str(s).map_err(|err| {
eyre!(
"No file {0} found and cannot parse package query: {1}",
s,
err
)
})?;
Ok(Self::Package(pkg))
}
}
}

#[derive(Args)]
pub struct Pack {
/// Path to a RockSpec or a package query for a package to pack.
/// Prioritises installed rocks and will install a rock to a temporary
/// directory if none is found.
/// In case of multiple matches, the latest version will be packed.
/// Examples: "pkg", "[email protected]", "pkg>=1.0.0"
///
/// If not set, rocks will pack the current project's rocks.toml.
#[clap(value_parser)]
package_or_rockspec: Option<PackageOrRockspec>,
}

pub async fn pack(args: Pack, config: Config) -> Result<()> {
let lua_version = LuaVersion::from(&config)?;
let dest_dir = std::env::current_dir()?;
let progress = MultiProgress::new_arc();
let package_or_rockspec = match args.package_or_rockspec {
Some(package_or_rockspec) => package_or_rockspec,
None => {
let project = Project::current()?.ok_or_eyre("Not in a project!")?;
PackageOrRockspec::RockSpec(project.rocks_path())
}
};
let result: Result<PathBuf> = match package_or_rockspec {
PackageOrRockspec::Package(package_req) => {
let default_tree = Tree::new(config.tree().clone(), lua_version.clone())?;
match default_tree.match_rocks(&package_req)? {
rocks_lib::tree::RockMatches::NotFound(_) => {
let temp_dir = TempDir::new("rocks-pack")?.into_path();
let tree = Tree::new(temp_dir.clone(), lua_version.clone())?;
let packages = Install::new(&config)
.package(BuildBehaviour::Force, package_req)
.progress(progress)
.install()
.await?;
let package = packages.first().unwrap();
let rock_path =
operations::Pack::new(dest_dir, tree, package.clone()).pack()?;
Ok(rock_path)
}
rocks_lib::tree::RockMatches::Single(local_package_id) => {
let lockfile = default_tree.lockfile()?;
let package = lockfile.get(&local_package_id).unwrap();
let rock_path =
operations::Pack::new(dest_dir, default_tree, package.clone()).pack()?;
Ok(rock_path)
}
rocks_lib::tree::RockMatches::Many(vec) => {
let local_package_id = vec.first().unwrap();
let lockfile = default_tree.lockfile()?;
let package = lockfile.get(local_package_id).unwrap();
let rock_path =
operations::Pack::new(dest_dir, default_tree, package.clone()).pack()?;
Ok(rock_path)
}
}
}
PackageOrRockspec::RockSpec(rockspec_path) => {
let content = std::fs::read_to_string(&rockspec_path)?;
let rockspec = match rockspec_path
.extension()
.map(|ext| ext.to_string_lossy().to_string())
.unwrap_or("".into())
.as_str()
{
".rockspec" => Ok(LuaRockspec::new(&content)?),
".toml" => Ok(RocksToml::new(&content)?
.into_validated_rocks_toml()?
.to_rockspec()?),
_ => Err(eyre!(
"expected a path to a .rockspec or rocks.toml or a package requirement."
)),
}?;
let temp_dir = TempDir::new("rocks-pack")?.into_path();
let bar = progress.map(|p| p.new_bar());
let tree = Tree::new(temp_dir.clone(), lua_version.clone())?;
let config = config.with_tree(temp_dir);
let package = Build::new(&rockspec, &config, &bar).build().await?;
let rock_path = operations::Pack::new(dest_dir, tree, package).pack()?;
Ok(rock_path)
}
};
print!("packed rock created at {}", result?.display());
Ok(())
}
1 change: 1 addition & 0 deletions rocks-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bon = { version = "3.3.2", features = ["implied-bounds"] }
clean-path = "0.2.1"
diffy = "0.4.0"
toml = "0.8.19"
md5 = "0.7.0"

[dev-dependencies]
httptest = { version = "0.16.1" }
Expand Down
Loading

0 comments on commit e7523ed

Please sign in to comment.