-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from hlafaille/feat/3
#3 Rework Contexts
- Loading branch information
Showing
12 changed files
with
255 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,124 @@ | ||
use crate::backend::project::Config; | ||
use std::{ | ||
cell::{Cell, RefCell}, | ||
env, fs, io, | ||
}; | ||
|
||
use crate::{backend::project::Config, frontend::terminal::print_err}; | ||
|
||
use super::project::get_config_from_fs; | ||
|
||
/** | ||
* Represents the currently loaded project | ||
*/ | ||
/// Represents the context of the currently loaded project. | ||
pub struct ProjectContext { | ||
/// Project config (espresso.toml) | ||
pub config: Config, | ||
/// Absolute paths (with suffixes known at compile time) that're relavent to this project (ex: path to src) | ||
pub absolute_paths: AbsoltuePaths, | ||
/// Absolute paths (with suffixes NOT known at compile time) that're relavent to this project (ex: path to base package) | ||
pub dynamic_absolute_paths: DynamicAbsolutePaths, | ||
/// If we're running in debug mode (ESPRESSO_DEBUG=1) | ||
pub debug_mode: bool, | ||
} | ||
|
||
/// 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, | ||
/// Path to the src/ directory within the currently loaded project. | ||
pub source: String, | ||
/// Path to the config file within the currently loaded project. | ||
pub config: String, | ||
} | ||
|
||
/// 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, | ||
} | ||
|
||
/// Get if debug mode is active. You can enable debug mode by setting the `ESPRESSO_DEBUG` | ||
/// environment variable to `1`. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `true` if `ESPRESSO_DEBUG=1`, `false` if `ESPRESSO_DEBUG=0` or not set | ||
pub fn get_debug_mode() -> bool { | ||
match env::var("ESPRESSO_DEBUG") { | ||
Ok(v) => { | ||
if v == "1" { | ||
return true; | ||
} else if v == "0" { | ||
return false; | ||
} else { | ||
return false; | ||
} | ||
} | ||
Err(_) => return false, | ||
}; | ||
} | ||
|
||
/// Get an AbsolutePaths struct | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `config`: Reference to a Config | ||
/// * `debug_mode`: Reference to a bool if we're in debug mode | ||
/// | ||
/// # Returns | ||
/// | ||
/// AbsolutePaths | ||
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/java", | ||
config: cwd.clone() + "/espresso.toml", | ||
} | ||
} | ||
|
||
/// Get a DynamicAbsolutePaths struct. | ||
/// | ||
/// # Arguments | ||
/// * `ap`: Reference to an `AbsolutePaths` struct. Used to get the `src/` directory. | ||
/// * `config`: Reference to a `Config` struct. used to get the current `base_package`. | ||
/// * `debug_mode`: Reference to a bool if we're in debug mode | ||
/// | ||
/// # Returns | ||
/// | ||
/// DynamicAbsolutePaths | ||
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(); | ||
DynamicAbsolutePaths { base_package } | ||
} | ||
|
||
/** | ||
* Get the Project Context | ||
*/ | ||
/// Get context about the currently loaded project. | ||
/// | ||
/// # Returns | ||
/// | ||
/// ProjectContext | ||
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); | ||
|
||
ProjectContext { | ||
config: get_config_from_fs() | ||
config, | ||
absolute_paths, | ||
debug_mode, | ||
dynamic_absolute_paths, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub mod context; | ||
pub mod project; | ||
pub mod toolchain; | ||
pub mod context; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.