Skip to content

Commit

Permalink
#2 lots of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 13, 2024
1 parent c2e1693 commit 7bb4132
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 66 deletions.
24 changes: 11 additions & 13 deletions src/backend/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ pub fn get_java_source_files(p_ctx: &ProjectContext) -> Result<Vec<String>, 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<String> = vec![];
for x in files.unwrap() {
Expand Down Expand Up @@ -129,29 +127,28 @@ 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")
.current_dir(p_ctx.absolute_paths.project.clone() + "/build")
.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(())


}

Expand All @@ -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<dyn error::Error>>{
// 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(())
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pub static INIT_CMD: Lazy<Command> = Lazy::new(|| {
pub static RUN_CMD: Lazy<Command> = Lazy::new(|| {
Command::new("run")
.about("Build & run your Java project")
.alias("i")
.alias("r")
});
67 changes: 17 additions & 50 deletions src/frontend/service/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,32 +12,14 @@ use super::terminal::print_general;
* Service function for the `run` command
*/
pub fn run(
override_p_ctx: Option<ProjectContext>,
override_tc_ctx: Option<ToolchainContext>,
mut p_ctx: ProjectContext,
mut tc_ctx: ToolchainContext,
) -> result::Result<(), Box<dyn error::Error>> {
// 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) => {
Expand All @@ -51,28 +33,11 @@ pub fn run(
* Service function for the `build` command
*/
pub fn build(
override_p_ctx: Option<ProjectContext>,
override_tc_ctx: Option<ToolchainContext>,
p_ctx: ProjectContext,
tc_ctx: ToolchainContext,
) -> result::Result<(ProjectContext, ToolchainContext), Box<dyn error::Error>> {
// 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!(
Expand All @@ -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))
Expand Down Expand Up @@ -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!");
Expand Down
29 changes: 27 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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"),
}
Expand Down

0 comments on commit 7bb4132

Please sign in to comment.