Skip to content

Commit

Permalink
#4 merging initialize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 13, 2024
1 parent f805109 commit 2f6d769
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/backend/project.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<dyn error::Error>> {
// populate a base_config struct
let base_config = Config {
project: Project {
Expand All @@ -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
Expand All @@ -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<ProjectContext, Box<dyn error::Error>> {
// 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);
}

0 comments on commit 2f6d769

Please sign in to comment.