Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lune scripts as directories with init.lua(u) files in them #119

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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