mirror of
https://github.com/CompeyDev/lune-packaging.git
synced 2025-01-09 20:29:10 +00:00
Fix remaining issues with home dir script discovery
This commit is contained in:
parent
0773dc024a
commit
5d72132ecc
3 changed files with 58 additions and 27 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
borrow::BorrowMut,
|
borrow::BorrowMut,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
fmt::Write as _,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::ExitCode,
|
process::ExitCode,
|
||||||
};
|
};
|
||||||
|
@ -20,7 +21,7 @@ use crate::{
|
||||||
gen::{generate_gitbook_dir_from_definitions, generate_typedef_files_from_definitions},
|
gen::{generate_gitbook_dir_from_definitions, generate_typedef_files_from_definitions},
|
||||||
utils::{
|
utils::{
|
||||||
files::{discover_script_file_path_including_lune_dirs, strip_shebang},
|
files::{discover_script_file_path_including_lune_dirs, strip_shebang},
|
||||||
listing::{find_lune_scripts, print_lune_scripts, sort_lune_scripts},
|
listing::{find_lune_scripts, sort_lune_scripts, write_lune_scripts_list},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -92,22 +93,42 @@ impl Cli {
|
||||||
// List files in `lune` and `.lune` directories, if wanted
|
// List files in `lune` and `.lune` directories, if wanted
|
||||||
// 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 {
|
||||||
match find_lune_scripts().await {
|
let sorted_relative = match find_lune_scripts(false).await {
|
||||||
Ok(scripts) => {
|
Ok(scripts) => sort_lune_scripts(scripts),
|
||||||
let sorted = sort_lune_scripts(scripts);
|
|
||||||
if sorted.is_empty() {
|
|
||||||
println!("No scripts found.");
|
|
||||||
} else {
|
|
||||||
print!("Available scripts:");
|
|
||||||
print_lune_scripts(sorted)?;
|
|
||||||
}
|
|
||||||
return Ok(ExitCode::SUCCESS);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{e}");
|
eprintln!("{e}");
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
let sorted_home_dir = match find_lune_scripts(true).await {
|
||||||
|
Ok(scripts) => sort_lune_scripts(scripts),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
return Ok(ExitCode::FAILURE);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut buffer = String::new();
|
||||||
|
if !sorted_relative.is_empty() {
|
||||||
|
if sorted_home_dir.is_empty() {
|
||||||
|
write!(&mut buffer, "Available scripts:")?;
|
||||||
|
} else {
|
||||||
|
write!(&mut buffer, "Available scripts in current directory:")?;
|
||||||
|
}
|
||||||
|
write_lune_scripts_list(&mut buffer, sorted_relative)?;
|
||||||
}
|
}
|
||||||
|
if !sorted_home_dir.is_empty() {
|
||||||
|
write!(&mut buffer, "Available global scripts:")?;
|
||||||
|
write_lune_scripts_list(&mut buffer, sorted_home_dir)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if buffer.is_empty() {
|
||||||
|
println!("No scripts found.");
|
||||||
|
} else {
|
||||||
|
print!("{buffer}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(ExitCode::SUCCESS);
|
||||||
}
|
}
|
||||||
// Generate (save) definition files, if wanted
|
// Generate (save) definition files, if wanted
|
||||||
let generate_file_requested = self.setup
|
let generate_file_requested = self.setup
|
||||||
|
|
|
@ -93,7 +93,7 @@ pub fn discover_script_file_path(path: &str, in_home_dir: bool) -> Result<PathBu
|
||||||
"No file was found at {}, found a directory",
|
"No file was found at {}, found a directory",
|
||||||
style(file_path.display()).yellow()
|
style(file_path.display()).yellow()
|
||||||
))
|
))
|
||||||
} else if is_abs {
|
} else if is_abs && !in_home_dir {
|
||||||
Err(anyhow!(
|
Err(anyhow!(
|
||||||
"No file was found at {}",
|
"No file was found at {}",
|
||||||
style(file_path.display()).yellow()
|
style(file_path.display()).yellow()
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use std::{cmp::Ordering, fmt::Write as _};
|
use std::{cmp::Ordering, ffi::OsStr, fmt::Write as _, path::PathBuf};
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use console::Style;
|
use console::Style;
|
||||||
|
use directories::UserDirs;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use tokio::{fs, io};
|
use tokio::{fs, io};
|
||||||
|
|
||||||
|
@ -10,10 +11,15 @@ use super::files::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());
|
||||||
|
|
||||||
pub async fn find_lune_scripts() -> Result<Vec<(String, String)>> {
|
pub async fn find_lune_scripts(in_home_dir: bool) -> Result<Vec<(String, String)>> {
|
||||||
let mut lune_dir = fs::read_dir("lune").await;
|
let base_path = if in_home_dir {
|
||||||
|
UserDirs::new().unwrap().home_dir().to_path_buf()
|
||||||
|
} else {
|
||||||
|
PathBuf::new()
|
||||||
|
};
|
||||||
|
let mut lune_dir = fs::read_dir(base_path.join("lune")).await;
|
||||||
if lune_dir.is_err() {
|
if lune_dir.is_err() {
|
||||||
lune_dir = fs::read_dir(".lune").await;
|
lune_dir = fs::read_dir(base_path.join(".lune")).await;
|
||||||
}
|
}
|
||||||
match lune_dir {
|
match lune_dir {
|
||||||
Ok(mut dir) => {
|
Ok(mut dir) => {
|
||||||
|
@ -27,6 +33,12 @@ pub async fn find_lune_scripts() -> Result<Vec<(String, String)>> {
|
||||||
}
|
}
|
||||||
let parsed: Vec<_> = files
|
let parsed: Vec<_> = files
|
||||||
.iter()
|
.iter()
|
||||||
|
.filter(|(entry, _, _)| {
|
||||||
|
matches!(
|
||||||
|
entry.path().extension().and_then(OsStr::to_str),
|
||||||
|
Some("lua" | "luau")
|
||||||
|
)
|
||||||
|
})
|
||||||
.map(|(entry, _, contents)| {
|
.map(|(entry, _, contents)| {
|
||||||
let contents_str = String::from_utf8_lossy(contents);
|
let contents_str = String::from_utf8_lossy(contents);
|
||||||
let file_path = entry.path().with_extension("");
|
let file_path = entry.path().with_extension("");
|
||||||
|
@ -65,7 +77,7 @@ pub fn sort_lune_scripts(scripts: Vec<(String, String)>) -> Vec<(String, String)
|
||||||
sorted
|
sorted
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_lune_scripts(scripts: Vec<(String, String)>) -> Result<()> {
|
pub fn write_lune_scripts_list(buffer: &mut String, scripts: Vec<(String, String)>) -> Result<()> {
|
||||||
let longest_file_name_len = scripts
|
let longest_file_name_len = scripts
|
||||||
.iter()
|
.iter()
|
||||||
.fold(0, |acc, (file_name, _)| acc.max(file_name.len()));
|
.fold(0, |acc, (file_name, _)| acc.max(file_name.len()));
|
||||||
|
@ -74,25 +86,24 @@ pub fn print_lune_scripts(scripts: Vec<(String, String)>) -> Result<()> {
|
||||||
let prefix = format!("{} ", COLOR_BLUE.apply_to('>'));
|
let prefix = format!("{} ", COLOR_BLUE.apply_to('>'));
|
||||||
let separator = format!("{}", STYLE_DIM.apply_to('-'));
|
let separator = format!("{}", STYLE_DIM.apply_to('-'));
|
||||||
// Write the entire output to a buffer, doing this instead of using individual
|
// Write the entire output to a buffer, doing this instead of using individual
|
||||||
// println! calls will ensure that no output get mixed up in between these lines
|
// writeln! calls will ensure that no output get mixed up in between these lines
|
||||||
let mut buffer = String::new();
|
|
||||||
if script_with_description_exists {
|
if script_with_description_exists {
|
||||||
for (file_name, description) in scripts {
|
for (file_name, description) in scripts {
|
||||||
if description.is_empty() {
|
if description.is_empty() {
|
||||||
write!(&mut buffer, "\n{prefix}{file_name}")?;
|
write!(buffer, "\n{prefix}{file_name}")?;
|
||||||
} else {
|
} else {
|
||||||
let mut lines = description.lines();
|
let mut lines = description.lines();
|
||||||
let first_line = lines.next().unwrap_or_default();
|
let first_line = lines.next().unwrap_or_default();
|
||||||
let file_spacing = " ".repeat(file_name.len());
|
let file_spacing = " ".repeat(file_name.len());
|
||||||
let line_spacing = " ".repeat(longest_file_name_len - file_name.len());
|
let line_spacing = " ".repeat(longest_file_name_len - file_name.len());
|
||||||
write!(
|
write!(
|
||||||
&mut buffer,
|
buffer,
|
||||||
"\n{prefix}{file_name}{line_spacing} {separator} {}",
|
"\n{prefix}{file_name}{line_spacing} {separator} {}",
|
||||||
COLOR_BLUE.apply_to(first_line)
|
COLOR_BLUE.apply_to(first_line)
|
||||||
)?;
|
)?;
|
||||||
for line in lines {
|
for line in lines {
|
||||||
write!(
|
write!(
|
||||||
&mut buffer,
|
buffer,
|
||||||
"\n{prefix}{file_spacing}{line_spacing} {}",
|
"\n{prefix}{file_spacing}{line_spacing} {}",
|
||||||
COLOR_BLUE.apply_to(line)
|
COLOR_BLUE.apply_to(line)
|
||||||
)?;
|
)?;
|
||||||
|
@ -101,11 +112,10 @@ pub fn print_lune_scripts(scripts: Vec<(String, String)>) -> Result<()> {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (file_name, _) in scripts {
|
for (file_name, _) in scripts {
|
||||||
write!(&mut buffer, "\n{prefix}{file_name}")?;
|
write!(buffer, "\n{prefix}{file_name}")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Finally, print the entire buffer out
|
// Finally, write an ending newline
|
||||||
// with an ending newline added to it
|
writeln!(buffer)?;
|
||||||
println!("{buffer}");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue