Use lune folder for scripts

Also improve cli logic for finding wanted script
This commit is contained in:
Filip Tibell 2023-01-18 21:35:57 -05:00
parent dd947d7604
commit 7e57fed8d8
No known key found for this signature in database
3 changed files with 49 additions and 48 deletions

View file

@ -2,7 +2,7 @@ print("Hello, lune! 🌙")
-- Use a function from another module
local module = require("scripts/module")
local module = require(".lune/module")
module.hello()
-- Read and print out directories & files in

View file

@ -1,4 +1,7 @@
use std::{fs::read_to_string, path::PathBuf};
use std::{
fs::read_to_string,
path::{PathBuf, MAIN_SEPARATOR},
};
use clap::Parser;
use mlua::{Lua, Result};
@ -21,54 +24,9 @@ impl Cli {
}
}
fn parse_file_path(&self) -> Result<PathBuf> {
let parsed_file_path = match &self.path {
path if path.ends_with(".luau") || path.ends_with(".lua") => Some(PathBuf::from(path)),
path => {
let temp_path = PathBuf::from(path);
if temp_path.extension().is_none() {
let as_luau_path = temp_path.with_extension("luau");
let as_lua_path = temp_path.with_extension("lua");
if as_luau_path.exists() {
Some(as_luau_path)
} else if as_lua_path.exists() {
Some(as_lua_path)
} else {
let as_luau_in_scripts_folder = PathBuf::from("scripts").join(as_luau_path);
let as_lua_in_scripts_folder = PathBuf::from("scripts").join(as_lua_path);
if as_luau_in_scripts_folder.exists() {
Some(as_luau_in_scripts_folder)
} else if as_lua_in_scripts_folder.exists() {
Some(as_lua_in_scripts_folder)
} else {
None
}
}
} else {
None
}
}
};
if let Some(file_path) = parsed_file_path {
if file_path.exists() {
Ok(file_path)
} else {
Err(mlua::Error::RuntimeError(format!(
"File does not exist at path: '{}'",
self.path
)))
}
} else {
Err(mlua::Error::RuntimeError(format!(
"Invalid file path: '{}'",
self.path
)))
}
}
pub async fn run(self) -> Result<()> {
// Parse and read the wanted file
let file_path = self.parse_file_path()?;
let file_path = find_parse_file_path(&self.path)?;
let file_contents = read_to_string(file_path)?;
// Create a new lua state and add in all lune globals
let lua = Lua::new();
@ -82,3 +40,46 @@ impl Cli {
Ok(())
}
}
fn find_luau_file_path(path: &str) -> Option<PathBuf> {
let file_path = PathBuf::from(path);
if let Some(ext) = file_path.extension() {
match ext {
e if e == "lua" || e == "luau" && file_path.exists() => Some(file_path),
_ => None,
}
} else {
let file_path_lua = PathBuf::from(path).with_extension("lua");
if file_path_lua.exists() {
Some(file_path_lua)
} else {
let file_path_luau = PathBuf::from(path).with_extension("luau");
if file_path_luau.exists() {
Some(file_path_luau)
} else {
None
}
}
}
}
fn find_parse_file_path(path: &str) -> Result<PathBuf> {
let parsed_file_path = find_luau_file_path(path)
.or_else(|| find_luau_file_path(&format!("lune{MAIN_SEPARATOR}{path}")))
.or_else(|| find_luau_file_path(&format!(".lune{MAIN_SEPARATOR}{path}")));
if let Some(file_path) = parsed_file_path {
if file_path.exists() {
Ok(file_path)
} else {
Err(mlua::Error::RuntimeError(format!(
"File does not exist at path: '{}'",
path
)))
}
} else {
Err(mlua::Error::RuntimeError(format!(
"Invalid file path: '{}'",
path
)))
}
}