Implement passing scripts to cli through stdin, minor bug fix

This commit is contained in:
Filip Tibell 2023-03-07 22:07:53 +01:00
parent 9cb793cdea
commit cbd4ba967e
No known key found for this signature in database
4 changed files with 40 additions and 16 deletions

View file

@ -12,12 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added support for reading scripts from stdin by passing `"-"` as the script name
- Added support for close codes in the `net` WebSocket APIs: - Added support for close codes in the `net` WebSocket APIs:
- A close code can be sent by passing it to `socket.close` - A close code can be sent by passing it to `socket.close`
- A received close code can be checked with the `socket.closeCode` value, which is populated after a socket has been closed - note that using `socket.close` will not set the close code value, it is only set when received and is guaranteed to exist after closure - A received close code can be checked with the `socket.closeCode` value, which is populated after a socket has been closed - note that using `socket.close` will not set the close code value, it is only set when received and is guaranteed to exist after closure
### Fixed ### Fixed
- Fixed scripts having to be valid utf8, they may now use any kind of encoding that base Luau supports
- The `net` WebSocket APIs will no longer return `nil` for partial messages being received in `socket.next`, and will instead wait for the full message to arrive - The `net` WebSocket APIs will no longer return `nil` for partial messages being received in `socket.next`, and will instead wait for the full message to arrive
## `0.5.3` - February 26th, 2023 ## `0.5.3` - February 26th, 2023
@ -25,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fixed `lune --generate-selene-types` generating an invalid Selene definitions file - Fixed `lune --generate-selene-types` generating an invalid Selene definitions file
- Fixed (maybe) type definition parsing issues on Windows - Fixed type definition parsing issues on Windows
## `0.5.2` - February 26th, 2023 ## `0.5.2` - February 26th, 2023

View file

@ -38,6 +38,12 @@ lune --list
Lists all scripts found in `lune` or `.lune` directories, including any top-level description comments. <br /> Lists all scripts found in `lune` or `.lune` directories, including any top-level description comments. <br />
Lune description comments are always written at the top of a file and start with a lua-style comment arrow (`-->`). Lune description comments are always written at the top of a file and start with a lua-style comment arrow (`-->`).
```sh
lune -
```
Runs a script passed to Lune using stdin. Occasionally useful for running scripts piped to Lune from external sources.
--- ---
**_<sup>[1]</sup>_** _Lune also supports files with the `.lua` extension but using the `.luau` extension is highly recommended. Additionally, if you don't want Lune to look in sub-directories you can provide a full file path with the file extension included, instead of only the file name._ **_<sup>[1]</sup>_** _Lune also supports files with the `.lua` extension but using the `.luau` extension is highly recommended. Additionally, if you don't want Lune to look in sub-directories you can provide a full file path with the file extension included, instead of only the file name._

View file

@ -1,10 +1,13 @@
use std::process::ExitCode; use std::process::ExitCode;
use anyhow::Result; use anyhow::{Context, Result};
use clap::{CommandFactory, Parser}; use clap::{CommandFactory, Parser};
use lune::Lune; use lune::Lune;
use tokio::fs::{read_to_string, write}; use tokio::{
fs::{read as read_to_vec, write},
io::{stdin, AsyncReadExt},
};
use crate::{ use crate::{
gen::{ gen::{
@ -153,14 +156,27 @@ impl Cli {
let cmd = Cli::command(); let cmd = Cli::command();
cmd.arg_required_else_help(true).get_matches(); cmd.arg_required_else_help(true).get_matches();
} }
// Parse and read the wanted file // Figure out if we should read from stdin or from a file,
let file_path = find_parse_file_path(&self.script_path.unwrap())?; // reading from stdin is marked by passing a single "-"
let file_contents = read_to_string(&file_path).await?; // (dash) as the script name to run to the cli
// Display the file path relative to cwd with no extensions in stack traces let script_path = self.script_path.unwrap();
let (script_display_name, script_contents) = if script_path == "-" {
let mut stdin_contents = Vec::new();
stdin()
.read_to_end(&mut stdin_contents)
.await
.context("Failed to read script contents from stdin")?;
("stdin".to_string(), stdin_contents)
} else {
let file_path = find_parse_file_path(&script_path)?;
let file_contents = read_to_vec(&file_path).await?;
// NOTE: We skip the extension here to remove it from stack traces
let file_display_name = file_path.with_extension("").display().to_string(); let file_display_name = file_path.with_extension("").display().to_string();
(file_display_name, file_contents)
};
// Create a new lune object with all globals & run the script // Create a new lune object with all globals & run the script
let lune = Lune::new().with_args(self.script_args); let lune = Lune::new().with_args(self.script_args);
let result = lune.run(&file_display_name, &file_contents).await; let result = lune.run(&script_display_name, &script_contents).await;
Ok(match result { Ok(match result {
Err(e) => { Err(e) => {
eprintln!("{e}"); eprintln!("{e}");

View file

@ -54,8 +54,8 @@ impl Lune {
*/ */
pub async fn run( pub async fn run(
&self, &self,
script_name: &str, script_name: impl AsRef<str>,
script_contents: &str, script_contents: impl AsRef<[u8]>,
) -> Result<ExitCode, LuaError> { ) -> Result<ExitCode, LuaError> {
// Create our special lune-flavored Lua object with extra registry values // Create our special lune-flavored Lua object with extra registry values
let lua = create_lune_lua().expect("Failed to create Lua object"); let lua = create_lune_lua().expect("Failed to create Lua object");
@ -64,8 +64,8 @@ impl Lune {
lua.set_app_data(sched); lua.set_app_data(sched);
// Create the main thread and schedule it // Create the main thread and schedule it
let main_chunk = lua let main_chunk = lua
.load(script_contents) .load(script_contents.as_ref())
.set_name(script_name) .set_name(script_name.as_ref())
.unwrap() .unwrap()
.into_function() .into_function()
.unwrap(); .unwrap();