From 7e57fed8d8a1230135f5676e59b86c896c90a2c8 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 18 Jan 2023 21:35:57 -0500 Subject: [PATCH] Use lune folder for scripts Also improve cli logic for finding wanted script --- {scripts => .lune}/hello_lune.luau | 2 +- {scripts => .lune}/module.luau | 0 src/cli.rs | 95 +++++++++++++++--------------- 3 files changed, 49 insertions(+), 48 deletions(-) rename {scripts => .lune}/hello_lune.luau (97%) rename {scripts => .lune}/module.luau (100%) diff --git a/scripts/hello_lune.luau b/.lune/hello_lune.luau similarity index 97% rename from scripts/hello_lune.luau rename to .lune/hello_lune.luau index 817eefb..5d0df58 100644 --- a/scripts/hello_lune.luau +++ b/.lune/hello_lune.luau @@ -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 diff --git a/scripts/module.luau b/.lune/module.luau similarity index 100% rename from scripts/module.luau rename to .lune/module.luau diff --git a/src/cli.rs b/src/cli.rs index 98cc2d4..952cd42 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 { - 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 { + 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 { + 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 + ))) + } +}