-
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.
- Loading branch information
Showing
12 changed files
with
210 additions
and
143 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,121 @@ | ||
use std::{cell::Cell, env}; | ||
|
||
use crate::backend::project::Config; | ||
|
||
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: Cell<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 the Project Context | ||
*/ | ||
/// Get an AbsolutePaths struct | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `config`: Reference to a Config | ||
/// * `debug_mode`: Reference to a bool if we're in debug mode | ||
/// | ||
/// # Returns | ||
/// | ||
/// AbsolutePaths | ||
fn get_absolute_paths(debug_mode: &bool) -> AbsoltuePaths { | ||
let cwd = env::current_dir() | ||
.expect("Failed to read the current working directory; are you in a shell?") | ||
.to_string_lossy() | ||
.into_owned(); | ||
AbsoltuePaths { | ||
project: cwd.clone(), | ||
source: cwd.clone() + "/src", | ||
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 | ||
fn get_dynamic_absolute_paths( | ||
ap: &AbsoltuePaths, | ||
config: &Config, | ||
debug_mode: &bool, | ||
) -> DynamicAbsolutePaths { | ||
let base_package = Cell::new( | ||
ap.source.clone() | ||
+ config | ||
.project | ||
.base_package | ||
.clone() | ||
.replace(".", "/") | ||
.as_str(), | ||
); | ||
DynamicAbsolutePaths { base_package } | ||
} | ||
|
||
/// 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, &debug_mode); | ||
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
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
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,21 +1,20 @@ | ||
|
||
use clap::{Command}; | ||
use clap::Command; | ||
use once_cell::sync::Lazy; | ||
|
||
pub static BUILD_CMD: Lazy<Command> = Lazy::new(|| { | ||
Command::new("build") | ||
.about("Build your Java project into a standalone .jar") | ||
.alias("b") | ||
.about("Build your Java project into a standalone .jar") | ||
.alias("b") | ||
}); | ||
|
||
pub static INIT_CMD: Lazy<Command> = Lazy::new(|| { | ||
Command::new("init") | ||
.about("Initialize a new Espresso project") | ||
.alias("i") | ||
.about("Initialize a new Espresso project") | ||
.alias("i") | ||
}); | ||
|
||
pub static RUN_CMD: Lazy<Command> = Lazy::new(|| { | ||
Command::new("run") | ||
.about("Build & run your Java project") | ||
.alias("i") | ||
}); | ||
.about("Build & run your Java project") | ||
.alias("i") | ||
}); |
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 command; | ||
pub mod service; | ||
pub mod terminal; | ||
pub mod terminal; |
Oops, something went wrong.