From f22e5ce49b1b8856c83dfa1b54ab0c0fa8347b27 Mon Sep 17 00:00:00 2001 From: hlafaille Date: Wed, 13 Mar 2024 21:29:20 -0400 Subject: [PATCH] #4 lots of changes i cant even count omgomgomg --- Cargo.toml | 4 +++- src/backend/dependency/manage.rs | 19 +++++++++++++++++++ src/backend/dependency/mod.rs | 2 ++ src/backend/dependency/resolve.rs | 4 ++++ src/backend/lock.rs | 13 +++++++------ src/backend/mod.rs | 3 ++- src/backend/project.rs | 17 +++++++++++++++-- src/frontend/command.rs | 6 ++++++ src/frontend/service/mod.rs | 9 +++++++++ src/main.rs | 17 +++++++++++++---- src/util/mod.rs | 1 + src/util/net.rs | 22 ++++++++++++++++++++++ 12 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 src/backend/dependency/manage.rs create mode 100644 src/backend/dependency/resolve.rs create mode 100644 src/util/net.rs diff --git a/Cargo.toml b/Cargo.toml index 1e2bace..f9eeb4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,9 @@ edition = "2021" [dependencies] toml = "0.8.10" serde = {version = "1.0.197", features = ["derive"]} -clap = { version = "3.2.20", features = ["cargo"] } +clap = { version = "4.5.2", features = ["cargo"] } colored = "2.1.0" once_cell = "1.19.0" walkdir = "2.5.0" +reqwest = "0.11.26" +tokio = { version = "1.36.0", features = ["full"] } diff --git a/src/backend/dependency/manage.rs b/src/backend/dependency/manage.rs new file mode 100644 index 0000000..1264e45 --- /dev/null +++ b/src/backend/dependency/manage.rs @@ -0,0 +1,19 @@ +use std::{collections, result}; + +use clap::error; + +use crate::backend::context::ProjectContext; + +/// Add a .jar from the filesystem to the dependencies. This will update the `espreso.toml` file. +/// +/// # Arguments +/// * `path`: Reference to a string of the filesystem path to the .jar. For example, `/home/user/Downloads/artifact.jar`. +/// * `name`: Reference to a string of the name of the dependency. For example, `lombok`. +/// * `version`: Reference to a string of the version of the dependency. For example, `1.0.0`. +/// * `p_ctx`: Reference to a `ProjectContext` struct +/// +/// # Returns +/// Propagated `error:Error`(s) +pub fn add(path: &String, name: &String, version: &String, p_ctx: &ProjectContext) -> result::Result<(), Box> { + +} diff --git a/src/backend/dependency/mod.rs b/src/backend/dependency/mod.rs index e69de29..044c3c4 100644 --- a/src/backend/dependency/mod.rs +++ b/src/backend/dependency/mod.rs @@ -0,0 +1,2 @@ +pub mod manage; +pub mod resolve; \ No newline at end of file diff --git a/src/backend/dependency/resolve.rs b/src/backend/dependency/resolve.rs new file mode 100644 index 0000000..325418e --- /dev/null +++ b/src/backend/dependency/resolve.rs @@ -0,0 +1,4 @@ +/// Represents a resolved dependency +pub struct ResolvedDependency { + +} \ No newline at end of file diff --git a/src/backend/lock.rs b/src/backend/lock.rs index 37ce15b..65afc12 100644 --- a/src/backend/lock.rs +++ b/src/backend/lock.rs @@ -1,4 +1,4 @@ -use std::{collections::{self, HashMap}, error, fs, hash, result}; +use std::{collections::{self, HashMap}, error, fs, result}; use serde::{Deserialize, Serialize}; @@ -6,16 +6,17 @@ use super::context::AbsoltuePaths; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct StateLockFile { - pub dependencies: collections::HashMap, + pub dependencies: collections::HashMap, } -/** - * Represents a dependency in '.espresso/dependencies'. - */ +/// Represents a dependency in the state lock file #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Dependency { +pub struct StateLockFileDependency { + /// The dependency name pub name: String, + /// The dependency filesystem name pub fs_name: String, + /// The dependency sha512 checksum pub checksum: String, } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7b807a8..fdc9b09 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,4 +1,5 @@ pub mod context; pub mod project; pub mod toolchain; -pub mod lock; \ No newline at end of file +pub mod lock; +pub mod dependency; \ No newline at end of file diff --git a/src/backend/project.rs b/src/backend/project.rs index c67ef41..823ec27 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -106,10 +106,23 @@ fn initialize_config(name: String, base_package: String, ap: &AbsoltuePaths) -> dependencies: HashMap::new(), }; + write_config(&base_config, ap); + + Ok(()) +} + +/// Write a `Config` to the filesystem. +/// +/// # Arguments +/// * `config`: Reference to a `Config` struct, whos contents will be written to the filesystem. +/// * `ap`: Reference to a `AbsolutePaths` struct. +/// +/// # Returns +/// Propagated `error:Error`(s) +pub fn write_config(config: &Config, ap: &AbsoltuePaths) -> result::Result<(), Box>{ // write it to a toml string, then write it to the config file - let toml_string = toml::to_string(&base_config)?; + let toml_string = toml::to_string(&config)?; fs::write(ap.config.clone(), toml_string)?; - Ok(()) } diff --git a/src/frontend/command.rs b/src/frontend/command.rs index 713927b..37bfbce 100644 --- a/src/frontend/command.rs +++ b/src/frontend/command.rs @@ -18,3 +18,9 @@ pub static RUN_CMD: Lazy = Lazy::new(|| { .about("Build & run your Java project") .alias("r") }); + +pub static ADD_CMD: Lazy = Lazy::new(|| { + Command::new("add") + .about("Add a dependency from Maven Repository to your project") + .alias("a") +}); diff --git a/src/frontend/service/mod.rs b/src/frontend/service/mod.rs index 8ab0b85..3c8cac6 100644 --- a/src/frontend/service/mod.rs +++ b/src/frontend/service/mod.rs @@ -1,4 +1,5 @@ use crate::backend::context::{AbsoltuePaths, ProjectContext}; +use crate::backend::project::Project; use crate::backend::toolchain::{ compile_project, run_jar, ToolchainContext, }; @@ -108,3 +109,11 @@ pub fn init() { } print_general("Project created: Edit espresso.toml to check it out!"); } + +/// Service function for the `add` command. +pub fn add(p_ctx: ProjectContext, tc_ctx: ToolchainContext) -> result::Result<(ProjectContext, ToolchainContext), Box> { + // + + // pass ownership back + Ok((p_ctx, tc_ctx)) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b60309d..408362e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,15 +21,17 @@ fn get_contexts() -> (ProjectContext, ToolchainContext) { (p_ctx, tc_ctx) } -fn main() { +#[tokio::main] +async fn main() { let cmd = Command::new("Espresso") .bin_name("espresso") .version("1.0.0") .about("Build Java apps without the fuss of antiquated build tools. Drink some Espresso.") .subcommand_required(true) - .subcommand((&*frontend::command::BUILD_CMD).clone()) - .subcommand((&*frontend::command::INIT_CMD).clone()) - .subcommand((&*frontend::command::RUN_CMD).clone()); + .subcommand((frontend::command::BUILD_CMD).clone()) + .subcommand((frontend::command::INIT_CMD).clone()) + .subcommand((frontend::command::RUN_CMD).clone()) + .subcommand((frontend::command::ADD_CMD).clone()); let matches = cmd.get_matches(); @@ -54,6 +56,13 @@ fn main() { Err(e) => print_err(format!("Error occurred running command: {}", e).as_str()), } } + Some("add") => { + let (p_ctx, tc_ctx) = get_contexts(); + match frontend::service::add(p_ctx, tc_ctx) { + Ok(_) => (), + Err(e) => print_err(format!("Error occurred running command: {}", e).as_str()), + } + } _ => print_err("Unknown subcommand"), } } diff --git a/src/util/mod.rs b/src/util/mod.rs index ed7f0cc..4fb8004 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,2 +1,3 @@ pub mod directory; pub mod pathutil; +pub mod net; diff --git a/src/util/net.rs b/src/util/net.rs new file mode 100644 index 0000000..1ad4ace --- /dev/null +++ b/src/util/net.rs @@ -0,0 +1,22 @@ +use std::error; + +use tokio::{fs::File, io::AsyncWriteExt}; + +/// Download a file from the internet. +/// +/// # Arguments +/// * `url`: URL of the file +/// * `path`: Filesystem path to the desired location +/// +/// # Returns +/// Propagates any errors +pub async fn download_file(url: &String, path: &String) -> Result<(), Box> { + let response = reqwest::get(url).await?; + + if response.status().is_success() { + let body = response.bytes().await?; + let mut file = File::create(path).await?; + file.write_all(&body).await?; + } + Ok(()) +} \ No newline at end of file