Skip to content

Commit

Permalink
feat: rocks add
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Oct 28, 2024
1 parent b49a493 commit 717a4d5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
13 changes: 13 additions & 0 deletions rocks-bin/src/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use clap::Args;
use eyre::{bail, OptionExt, Result};
use rocks_lib::{config::Config, project::Project};

#[derive(Args)]
pub struct Add {}

pub fn add(data: Add, config: Config) -> Result<()> {
let mut project = Project::current()?.ok_or_eyre("Unable to add dependency - current directory does not belong to a Lua project. Run `rocks new` to create one.")?;
project.rockspec_mut().dependencies.push("what the dog doing".to_string());

Ok(())
}
5 changes: 4 additions & 1 deletion rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::project::write_new::NewProject;
use std::{path::PathBuf, time::Duration};

use add::Add;
use build::Build;
use clap::{Parser, Subcommand};
use debug::Debug;
Expand All @@ -18,6 +19,7 @@ use rocks_lib::{
use search::Search;
use update::Update;

mod add;
mod build;
mod debug;
mod download;
Expand Down Expand Up @@ -100,7 +102,7 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Add a dependency to the current project.
Add,
Add(Add),
/// Build/compile a rock.
Build(Build),
/// Query information about Rocks's configuration.
Expand Down Expand Up @@ -208,6 +210,7 @@ async fn main() {
Commands::Info(info_data) => info::info(info_data, config).await.unwrap(),
Commands::Pin(pin_data) => pin::set_pinned_state(pin_data, config, Pinned).unwrap(),
Commands::Unpin(pin_data) => pin::set_pinned_state(pin_data, config, Unpinned).unwrap(),
Commands::Add(add_data) => add::add(add_data, config).unwrap(),
_ => unimplemented!(),
}
}
40 changes: 40 additions & 0 deletions rocks-lib/src/project/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use lets_find_up::{find_up_with, FindUpKind, FindUpOptions};
use mlua::{Lua, LuaSerdeExt};
use serde::{Deserialize, Serialize};
use std::{
io,
path::{Path, PathBuf},
Expand All @@ -24,6 +26,27 @@ pub struct Project {
root: PathBuf,
/// The parsed rockspec.
rockspec: Rockspec,
/// A partial rockspec representation for write operations
partial_rockspec: PartialRockspec,
}

#[derive(Debug, Serialize)]
pub struct PartialRockspec {
pub dependencies: Vec<String>,
}

impl PartialRockspec {
// TODO: Don't propagate different error types
pub fn new(rockspec_content: &str) -> mlua::Result<Self> {
let lua = Lua::new();
lua.load(dbg!(rockspec_content)).exec()?;

let globals = lua.globals();

Ok(PartialRockspec {
dependencies: globals.get("dependencies")?,
})
}
}

impl Project {
Expand All @@ -41,6 +64,7 @@ impl Project {
)? {
Some(path) => {
let rockspec_content = std::fs::read_to_string(&path)?;
let partial_rockspec = PartialRockspec::new(&rockspec_content).unwrap();
let rockspec = Rockspec::new(&rockspec_content)?;

let root = path.parent().unwrap();
Expand All @@ -50,11 +74,23 @@ impl Project {
Ok(Some(Project {
root: root.to_path_buf(),
rockspec,
partial_rockspec,
}))
}
None => Ok(None),
}
}

pub fn flush(&mut self) {
let lua = Lua::new();
std::fs::write(self.root().join("project.rockspec"), lua.to_value(&self.partial_rockspec).unwrap().to_string().unwrap()).unwrap();
}
}

impl Drop for Project {
fn drop(&mut self) {
let _ = self.flush();
}
}

impl Project {
Expand All @@ -66,6 +102,10 @@ impl Project {
&self.rockspec
}

pub fn rockspec_mut(&mut self) -> &mut PartialRockspec {
&mut self.partial_rockspec
}

pub fn tree(&self, lua_version: LuaVersion) -> io::Result<Tree> {
Tree::new(self.root.clone(), lua_version)
}
Expand Down
2 changes: 1 addition & 1 deletion rocks-lib/src/rockspec/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;

use serde::{
de::{self, DeserializeOwned},
Deserialize, Deserializer,
Deserialize, Deserializer, Serialize,
};
use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};

Expand Down

0 comments on commit 717a4d5

Please sign in to comment.