From 82d085b11ef2cd4a7037c6485a7f4abd68e40278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:56:09 +0200 Subject: [PATCH] Implement package subcommand --- src/subcommands/build.rs | 2 +- src/subcommands/package.rs | 62 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/subcommands/build.rs b/src/subcommands/build.rs index 4814540..805d1e7 100644 --- a/src/subcommands/build.rs +++ b/src/subcommands/build.rs @@ -11,7 +11,7 @@ use std::{fs, path::PathBuf}; pub struct BuildArgs { /// The path of the project to build. #[clap(default_value = ".")] - path: PathBuf, + pub path: PathBuf, } pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> { diff --git a/src/subcommands/package.rs b/src/subcommands/package.rs index 89a8899..d9c48f3 100644 --- a/src/subcommands/package.rs +++ b/src/subcommands/package.rs @@ -1,4 +1,12 @@ -use crate::error::Result; +use std::fs; + +use path_absolutize::Absolutize; + +use crate::{ + config::ProjectConfig, + error::{Error, Result}, + terminal_output::{print_error, print_info}, +}; use super::BuildArgs; @@ -9,8 +17,56 @@ pub struct PackageArgs { } pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> { - println!("PACKAGE"); - println!(" - Args: {:?}", args); + let path = args.build_args.path.as_path(); + + print_info(&format!( + "Packaging project at {}", + path.absolutize()?.display() + )); + + let toml_path = if !path.exists() { + print_error("The specified path does not exist."); + return Err(Error::PathNotFoundError(path.to_path_buf())); + } else if path.is_dir() { + let toml_path = path.join("pack.toml"); + if !toml_path.exists() { + print_error("The specified directory does not contain a pack.toml file."); + return Err(Error::InvalidPackPathError(path.to_path_buf())); + } + toml_path + } else if path.is_file() + && path + .file_name() + .ok_or(Error::InvalidPackPathError(path.to_path_buf()))? + == "pack.toml" + { + path.to_path_buf() + } else { + print_error("The specified path is neither a directory nor a pack.toml file."); + return Err(Error::InvalidPackPathError(path.to_path_buf())); + }; + + 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 dist_path = toml_path + .parent() + .expect("Failed to get parent directory of pack.toml") + .join("dist") + .join(project_config.pack.name + ".zip"); + + compiled.zip(&dist_path)?; + + print_info(&format!( + "Finished packaging project to {}", + dist_path.absolutize_from(path)?.display() + )); Ok(()) }