Support listing directories with init.lua(u) files in them (#119)

This commit is contained in:
Erica Marigold 2023-10-12 19:50:20 -07:00 committed by GitHub
parent e16c28fd40
commit ec6060a6cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View file

@ -84,6 +84,7 @@ impl Cli {
// This will also exit early and not run anything else // This will also exit early and not run anything else
if self.list { if self.list {
let sorted_relative = find_lune_scripts(false).await.map(sort_lune_scripts); 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); let sorted_home_dir = find_lune_scripts(true).await.map(sort_lune_scripts);
if sorted_relative.is_err() && sorted_home_dir.is_err() { if sorted_relative.is_err() && sorted_home_dir.is_err() {
eprintln!("{}", sorted_relative.unwrap_err()); eprintln!("{}", sorted_relative.unwrap_err());

View file

@ -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}"), false))
.or_else(|_| discover_script_path(format!("lune{MAIN_SEPARATOR}{path}"), true)) .or_else(|_| discover_script_path(format!("lune{MAIN_SEPARATOR}{path}"), true))
.or_else(|_| discover_script_path(format!(".lune{MAIN_SEPARATOR}{path}"), true)); .or_else(|_| discover_script_path(format!(".lune{MAIN_SEPARATOR}{path}"), true));
match res { match res {
// NOTE: The first error message is generally more // NOTE: The first error message is generally more
// descriptive than the ones for the lune subfolders // descriptive than the ones for the lune subfolders

View file

@ -1,3 +1,5 @@
#![allow(clippy::match_same_arms)]
use std::{cmp::Ordering, ffi::OsStr, fmt::Write as _, path::PathBuf}; use std::{cmp::Ordering, ffi::OsStr, fmt::Write as _, path::PathBuf};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
@ -6,7 +8,7 @@ use directories::UserDirs;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tokio::{fs, io}; 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 COLOR_BLUE: Lazy<Style> = Lazy::new(|| Style::new().blue());
pub static STYLE_DIM: Lazy<Style> = Lazy::new(|| Style::new().dim()); pub static STYLE_DIM: Lazy<Style> = Lazy::new(|| Style::new().dim());
@ -28,16 +30,44 @@ pub async fn find_lune_scripts(in_home_dir: bool) -> Result<Vec<(String, String)
let meta = entry.metadata().await?; let meta = entry.metadata().await?;
if meta.is_file() { if meta.is_file() {
let contents = fs::read(entry.path()).await?; 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)); files.push((entry, meta, contents));
} }
} }
let parsed: Vec<_> = files let parsed: Vec<_> = files
.iter() .iter()
.filter(|(entry, _, _)| { .filter(|(entry, _, _)| {
matches!( let mut is_match = matches!(
entry.path().extension().and_then(OsStr::to_str), entry.path().extension().and_then(OsStr::to_str),
Some("lua" | "luau") 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)| { .map(|(entry, _, contents)| {
let contents_str = String::from_utf8_lossy(contents); let contents_str = String::from_utf8_lossy(contents);