Skip to content

Commit

Permalink
Merge pull request #8 from hlafaille/feat/2
Browse files Browse the repository at this point in the history
Feat/2
  • Loading branch information
hlafaille authored Mar 13, 2024
2 parents 41652dd + 7bb4132 commit b51a353
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 151 deletions.
37 changes: 16 additions & 21 deletions src/backend/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
cell::{Cell, RefCell},
env, fs, io,
};
use std::{env, error, io, result};

use crate::{backend::project::Config, frontend::terminal::print_err};
use crate::backend::project::Config;

use super::project::get_config_from_fs;

Expand Down Expand Up @@ -66,21 +63,19 @@ pub fn get_debug_mode() -> bool {
/// # Returns
///
/// AbsolutePaths
pub fn get_absolute_paths(debug_mode: &bool) -> AbsoltuePaths {
let mut cwd = env::current_dir()
.expect("Failed to read the current working directory; are you in a shell?")
.to_string_lossy()
.into_owned();
pub fn get_absolute_paths(debug_mode: &bool) -> io::Result<AbsoltuePaths> {
let cwd = env::current_dir()?;
let mut cwd_string: String = cwd.to_string_lossy().into_owned();

if *debug_mode {
cwd += "/espresso_debug"
cwd_string += "/espresso_debug";
}

AbsoltuePaths {
project: cwd.clone(),
source: cwd.clone() + "/src/java",
config: cwd.clone() + "/espresso.toml",
}
Ok(AbsoltuePaths {
project: cwd_string.clone(),
source: cwd_string.clone() + "/src/java",
config: cwd_string.clone() + "/espresso.toml",
})
}

/// Get a DynamicAbsolutePaths struct.
Expand Down Expand Up @@ -109,16 +104,16 @@ pub fn get_dynamic_absolute_paths(ap: &AbsoltuePaths, config: &Config) -> Dynami
/// # Returns
///
/// ProjectContext
pub fn get_project_context() -> ProjectContext {
pub fn get_project_context() -> result::Result<ProjectContext, Box<dyn error::Error>> {
let debug_mode = get_debug_mode();
let absolute_paths = get_absolute_paths(&debug_mode);
let config = get_config_from_fs(&absolute_paths);
let absolute_paths = get_absolute_paths(&debug_mode)?;
let config = get_config_from_fs(&absolute_paths)?;
let dynamic_absolute_paths = get_dynamic_absolute_paths(&absolute_paths, &config);

ProjectContext {
Ok(ProjectContext {
config,
absolute_paths,
debug_mode,
dynamic_absolute_paths,
}
})
}
9 changes: 5 additions & 4 deletions src/backend/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::util::pathutil;

use super::context::{AbsoltuePaths, ProjectContext};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, io};
use std::{collections::HashMap, fs, io, error};

#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
Expand All @@ -26,9 +26,10 @@ pub struct Toolchain {
/**
* Load the project at the current working directory
*/
pub fn get_config_from_fs(ap: &AbsoltuePaths) -> Config {
let contents = fs::read_to_string(ap.config.clone()).expect("Unable to read conig file");
toml::from_str(&contents).unwrap()
pub fn get_config_from_fs(ap: &AbsoltuePaths) -> Result<Config, Box<dyn error::Error>>{
let contents = fs::read_to_string(ap.config.clone())?;
let x: Config = toml::from_str(&contents)?;
Ok(x)
}


Expand Down
88 changes: 26 additions & 62 deletions src/backend/toolchain.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use crate::{
frontend::terminal::{print_debug, print_err, print_general},
frontend::terminal::{print_debug, print_err},
util,
};
use std::{
borrow::Cow,
env, fs, io, path,
process::{Command, ExitStatus},
vec,
};
use walkdir::WalkDir;
use std::error;
use std::{borrow::Cow, env, fs, io, path, process::Command, result, vec};

use super::context::ProjectContext;

Expand Down Expand Up @@ -59,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 All @@ -71,21 +64,6 @@ pub fn get_java_source_files(p_ctx: &ProjectContext) -> Result<Vec<String>, std:
return Ok(java_files);
}

/**
* Compile a Java file using the defined toolchain
*/
pub fn compile_java_file(path: &String, tc_ctx: &ToolchainContext) {
Command::new("sh")
.arg("-c")
.arg(format!(
"{} {}",
&tc_ctx.compiler_path.to_string_lossy(),
path
))
.output()
.expect("failed to execute java compiler");
}

/**
* Compile all Java files under a project
*/
Expand All @@ -96,10 +74,7 @@ pub fn compile_project(java_files: Vec<String>, p_ctx: &ProjectContext, tc_ctx:
// build our compiler string
let cmd = format!(
"{} {} -d {}/build -cp {}/build",
&compiler_path,
file,
&p_ctx.absolute_paths.project,
&p_ctx.absolute_paths.project
&compiler_path, file, &p_ctx.absolute_paths.project, &p_ctx.absolute_paths.project
);

print_debug(format!("Running '{}'", cmd).as_str());
Expand Down Expand Up @@ -141,57 +116,50 @@ pub fn write_manifest(p_ctx: &ProjectContext) -> io::Result<()> {

/**
* Build our JAR file
*
* TODO: ensure we're using the proper toolchain
*/
pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) {
pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) -> result::Result<(), Box<dyn error::Error>>{
// write our manifest
write_manifest(p_ctx).unwrap();

// convert our base package (ex: com.xyz.whatever) to its filesystem equivalent (com/xyz/whatever)
let relative_base_package_path = p_ctx.config.project.base_package.clone().replace(".", "/");

// remove the old jar
let remove_artifact_res = fs::remove_file(p_ctx.absolute_paths.project.clone() + "/build/artifact.jar");
match remove_artifact_res {
Ok(_) => (),
Err(e) => {
if e.raw_os_error().unwrap() != 2 {
print_err(
format!(
"Unable to cleanup 'artifact.jar': {}",
e.to_string().as_str()
)
.as_str(),
);
}
}
}
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()
.expect("failed to run jar packager");
.output()?;
if !output.status.success() {
println!("{}", String::from_utf8(output.stderr).unwrap());
print_err("java packager (jar) exited with error(s)");
let process_err = String::from_utf8(output.stderr)?;
Err(format!("Failed to package jar: Packager output was: {}", process_err))?
}

Ok(())

}

/**
* Run our JAR file
*/
pub fn run_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) {
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"
);

Expand All @@ -200,16 +168,12 @@ pub fn run_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) {
.current_dir(p_ctx.absolute_paths.project.clone() + "/build")
.arg("-c")
.arg(cmd)
.status();
.status()?;

match status {
Ok(v) => {
if !v.success() {
print_err("java virtual machine (java) exited with error(s)");
}
}
Err(e) => {
print_err("unable to execute java virtual machine command");
}
// dela with our output. We wa
if !status.success() {
Err(format!("Java process exited with non-0 status code"))?
}

Ok(())
}
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")
});
Loading

0 comments on commit b51a353

Please sign in to comment.