From 07e1fa90e44c046d81261aa7032b3258a077a4d8 Mon Sep 17 00:00:00 2001 From: Vhyrro Date: Sat, 28 Dec 2024 18:27:44 +0100 Subject: [PATCH] feat: `rocks check` --- rocks-bin/src/check.rs | 38 ++++++++++++++++++++++++++++++++++++ rocks-bin/src/lib.rs | 3 +++ rocks-bin/src/main.rs | 4 ++++ rocks-lib/src/project/mod.rs | 14 +++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 rocks-bin/src/check.rs diff --git a/rocks-bin/src/check.rs b/rocks-bin/src/check.rs new file mode 100644 index 00000000..07ab8d69 --- /dev/null +++ b/rocks-bin/src/check.rs @@ -0,0 +1,38 @@ +use eyre::{OptionExt, Result}; +use rocks_lib::{ + build::BuildBehaviour, + config::Config, + lockfile::PinnedState::Pinned, + manifest::Manifest, + operations::{self, install}, + progress::MultiProgress, + project::Project, +}; + +pub async fn check(config: Config) -> Result<()> { + let project = Project::current()?.ok_or_eyre("Not in a project!")?; + + let src_dir = project.src_dir().ok_or_eyre( + "No source directory found! Expected a `src/` or `lua/` directory at the project root.", + )?; + + let manifest = Manifest::from_config(config.server(), &config).await?; + + install( + vec![(BuildBehaviour::NoForce, "luacheck".parse()?)], + Pinned, + &manifest, + &config, + MultiProgress::new_arc(), + ) + .await?; + + operations::run( + "luacheck", + vec![src_dir.to_str().unwrap().to_string()], + config, + ) + .await?; + + Ok(()) +} diff --git a/rocks-bin/src/lib.rs b/rocks-bin/src/lib.rs index 33d0fa72..de099f75 100644 --- a/rocks-bin/src/lib.rs +++ b/rocks-bin/src/lib.rs @@ -21,6 +21,7 @@ use update::Update; use upload::Upload; pub mod build; +pub mod check; pub mod debug; pub mod download; pub mod fetch; @@ -110,6 +111,8 @@ pub enum Commands { Add, /// Build/compile a rock. Build(Build), + /// Runs `luacheck` in the current project. + Check, /// [UNIMPLEMENTED] Query information about Rocks's configuration. Config, /// Various debugging utilities. diff --git a/rocks-bin/src/main.rs b/rocks-bin/src/main.rs index f03726c3..baef281f 100644 --- a/rocks-bin/src/main.rs +++ b/rocks-bin/src/main.rs @@ -3,6 +3,7 @@ use std::{path::PathBuf, time::Duration}; use clap::{Parser, Subcommand}; use rocks::{ build::{self, Build}, + check, debug::Debug, download::{self, Download}, fetch, format, @@ -95,6 +96,8 @@ pub enum Commands { Add, /// Build/compile a rock. Build(Build), + /// Runs `luacheck` in the current project. + Check, /// [UNIMPLEMENTED] Query information about Rocks's configuration. Config, /// Various debugging utilities. @@ -211,6 +214,7 @@ async fn main() { 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::Upload(upload_data) => upload::upload(upload_data, config).await.unwrap(), + Commands::Check => check::check(config).await.unwrap(), Commands::Add => unimplemented!(), Commands::Config => unimplemented!(), Commands::Doc => unimplemented!(), diff --git a/rocks-lib/src/project/mod.rs b/rocks-lib/src/project/mod.rs index 9d40af81..ee649416 100644 --- a/rocks-lib/src/project/mod.rs +++ b/rocks-lib/src/project/mod.rs @@ -73,6 +73,20 @@ impl Project { pub fn tree(&self, lua_version: LuaVersion) -> io::Result { Tree::new(self.root.clone(), lua_version) } + + pub fn src_dir(&self) -> Option { + let root = self.root(); + + // Prioritise the `lua/` directory, in case the project + // is mixed with a different language which also has a `src/` directory. + if root.join("lua").exists() { + Some(root.join("lua")) + } else if root.join("src").exists() { + Some(root.join("src")) + } else { + None + } + } } // TODO: Add plenty of tests