From 0528501f074d3abf329f7c92e22baeb6aa45ce40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sun, 9 Jun 2024 21:22:13 +0200 Subject: [PATCH] change build and package commands to use all shu files in src directory instead of only main.shu --- src/subcommands/build.rs | 66 +++++++++++++++++++++++++++++++---- src/subcommands/lang_debug.rs | 3 +- src/subcommands/package.rs | 13 ++++--- 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/subcommands/build.rs b/src/subcommands/build.rs index e318b22..746268a 100644 --- a/src/subcommands/build.rs +++ b/src/subcommands/build.rs @@ -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::(&toml_content)?; - let main_path = toml_path - .parent() - .ok_or(Error::InvalidPackPathError(path.to_path_buf()))? - .join("src/main.shu"); - let compiled = shulkerscript_lang::compile(&main_path)?; + let script_paths = get_script_paths( + &toml_path + .parent() + .ok_or(Error::InvalidPackPathError(path.to_path_buf()))? + .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> { + _get_script_paths(path, "") +} + +fn _get_script_paths(path: &Path, prefix: &str) -> std::io::Result> { + 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()) + } +} diff --git a/src/subcommands/lang_debug.rs b/src/subcommands/lang_debug.rs index 9475708..3eed643 100644 --- a/src/subcommands/lang_debug.rs +++ b/src/subcommands/lang_debug.rs @@ -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 { diff --git a/src/subcommands/package.rs b/src/subcommands/package.rs index 203ad54..e0f70aa 100644 --- a/src/subcommands/package.rs +++ b/src/subcommands/package.rs @@ -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::(&toml_content)?; - let main_path = toml_path - .parent() - .ok_or(Error::InvalidPackPathError(path.to_path_buf()))? - .join("src/main.shu"); - let compiled = shulkerscript_lang::compile(&main_path)?; + let script_paths = super::build::get_script_paths( + &toml_path + .parent() + .ok_or(Error::InvalidPackPathError(path.to_path_buf()))? + .join("src"), + )?; + + let compiled = shulkerscript_lang::compile(&script_paths)?; let dist_path = toml_path .parent()