change build and package commands to use all shu files in src directory instead of only main.shu
This commit is contained in:
parent
ebb8fff3d3
commit
0528501f07
|
@ -6,7 +6,10 @@ use crate::{
|
||||||
error::Error,
|
error::Error,
|
||||||
terminal_output::{print_error, print_info},
|
terminal_output::{print_error, print_info},
|
||||||
};
|
};
|
||||||
use std::{fs, path::PathBuf};
|
use std::{
|
||||||
|
env, fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, clap::Args, Clone)]
|
#[derive(Debug, clap::Args, Clone)]
|
||||||
pub struct BuildArgs {
|
pub struct BuildArgs {
|
||||||
|
@ -45,14 +48,23 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
|
||||||
return Err(Error::InvalidPackPathError(path.to_path_buf()))?;
|
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 toml_content = fs::read_to_string(&toml_path)?;
|
||||||
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
|
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
|
||||||
|
|
||||||
let main_path = toml_path
|
let script_paths = get_script_paths(
|
||||||
.parent()
|
&toml_path
|
||||||
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
|
.parent()
|
||||||
.join("src/main.shu");
|
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
|
||||||
let compiled = shulkerscript_lang::compile(&main_path)?;
|
.join("src"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let compiled = shulkerscript_lang::compile(&script_paths)?;
|
||||||
|
|
||||||
let dist_path = toml_path
|
let dist_path = toml_path
|
||||||
.parent()
|
.parent()
|
||||||
|
@ -69,3 +81,45 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
|
||||||
|
|
||||||
Ok(())
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -43,7 +43,8 @@ pub fn lang_debug(args: &LangDebugArgs) -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DumpState::Datapack => {
|
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 {
|
if args.pretty {
|
||||||
println!("{:#?}", datapack);
|
println!("{:#?}", datapack);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -50,11 +50,14 @@ pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
|
||||||
let toml_content = fs::read_to_string(&toml_path)?;
|
let toml_content = fs::read_to_string(&toml_path)?;
|
||||||
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
|
let project_config = toml::from_str::<ProjectConfig>(&toml_content)?;
|
||||||
|
|
||||||
let main_path = toml_path
|
let script_paths = super::build::get_script_paths(
|
||||||
.parent()
|
&toml_path
|
||||||
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
|
.parent()
|
||||||
.join("src/main.shu");
|
.ok_or(Error::InvalidPackPathError(path.to_path_buf()))?
|
||||||
let compiled = shulkerscript_lang::compile(&main_path)?;
|
.join("src"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let compiled = shulkerscript_lang::compile(&script_paths)?;
|
||||||
|
|
||||||
let dist_path = toml_path
|
let dist_path = toml_path
|
||||||
.parent()
|
.parent()
|
||||||
|
|
Loading…
Reference in New Issue