From 2f6d7699e25041bd51e7427f78c3fb77a5953d18 Mon Sep 17 00:00:00 2001 From: hlafaille Date: Wed, 13 Mar 2024 12:30:54 -0400 Subject: [PATCH] #4 merging initialize functions --- src/backend/project.rs | 50 +++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/backend/project.rs b/src/backend/project.rs index 2d11e14..c67ef41 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -1,8 +1,8 @@ use crate::util::pathutil; -use super::context::{AbsoltuePaths, ProjectContext}; +use super::{context::{get_project_context, AbsoltuePaths, ProjectContext}, lock}; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, fs, io, error}; +use std::{collections::HashMap, error, fs, io, result}; #[derive(Deserialize, Serialize, Debug)] pub struct Config { @@ -53,7 +53,7 @@ pub fn does_exist(ap: &AbsoltuePaths) -> bool { /** * Initialize the source tree */ -pub fn initialize_source_tree(p_ctx: &ProjectContext) -> io::Result<()>{ +fn initialize_source_tree(p_ctx: &ProjectContext) -> io::Result<()>{ // get the base backage (dot notation) and the base package path on the fs let base_package_path = p_ctx.dynamic_absolute_paths.base_package.clone(); let base_package = p_ctx.config.project.base_package.clone(); @@ -92,8 +92,7 @@ fn process_input(x: String, default: String) -> String { /** * Initialize a config */ -pub fn initialize_config(name: String, base_package: String, ap: &AbsoltuePaths) -> io::Result<()> { - +fn initialize_config(name: String, base_package: String, ap: &AbsoltuePaths) -> result::Result<(), Box> { // populate a base_config struct let base_config = Config { project: Project { @@ -108,12 +107,12 @@ 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"); + let toml_string = toml::to_string(&base_config)?; fs::write(ap.config.clone(), toml_string)?; + Ok(()) } - /// Ensure the project environment is properly setup /// /// # Arguments @@ -122,9 +121,44 @@ pub fn initialize_config(name: String, base_package: String, ap: &AbsoltuePaths) /// /// # Returns /// `io::Result`, propagated from `fs::create_dir` -pub fn ensure_environment(ap: &AbsoltuePaths, debug_mode: &bool) -> io::Result<()>{ +fn ensure_environment(ap: &AbsoltuePaths, debug_mode: &bool) -> io::Result<()>{ + // if we're in debug mode, ensure the debug dir exists if *debug_mode { fs::create_dir(&ap.project)? } + + // create the inner workings dir + std::fs::create_dir_all(&ap.inner_workings)?; + Ok(()) +} + +/// Initialize a new project. This function will: +/// * Ensure the environment structure +/// * Initialize a config (`./espresso.toml`) +/// * Initialize the source tree (`./src/java/the.base.package/Main.java`) +/// +/// # Arguments +/// * `name`: Name of the new project from the user +/// * `base_package`: Base package of the new project from the user +/// * `ap`: Reference to an `AbsolutePaths` struct +/// * `debug_mode`: Reference to a `bool` dictating if we're in debug mode or not +/// +/// # Returns +/// A `ProjectContext` struct +pub fn initialize(name: String, base_package: String, ap: &AbsoltuePaths, debug_mode: &bool) -> result::Result> { + // check if the project exists + if does_exist(&ap) { + return Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "An Espresso project already exists"))); + } + + // ensure our environment is correct + ensure_environment(ap, debug_mode)?; + + // initialize the config & source tree + initialize_config(name, base_package, ap)?; + lock::initialize_state_lockfile(ap)?; + let p_ctx = get_project_context()?; + initialize_source_tree(&p_ctx)?; + return Ok(p_ctx); } \ No newline at end of file