From c063230f4c3981613fa0cdb81ee9f8da847f58a6 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 8 Jun 2023 11:09:52 +0200 Subject: [PATCH] Implement home dir script discovery --- CHANGELOG.md | 9 ++++++++ packages/cli/src/utils/files.rs | 41 ++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7d0d5c..f13a772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added automatic decompression for compressed responses when using `net.request`. This behavior can be disabled by passing `options = { decompress = false }` in request params. +- Added support for finding scripts in the current home directory. + This means that if you have a script called `script-name.luau`, you can place it in the following location: + + - `C:\Users\YourName\.lune\script-name.luau` (Windows) + - `/Users/YourName/.lune/script-name.luau` (macOS) + - `/home/YourName/.lune/script-name.luau` (Linux) + + And then run it using `lune script-name` from any directory you are currently in. + - Added several new instance methods in the `roblox` builtin library: - [`Instance:AddTag`](https://create.roblox.com/docs/reference/engine/classes/Instance#AddTag) - [`Instance:GetTags`](https://create.roblox.com/docs/reference/engine/classes/Instance#GetTags) diff --git a/packages/cli/src/utils/files.rs b/packages/cli/src/utils/files.rs index ea25244..0c741cf 100644 --- a/packages/cli/src/utils/files.rs +++ b/packages/cli/src/utils/files.rs @@ -3,8 +3,9 @@ use std::{ path::{PathBuf, MAIN_SEPARATOR}, }; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, bail, Result}; use console::style; +use directories::UserDirs; use once_cell::sync::Lazy; const LUNE_COMMENT_PREFIX: &str = "-->"; @@ -42,8 +43,23 @@ static ERR_MESSAGE_HELP_NOTE: Lazy = Lazy::new(|| { path, and that they then have control over script discovery behavior, whereas if they pass in a relative path we will instead try to be as permissive as possible for user-friendliness */ -pub fn discover_script_file_path(path: &str) -> Result { - let file_path = PathBuf::from(path); +pub fn discover_script_file_path(path: &str, in_home_dir: bool) -> Result { + // NOTE: We don't actually support any platforms without home directories, + // but just in case the user has some strange configuration and it cannot + // be found we should at least throw a nice error instead of panicking + let file_path = if in_home_dir { + match UserDirs::new() { + Some(dirs) => dirs.home_dir().join(path), + None => { + bail!( + "No file was found at {}\nThe home directory does not exist", + style(path).yellow() + ) + } + } + } else { + PathBuf::from(path) + }; // NOTE: We use metadata directly here to try to // avoid accessing the file path more than once let file_meta = file_path.metadata(); @@ -112,7 +128,7 @@ pub fn discover_script_file_path(path: &str) -> Result { Behavior is otherwise exactly the same as for `discover_script_file_path`. */ pub fn discover_script_file_path_including_lune_dirs(path: &str) -> Result { - match discover_script_file_path(path) { + match discover_script_file_path(path, false) { Ok(path) => Ok(path), Err(e) => { // If we got any absolute path it means the user has also @@ -121,10 +137,19 @@ pub fn discover_script_file_path_including_lune_dirs(path: &str) -> Result Err(e),