Skip to content

Commit

Permalink
#4 lots of changes i cant even count omgomgomg
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 14, 2024
1 parent bc56518 commit f22e5ce
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
19 changes: 19 additions & 0 deletions src/backend/dependency/manage.rs
Original file line number Diff line number Diff line change
@@ -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<dyn error::Error>> {

}
2 changes: 2 additions & 0 deletions src/backend/dependency/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod manage;
pub mod resolve;
4 changes: 4 additions & 0 deletions src/backend/dependency/resolve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Represents a resolved dependency
pub struct ResolvedDependency {

}
13 changes: 7 additions & 6 deletions src/backend/lock.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::{collections::{self, HashMap}, error, fs, hash, result};
use std::{collections::{self, HashMap}, error, fs, result};

use serde::{Deserialize, Serialize};

use super::context::AbsoltuePaths;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StateLockFile {
pub dependencies: collections::HashMap<String, Dependency>,
pub dependencies: collections::HashMap<String, StateLockFileDependency>,
}

/**
* 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,
}

Expand Down
3 changes: 2 additions & 1 deletion src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod context;
pub mod project;
pub mod toolchain;
pub mod lock;
pub mod lock;
pub mod dependency;
17 changes: 15 additions & 2 deletions src/backend/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn error::Error>>{
// 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(())
}

Expand Down
6 changes: 6 additions & 0 deletions src/frontend/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ pub static RUN_CMD: Lazy<Command> = Lazy::new(|| {
.about("Build & run your Java project")
.alias("r")
});

pub static ADD_CMD: Lazy<Command> = Lazy::new(|| {
Command::new("add")
.about("Add a dependency from Maven Repository to your project")
.alias("a")
});
9 changes: 9 additions & 0 deletions src/frontend/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::backend::context::{AbsoltuePaths, ProjectContext};
use crate::backend::project::Project;
use crate::backend::toolchain::{
compile_project, run_jar, ToolchainContext,
};
Expand Down Expand Up @@ -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<dyn error::Error>> {
//

// pass ownership back
Ok((p_ctx, tc_ctx))
}
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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"),
}
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod directory;
pub mod pathutil;
pub mod net;
22 changes: 22 additions & 0 deletions src/util/net.rs
Original file line number Diff line number Diff line change
@@ -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<dyn error::Error>> {
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(())
}

0 comments on commit f22e5ce

Please sign in to comment.