change build and package commands to use all shu files in src directory instead of only main.shu

This commit is contained in:
Moritz Hölting 2024-06-09 21:22:13 +02:00
parent ebb8fff3d3
commit 0528501f07
3 changed files with 70 additions and 12 deletions

View File

@ -6,7 +6,10 @@ use crate::{
error::Error,
terminal_output::{print_error, print_info},
};
use std::{fs, path::PathBuf};
use std::{
env, fs,
path::{Path, PathBuf},
};
#[derive(Debug, clap::Args, Clone)]
pub struct BuildArgs {
@ -45,14 +48,23 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
return Err(Error::InvalidPackPathError(path.to_path_buf()))?;
};
env::set_current_dir(
toml_path
.parent()
.expect("Failed to get parent directory of pack.toml"),
)?;
let toml_content = fs::read_to_string(&toml_path)?;
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
let main_path = toml_path
let script_paths = get_script_paths(
&toml_path
.parent()
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
.join("src/main.shu");
let compiled = shulkerscript_lang::compile(&main_path)?;
.join("src"),
)?;
let compiled = shulkerscript_lang::compile(&script_paths)?;
let dist_path = toml_path
.parent()
@ -69,3 +81,45 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
Ok(())
}
/// Recursively get all script paths in a directory.
pub(super) fn get_script_paths(path: &Path) -> std::io::Result<Vec<(String, PathBuf)>> {
_get_script_paths(path, "")
}
fn _get_script_paths(path: &Path, prefix: &str) -> std::io::Result<Vec<(String, PathBuf)>> {
if path.exists() && path.is_dir() {
let contents = path.read_dir()?;
let mut paths = Vec::new();
for entry in contents {
let path = entry?.path();
if path.is_dir() {
let prefix = path
.absolutize()?
.file_name()
.unwrap()
.to_str()
.expect("Invalid folder name")
.to_string()
+ "/";
paths.extend(_get_script_paths(&path, &prefix)?);
} else if path.extension().unwrap_or_default() == "shu" {
paths.push((
prefix.to_string()
+ path
.file_stem()
.expect("ShulkerScript files are not allowed to have empty names")
.to_str()
.expect("Invalid characters in filename"),
path,
));
}
}
Ok(paths)
} else {
Ok(Vec::new())
}
}

View File

@ -43,7 +43,8 @@ pub fn lang_debug(args: &LangDebugArgs) -> Result<()> {
}
}
DumpState::Datapack => {
let datapack = shulkerscript_lang::transpile(&args.path)?;
let program_paths = super::build::get_script_paths(&args.path.join("src"))?;
let datapack = shulkerscript_lang::transpile(&program_paths)?;
if args.pretty {
println!("{:#?}", datapack);
} else {

View File

@ -50,11 +50,14 @@ pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
let toml_content = fs::read_to_string(&toml_path)?;
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
let main_path = toml_path
let script_paths = super::build::get_script_paths(
&toml_path
.parent()
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
.join("src/main.shu");
let compiled = shulkerscript_lang::compile(&main_path)?;
.join("src"),
)?;
let compiled = shulkerscript_lang::compile(&script_paths)?;
let dist_path = toml_path
.parent()