diff --git a/src/backend/toolchain.rs b/src/backend/toolchain.rs index 8c0c2d6..a6761bd 100644 --- a/src/backend/toolchain.rs +++ b/src/backend/toolchain.rs @@ -54,8 +54,6 @@ pub fn get_java_source_files(p_ctx: &ProjectContext) -> Result, std: let files = util::directory::read_files_recursively(base_package); - print!("fuck: {}", p_ctx.dynamic_absolute_paths.base_package); - // begin sorting out java files let mut java_files: Vec = vec![]; for x in files.unwrap() { @@ -129,14 +127,15 @@ pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) -> result::R let relative_base_package_path = p_ctx.config.project.base_package.clone().replace(".", "/"); // remove the old jar - fs::remove_file(p_ctx.absolute_paths.project.clone() + "/build/artifact.jar")?; + let _ = fs::remove_file(p_ctx.absolute_paths.project.clone() + "/build/artifact.jar"); // build our packager command let cmd = format!( - "{}/jar -c --file=artifact.jar --manifest=MANIFEST.MF {}", + "{} -c --file=artifact.jar --manifest=MANIFEST.MF {}", tc_ctx.packager_path.to_string_lossy().clone(), relative_base_package_path ); + print_debug(format!("Running '{}'", cmd).as_str()); // run the command let output = Command::new("sh") @@ -144,14 +143,12 @@ pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) -> result::R .arg("-c") .arg(cmd) .output()?; - - if !output.status.success() { - Err("ass".to_string())?; + let process_err = String::from_utf8(output.stderr)?; + Err(format!("Failed to package jar: Packager output was: {}", process_err))? } Ok(()) - } @@ -161,20 +158,21 @@ pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) -> result::R pub fn run_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) -> result::Result<(), Box>{ // build our packager command let cmd = format!( - "{}/java -jar {}", + "{} -jar {}", tc_ctx.runtime_path.to_string_lossy().clone(), p_ctx.absolute_paths.project.clone() + "/build/artifact.jar" ); // run the command - let output = Command::new("sh") + let status = Command::new("sh") .current_dir(p_ctx.absolute_paths.project.clone() + "/build") .arg("-c") .arg(cmd) - .output()?; + .status()?; - if !output.status.success() { - Err("ass".to_string())?; + // dela with our output. We wa + if !status.success() { + Err(format!("Java process exited with non-0 status code"))? } Ok(()) diff --git a/src/frontend/command.rs b/src/frontend/command.rs index 28f7f8f..713927b 100644 --- a/src/frontend/command.rs +++ b/src/frontend/command.rs @@ -16,5 +16,5 @@ pub static INIT_CMD: Lazy = Lazy::new(|| { pub static RUN_CMD: Lazy = Lazy::new(|| { Command::new("run") .about("Build & run your Java project") - .alias("i") + .alias("r") }); diff --git a/src/frontend/service/mod.rs b/src/frontend/service/mod.rs index f112aa5..aba3a1f 100644 --- a/src/frontend/service/mod.rs +++ b/src/frontend/service/mod.rs @@ -1,6 +1,6 @@ -use crate::backend::context::{get_project_context, AbsoltuePaths, ProjectContext}; +use crate::backend::context::{AbsoltuePaths, ProjectContext}; use crate::backend::toolchain::{ - compile_project, get_toolchain_context, run_jar, ToolchainContext, + compile_project, run_jar, ToolchainContext, }; use crate::backend::{self, context, project}; use crate::frontend::terminal::{print_err, print_sameline}; @@ -12,32 +12,14 @@ use super::terminal::print_general; * Service function for the `run` command */ pub fn run( - override_p_ctx: Option, - override_tc_ctx: Option, + mut p_ctx: ProjectContext, + mut tc_ctx: ToolchainContext, ) -> result::Result<(), Box> { - // handle an override project context - let mut p_ctx: ProjectContext; - match override_p_ctx { - Some(v) => p_ctx = v, - None => p_ctx = get_project_context()?, - } - - // handle an override toolchain context - let mut tc_ctx: ToolchainContext; - match override_tc_ctx { - Some(v) => { - tc_ctx = v; - } - None => { - tc_ctx = get_toolchain_context(&p_ctx); - } - } - // build our jar - (p_ctx, tc_ctx) = build(Some(p_ctx), Some(tc_ctx))?; + (p_ctx, tc_ctx) = build(p_ctx, tc_ctx)?; // run it - print_general("Running 'artifact.jar'"); + print_general("-- RUNNING ARTIFACT -----"); match run_jar(&p_ctx, &tc_ctx) { Ok(_) => (), Err(e) => { @@ -51,28 +33,11 @@ pub fn run( * Service function for the `build` command */ pub fn build( - override_p_ctx: Option, - override_tc_ctx: Option, + p_ctx: ProjectContext, + tc_ctx: ToolchainContext, ) -> result::Result<(ProjectContext, ToolchainContext), Box> { - // handle an override project context - let p_ctx: ProjectContext; - match override_p_ctx { - Some(v) => p_ctx = v, - None => p_ctx = get_project_context()?, - } - - // handle an override toolchain context - let tc_ctx: ToolchainContext; - match override_tc_ctx { - Some(v) => { - tc_ctx = v; - } - None => { - tc_ctx = get_toolchain_context(&p_ctx); - } - } - // walk our src directory, find source files + print_general("-- DISCOVERING ----------"); let java_files = backend::toolchain::get_java_source_files(&p_ctx).unwrap(); print_general( format!( @@ -82,21 +47,23 @@ pub fn build( ) .as_str(), ); + print_general("------------------------"); + // compile the project to class files - print_general("Compiling"); + print_general("-- COMPILING ------------"); compile_project(java_files, &p_ctx, &tc_ctx); + print_general("------------------------"); // build our jar - print_general("Packaging"); + print_general("-- PACKAGING ------------"); match backend::toolchain::build_jar(&p_ctx, &tc_ctx) { Ok(_) => (), Err(e) => { - print_err(format!("Failed to package 'artifact.jar': {}", {e}).as_str()); + print_err(format!("Failed to build jar: {}", {e}).as_str()); } } - - print_general(" ^~~^ ...done!"); + print_general("------------------------"); // pass ownership back to the caller Ok((p_ctx, tc_ctx)) @@ -167,7 +134,7 @@ pub fn init() { match backend::project::initialize_source_tree(&p_ctx) { Ok(_) => (), Err(e) => { - print_err(format!("Failed to run 'artifact.jar': {}", {e}).as_str()); + print_err(format!("Failed to initialize source tree: {}", {e}).as_str()); } } print_general("Project created: Edit espresso.toml to check it out!"); diff --git a/src/main.rs b/src/main.rs index a4b670d..2183fb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,26 @@ +use backend::{ + context::{get_project_context, ProjectContext}, + toolchain::{get_toolchain_context, ToolchainContext}, +}; use clap::Command; use frontend::terminal::print_err; mod backend; mod frontend; mod util; +fn get_contexts() -> (ProjectContext, ToolchainContext) { + let p_ctx = match get_project_context() { + Ok(v) => v, + Err(e) => { + print_err("Failed to get project context"); + panic!("{}", e); + } + }; + let tc_ctx = get_toolchain_context(&p_ctx); + + (p_ctx, tc_ctx) +} + fn main() { let cmd = Command::new("Espresso") .bin_name("espresso") @@ -21,13 +38,21 @@ fn main() { match matches.subcommand_name() { Some("build") => { - frontend::service::build(None, None); + let (p_ctx, tc_ctx) = get_contexts(); + match frontend::service::build(p_ctx, tc_ctx) { + Ok(_) => (), + Err(e) => print_err(format!("Error occurred running command: {}", e).as_str()), + } } Some("init") => { frontend::service::init(); } Some("run") => { - frontend::service::run(None, None); + let (p_ctx, tc_ctx) = get_contexts(); + match frontend::service::run(p_ctx, tc_ctx) { + Ok(_) => (), + Err(e) => print_err(format!("Error occurred running command: {}", e).as_str()), + } } _ => print_err("Unknown subcommand"), }