Skip to content

Commit

Permalink
#4 fixing some broken borrows
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 12, 2024
1 parent cf7c976 commit f2747a2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
34 changes: 18 additions & 16 deletions src/backend/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{cell::Cell, env};
use std::{
cell::{Cell, RefCell},
env,
};

use crate::backend::project::Config;

Expand Down Expand Up @@ -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<String>,
pub base_package: String,
}

/// Get if debug mode is active. You can enable debug mode by setting the `ESPRESSO_DEBUG`
Expand Down Expand Up @@ -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",
}
}
Expand All @@ -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 }
}

Expand All @@ -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,
Expand Down
27 changes: 10 additions & 17 deletions src/backend/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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????)
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
20 changes: 12 additions & 8 deletions src/backend/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>, 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<String> = vec![];
for x in files.unwrap() {
Expand Down Expand Up @@ -95,8 +99,8 @@ pub fn compile_project(java_files: Vec<String>, 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());
Expand Down Expand Up @@ -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),
)
}
Expand All @@ -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) => {
Expand All @@ -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()
Expand All @@ -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();
Expand Down
10 changes: 7 additions & 3 deletions src/frontend/service/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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",
);
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use backend::project::ensure_debug_directory_exists_if_debug;
use clap::Command;
use frontend::terminal::print_err;
mod backend;
Expand All @@ -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") => {
Expand Down

0 comments on commit f2747a2

Please sign in to comment.