Skip to content

Commit

Permalink
#4 Refactor file paths and use path::PathBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 27, 2024
1 parent c17a9bd commit 6601b14
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
36 changes: 18 additions & 18 deletions src/backend/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, error, io, result};
use std::{env, error, io, path, result};

use super::{lock::{self, StateLockFile}, project::get_config_from_fs, Config};

Expand All @@ -19,27 +19,27 @@ pub struct ProjectContext {
/// Contains absolute paths to critical resources within the currently loaded project.
pub struct AbsoltuePaths {
/// Path to the currently loaded projects directory. Should be the current working directory.
pub project: String,
pub project: path::PathBuf,
/// Path to the src/ directory within the currently loaded project.
pub source: String,
pub source: path::PathBuf,
/// Path to the config file within the currently loaded project.
pub config: String,
pub config: path::PathBuf,
/// Path to the directory that contains inner working files (ex: state_lockfile, dependency jars, etc)
pub inner_workings: String,
pub inner_workings: path::PathBuf,
/// Path to the inner workings directory containing downloaded dependencies.
pub dependencies: String,
pub dependencies: path::PathBuf,
/// Path to the inner workings state lockfile within the currently loaded project.
pub state_lockfile: String,
pub state_lockfile: path::PathBuf,
/// Path to the inner workings directory containing extracted dependencies.
pub dependencies_extracted: String,
pub dependencies_extracted: path::PathBuf,
/// Path to the directory containing built .class files and potentially an artifact.jar
pub build: String,
pub build: path::PathBuf,
}

/// 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: String,
pub base_package: path::PathBuf,
}

/// Get if debug mode is active. You can enable debug mode by setting the `ESPRESSO_DEBUG`
Expand Down Expand Up @@ -82,14 +82,14 @@ pub fn get_absolute_paths(debug_mode: &bool) -> io::Result<AbsoltuePaths> {
}

Ok(AbsoltuePaths {
project: cwd_string.clone(),
source: cwd_string.clone() + "/src/java",
config: cwd_string.clone() + "/espresso.toml",
inner_workings: cwd_string.clone() + "/.espresso",
dependencies: cwd_string.clone() + "/.espresso/dependencies",
state_lockfile: cwd_string.clone() + "/.espresso/state.lock.toml",
dependencies_extracted: cwd_string.clone() + "/.espresso/dependencies_extracted",
build: cwd_string.clone() + "/build"
project: cwd_string.clone().into(),
source: (cwd_string.clone() + "/src/java").into(),
config: (cwd_string.clone() + "/espresso.toml").into(),
inner_workings: (cwd_string.clone() + "/.espresso").into(),
dependencies: (cwd_string.clone() + "/.espresso/dependencies").into(),
state_lockfile: (cwd_string.clone() + "/.espresso/state.lock.toml").into(),
dependencies_extracted: (cwd_string.clone() + "/.espresso/dependencies_extracted").into(),
build: (cwd_string.clone() + "/build").into()
})
}

Expand Down
20 changes: 17 additions & 3 deletions src/backend/dependency/class.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error, result};
use std::{error, result, vec};

use crate::{backend::{context::ProjectContext, lock::StateLockFileDependency}, util};

Expand All @@ -10,6 +10,8 @@ pub struct ExtractedClass {
pub containing_directory_relative_path: String,
// The class name (minus extension)
pub name: String,
// The absolute path to the class file, pre merge.
pub path: String,
}

/// Get extracted classes for a particular dependency
Expand All @@ -31,8 +33,20 @@ pub fn get(p_ctx: &ProjectContext, dependency: &StateLockFileDependency) -> resu
class_files.push(file);
}
}


// build the extracted class structs
let extracted_classes: Vec<ExtractedClass> = vec![];
for file in class_files {
let absolute_path: Vec<&str> = file.split("/").collect();

let containing_directory_absolute_path = "";

extracted_classes.push(
ExtractedClass {
containing_directory_absolute_path
}
)
}

Ok(())
Ok(extracted_classes)
}
26 changes: 26 additions & 0 deletions src/util/pathutil.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::panic;
use std::{error, path::Path, result};
use tokio::fs;
use sha2::{Digest, Sha512};
Expand Down Expand Up @@ -52,3 +53,28 @@ pub async fn ensure_integrity_sha512(
}
Ok(())
}

/// Get the parent directory of a file. For example, with `/home/x/EspressoProjects/project/espresso.toml` as
/// the full path, `/home/x/EspressoProjects/project` would be the parent path.
///
/// # Arguments
/// * `file_path`: A reference to a `String`, containing the path to the file.
///
/// # Returns
/// `String`, the parent path.
///
#[deprecated(note = "Use std::path::Path::parent() instead")]
pub fn get_parent_path_of_file(file_path: &String) -> result::Result<String, Box<dyn error::Error>> {
let split_path: Vec<&str> = file_path.split("/").collect();
let mut parent_path: String = String::new();
if let Some(slice) = split_path.get(0..split_path.len() - 1) {
for part in slice {
parent_path.push_str(part);
parent_path.push_str("/")
}
} else {
return Err(EspressoError::nib(""));
}
Ok(parent_path)

}

0 comments on commit 6601b14

Please sign in to comment.