mirror of
https://github.com/lune-org/lune.git
synced 2025-04-11 22:10:53 +01:00
feat: support directories with init file for lune scripts
This commit is contained in:
parent
1aa6aef679
commit
94fe4b8ac0
5 changed files with 93 additions and 4 deletions
3
.lune/other_dir_script/init.lua
Normal file
3
.lune/other_dir_script/init.lua
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
--> Hello, there?
|
||||||
|
|
||||||
|
print("Hello, world! -- from init.lua")
|
3
.lune/some_dir_script/init.luau
Normal file
3
.lune/some_dir_script/init.luau
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
--> Hello, there!
|
||||||
|
|
||||||
|
print("Hello, world! -- from init.luau")
|
|
@ -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());
|
||||||
|
|
|
@ -149,8 +149,56 @@ pub fn discover_script_path_including_lune_dirs(path: &str) -> Result<PathBuf> {
|
||||||
// directories + the home directory for the current user
|
// directories + the home directory for the current user
|
||||||
let res = discover_script_path(format!("lune{MAIN_SEPARATOR}{path}"), false)
|
let res = 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}"), false))
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!("lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.lua"),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!(".lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.lua"),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!("lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.luau"),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!(".lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.luau"),
|
||||||
|
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))
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!("lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.lua"),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!(".lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.lua"),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!("lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.luau"),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
discover_script_path(
|
||||||
|
format!(".lune{MAIN_SEPARATOR}{path}{MAIN_SEPARATOR}init.luau"),
|
||||||
|
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
|
||||||
|
|
|
@ -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,48 @@ 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),
|
||||||
|
discover_script_path(dir_path, in_home_dir),
|
||||||
|
) {
|
||||||
|
(Ok(lune_init), _) => lune_init,
|
||||||
|
(_, Ok(dotlune_init)) => dotlune_init,
|
||||||
|
_ => 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);
|
||||||
|
|
Loading…
Add table
Reference in a new issue