Skip to content

Commit

Permalink
Support listing directories with init.lua(u) files in them (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev authored Oct 13, 2023
1 parent e16c28f commit ec6060a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Cli {
// This will also exit early and not run anything else
if self.list {
let sorted_relative = find_lune_scripts(false).await.map(sort_lune_scripts);

let sorted_home_dir = find_lune_scripts(true).await.map(sort_lune_scripts);
if sorted_relative.is_err() && sorted_home_dir.is_err() {
eprintln!("{}", sorted_relative.unwrap_err());
Expand Down
1 change: 1 addition & 0 deletions src/cli/utils/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub fn discover_script_path_including_lune_dirs(path: &str) -> Result<PathBuf> {
.or_else(|_| discover_script_path(format!(".lune{MAIN_SEPARATOR}{path}"), false))
.or_else(|_| discover_script_path(format!("lune{MAIN_SEPARATOR}{path}"), true))
.or_else(|_| discover_script_path(format!(".lune{MAIN_SEPARATOR}{path}"), true));

match res {
// NOTE: The first error message is generally more
// descriptive than the ones for the lune subfolders
Expand Down
36 changes: 33 additions & 3 deletions src/cli/utils/listing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::match_same_arms)]

use std::{cmp::Ordering, ffi::OsStr, fmt::Write as _, path::PathBuf};

use anyhow::{bail, Result};
Expand All @@ -6,7 +8,7 @@ use directories::UserDirs;
use once_cell::sync::Lazy;
use tokio::{fs, io};

use super::files::parse_lune_description_from_file;
use super::files::{discover_script_path, parse_lune_description_from_file};

pub static COLOR_BLUE: Lazy<Style> = Lazy::new(|| Style::new().blue());
pub static STYLE_DIM: Lazy<Style> = Lazy::new(|| Style::new().dim());
Expand All @@ -28,16 +30,44 @@ pub async fn find_lune_scripts(in_home_dir: bool) -> Result<Vec<(String, String)
let meta = entry.metadata().await?;
if meta.is_file() {
let contents = fs::read(entry.path()).await?;
files.push((entry, meta, contents));
} else if meta.is_dir() {
let entry_path_os = entry.path();

let Some(dir_path) = entry_path_os.to_str() else {
bail!("Failed to cast path to string slice.")
};

let init_file_path = match discover_script_path(dir_path, in_home_dir) {
Ok(init_file) => init_file,
_ => continue,
};

let contents = fs::read(init_file_path).await?;

files.push((entry, meta, contents));
}
}
let parsed: Vec<_> = files
.iter()
.filter(|(entry, _, _)| {
matches!(
let mut is_match = matches!(
entry.path().extension().and_then(OsStr::to_str),
Some("lua" | "luau")
)
);

// If the entry is not a lua or luau file, and is a directory,
// then we check if it contains a init.lua(u), and if it does,
// we include it in our Vec
if !is_match
&& entry.path().is_dir()
&& (entry.path().join("init.lua").exists()
|| entry.path().join("init.luau").exists())
{
is_match = true;
}

is_match
})
.map(|(entry, _, contents)| {
let contents_str = String::from_utf8_lossy(contents);
Expand Down

0 comments on commit ec6060a

Please sign in to comment.