From f2747a2feb40c0ee6fbc76145242a811fef92f7c Mon Sep 17 00:00:00 2001 From: hlafaille Date: Tue, 12 Mar 2024 16:25:10 -0400 Subject: [PATCH] #4 fixing some broken borrows --- src/backend/context.rs | 34 ++++++++++++++++++---------------- src/backend/project.rs | 27 ++++++++++----------------- src/backend/toolchain.rs | 20 ++++++++++++-------- src/frontend/service/mod.rs | 10 +++++++--- src/main.rs | 3 +-- 5 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/backend/context.rs b/src/backend/context.rs index c0dbd50..e42164c 100644 --- a/src/backend/context.rs +++ b/src/backend/context.rs @@ -1,4 +1,7 @@ -use std::{cell::Cell, env}; +use std::{ + cell::{Cell, RefCell}, + env, +}; use crate::backend::project::Config; @@ -29,7 +32,7 @@ pub struct AbsoltuePaths { /// Contains absolute paths to critical resources within the currently loaded project. Determined at runtime. pub struct DynamicAbsolutePaths { /// Path to the base package. Should be {source}/package/path/here. The Main.java file will live here. - pub base_package: Cell, + pub base_package: String, } /// Get if debug mode is active. You can enable debug mode by setting the `ESPRESSO_DEBUG` @@ -63,14 +66,19 @@ pub fn get_debug_mode() -> bool { /// # Returns /// /// AbsolutePaths -fn get_absolute_paths(debug_mode: &bool) -> AbsoltuePaths { - let cwd = env::current_dir() +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(); + + if *debug_mode { + cwd += "/espresso_debug" + } + AbsoltuePaths { project: cwd.clone(), - source: cwd.clone() + "/src", + source: cwd.clone() + "/src/java", config: cwd.clone() + "/espresso.toml", } } @@ -85,20 +93,14 @@ fn get_absolute_paths(debug_mode: &bool) -> AbsoltuePaths { /// # Returns /// /// DynamicAbsolutePaths -fn get_dynamic_absolute_paths( - ap: &AbsoltuePaths, - config: &Config, - debug_mode: &bool, -) -> DynamicAbsolutePaths { - let base_package = Cell::new( - ap.source.clone() - + config +pub fn get_dynamic_absolute_paths(ap: &AbsoltuePaths, config: &Config) -> DynamicAbsolutePaths { + let base_package = ap.source.clone() + + "/" + config .project .base_package .clone() .replace(".", "/") - .as_str(), - ); + .as_str(); DynamicAbsolutePaths { base_package } } @@ -111,7 +113,7 @@ pub fn get_project_context() -> ProjectContext { let debug_mode = get_debug_mode(); 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, &debug_mode); + let dynamic_absolute_paths = get_dynamic_absolute_paths(&absolute_paths, &config); ProjectContext { config, absolute_paths, diff --git a/src/backend/project.rs b/src/backend/project.rs index 19edc74..0587b3c 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -28,25 +28,15 @@ pub struct Toolchain { pub path: String, } -/** - * Ensure development directory exists - */ -pub fn ensure_debug_directory_exists_if_debug(p_ctx: &ProjectContext) { - if p_ctx.debug_mode { - if !Path::exists(Path::new("espresso_debug")) { - create_dir_all("espresso_debug").expect("Failed to ensure debug directory exists"); - } - } -} - /** * 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).expect("Unable to read conig file"); + let contents = fs::read_to_string(ap.config.clone()).expect("Unable to read conig file"); toml::from_str(&contents).unwrap() } + /** * If a project exists. A project is deemed existing if it has a source directory * and a config file. @@ -68,8 +58,8 @@ pub fn does_exist(ap: &AbsoltuePaths) -> bool { * Initialize the source tree */ pub fn initialize_source_tree(p_ctx: &ProjectContext) { - let base_package_path = p_ctx.dynamic_absolute_paths.base_package.take(); - std::fs::create_dir_all(base_package_path) + let base_package_path = p_ctx.dynamic_absolute_paths.base_package.clone(); + std::fs::create_dir_all(&base_package_path) .expect("failed to create main package directories in file system"); // create the Main.java file (textwrap doesn't work????) @@ -81,9 +71,12 @@ public class Main { System.out.println("Hello, world!"); } }"# - .replace("${BASE_PACKAGE}", &p_ctx.config.project.base_package); + .replace("${BASE_PACKAGE}", &base_package_path); - std::fs::write(base_package_path + "/Main.java", base_java_content); + std::fs::write( + base_package_path.clone() + "/Main.java", + base_java_content, + ); } fn process_input(x: String, default: String) -> String { @@ -114,5 +107,5 @@ pub fn initialize_config(name: String, base_package: String, ap: &AbsoltuePaths) // write it to a toml string, then write it to the config file let toml_string = toml::to_string(&base_config).expect("Failed to serialize struct"); - fs::write(ap.config, toml_string).expect("Failed to write config file") + fs::write(ap.config.clone(), toml_string).expect("Failed to write config file") } diff --git a/src/backend/toolchain.rs b/src/backend/toolchain.rs index 27b414a..0305a98 100644 --- a/src/backend/toolchain.rs +++ b/src/backend/toolchain.rs @@ -55,9 +55,13 @@ pub fn get_toolchain_context(p_ctx: &ProjectContext) -> ToolchainContext { * Get a list of source files */ pub fn get_java_source_files(p_ctx: &ProjectContext) -> Result, std::io::Error> { - let base_package = project::get_full_base_package_path(&p_ctx); + // + let base_package = p_ctx.dynamic_absolute_paths.base_package.clone(); + 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() { @@ -95,8 +99,8 @@ pub fn compile_project(java_files: Vec, p_ctx: &ProjectContext, tc_ctx: "{} {} -d {}/build -cp {}/build", &compiler_path, file, - get_absolute_project_path(), - get_absolute_project_path() + &p_ctx.absolute_paths.project, + &p_ctx.absolute_paths.project ); print_debug(format!("Running '{}'", cmd).as_str()); @@ -131,7 +135,7 @@ fn get_manifest(p_ctx: &ProjectContext) -> String { */ pub fn write_manifest(p_ctx: &ProjectContext) -> io::Result<()> { std::fs::write( - get_absolute_project_path() + "/build/MANIFEST.MF", + p_ctx.absolute_paths.project.clone() + "/build/MANIFEST.MF", get_manifest(p_ctx), ) } @@ -147,7 +151,7 @@ pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) { let relative_base_package_path = p_ctx.config.project.base_package.clone().replace(".", "/"); // remove the old jar - let remove_artifact_res = fs::remove_file(get_absolute_project_path() + "/build/artifact.jar"); + let remove_artifact_res = fs::remove_file(p_ctx.absolute_paths.project.clone() + "/build/artifact.jar"); match remove_artifact_res { Ok(_) => (), Err(e) => { @@ -171,7 +175,7 @@ pub fn build_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) { // run the command let output = Command::new("sh") - .current_dir(get_absolute_project_path() + "/build") + .current_dir(p_ctx.absolute_paths.project.clone() + "/build") .arg("-c") .arg(cmd) .output() @@ -189,12 +193,12 @@ pub fn run_jar(p_ctx: &ProjectContext, tc_ctx: &ToolchainContext) { // build our packager command let cmd = format!( "java -jar {}", - project::get_absolute_project_path() + "/build/artifact.jar" + p_ctx.absolute_paths.project.clone() + "/build/artifact.jar" ); // run the command let status = Command::new("sh") - .current_dir(get_absolute_project_path() + "/build") + .current_dir(p_ctx.absolute_paths.project.clone() + "/build") .arg("-c") .arg(cmd) .status(); diff --git a/src/frontend/service/mod.rs b/src/frontend/service/mod.rs index a5c0307..4cd4cfc 100644 --- a/src/frontend/service/mod.rs +++ b/src/frontend/service/mod.rs @@ -1,11 +1,12 @@ use std::io; -use crate::backend; +use crate::backend::{self, context, project}; use crate::backend::context::{get_project_context, ProjectContext}; use crate::backend::toolchain::{ compile_project, get_toolchain_context, run_jar, ToolchainContext, }; use crate::frontend::terminal::{print_err, print_sameline}; +use crate::util::pathutil; use super::terminal::print_general; @@ -93,8 +94,11 @@ pub fn build( * Service function for the `init` command */ pub fn init() { + // get absolute paths + let ap = context::get_absolute_paths(&context::get_debug_mode()); + // check if the project exists - if backend::project::does_exist() { + if project::does_exist(&ap){ print_err( "Unable to initialize project: An Espresso project (or remnants of one) already exist", ); @@ -117,7 +121,7 @@ pub fn init() { } // initialize the config - backend::project::initialize_config(name, base_package); + backend::project::initialize_config(name, base_package, &ap); // get our project context let p_ctx = backend::context::get_project_context(); diff --git a/src/main.rs b/src/main.rs index 3a1c51f..a4b670d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use backend::project::ensure_debug_directory_exists_if_debug; use clap::Command; use frontend::terminal::print_err; mod backend; @@ -18,7 +17,7 @@ fn main() { let matches = cmd.get_matches(); // ensure the espresso_debug directory exists if ESPRESSO_DEBUG=1 - ensure_debug_directory_exists_if_debug(); + // ensure_debug_directory_exists_if_debug(); match matches.subcommand_name() { Some("build") => {