Skip to content

Commit

Permalink
this is better
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Sep 23, 2023
1 parent b4309f9 commit 281c65b
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 725 deletions.
48 changes: 30 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![allow(clippy::struct_excessive_bools)]
#![allow(unknown_lints)]

use std::{path::PathBuf, collections::HashMap};
use std::collections::HashMap;

use anyhow::{Context, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -111,7 +111,7 @@ pub trait Source {
async fn resolve_source(
&self,
app: &App,
) -> Result<FileSource>;
) -> Result<ResolvedFile>;
}

pub struct BaseApp {
Expand Down Expand Up @@ -188,6 +188,10 @@ impl<'a> App {
sources::maven::MavenAPI(&self)
}

pub fn quilt(&'a self) -> sources::quilt::QuiltAPI<'a> {
sources::quilt::QuiltAPI(&self)
}

pub fn forge(&'a self) -> sources::forge::ForgeAPI<'a> {
sources::forge::ForgeAPI(&self)
}
Expand All @@ -196,32 +200,40 @@ impl<'a> App {
sources::neoforge::NeoforgeAPI(&self)
}


}
pub fn papermc(&'a self) -> sources::papermc::PaperMCAPI<'a> {
sources::papermc::PaperMCAPI(&self)
}

pub enum FileSource {
Download {
url: String,
filename: String,
cache: CacheStrategy,
size: Option<i32>,
hashes: HashMap<String, String>,
},
pub fn purpurmc(&'a self) -> sources::purpur::PurpurAPI<'a> {
sources::purpur::PurpurAPI(&self)
}

pub fn spigot(&'a self) -> sources::spigot::SpigotAPI<'a> {
sources::spigot::SpigotAPI(&self)
}

Cached {
path: PathBuf,
filename: String,
pub fn vanilla(&'a self) -> sources::vanilla::VanillaAPI<'a> {
sources::vanilla::VanillaAPI(&self)
}
}

pub struct ResolvedFile {
url: String,
filename: String,
cache: CacheStrategy,
size: Option<i32>,
hashes: HashMap<String, String>,
}

pub enum CacheStrategy {
File {
path: PathBuf,
namespace: String,
path: String,
},
Indexed {
index_path: PathBuf,
index_path: String,
key: String,
value: PathBuf,
value: String,
},
None,
}
27 changes: 10 additions & 17 deletions src/model/downloadable/import_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
use reqwest::Url;

use crate::{
model::Server,
sources::{
curserinth::{fetch_curserinth_versions, CurseRinthVersion},
github::fetch_github_releases,
modrinth::{fetch_modrinth_versions, ModrinthVersion},
},
curserinth::CurseRinthVersion,

Check warning on line 7 in src/model/downloadable/import_url.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `curserinth::CurseRinthVersion`

warning: unused import: `curserinth::CurseRinthVersion` --> src/model/downloadable/import_url.rs:7:9 | 7 | curserinth::CurseRinthVersion, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
modrinth::ModrinthVersion,
}, App,
};

use super::Downloadable;

impl Downloadable {
#[allow(clippy::too_many_lines)]
pub async fn from_url_interactive(
client: &reqwest::Client,
_server: &Server,
app: &App,
urlstr: &str,
datapack_mode: bool,
) -> Result<Self> {
Expand Down Expand Up @@ -61,11 +58,9 @@ impl Downloadable {
.to_owned()
.to_owned();

let versions: Vec<ModrinthVersion> = fetch_modrinth_versions(client, &id, None)
let versions: Vec<ModrinthVersion> = app.modrinth().fetch_versions(&id)
.await?
.into_iter()
// TODO: better filtering, commented out because proxy server versioning is complex..?
//.filter(|v| v.game_versions.contains(&server.mc_version))
.filter(|v| {
if datapack_mode {
v.loaders.contains(&"datapack".to_owned())
Expand Down Expand Up @@ -134,8 +129,7 @@ impl Downloadable {
.to_owned()
.to_owned();

let versions: Vec<CurseRinthVersion> =
fetch_curserinth_versions(client, &id, None).await?;
let (versions, _) = app.curserinth().fetch_versions(&id).await?;

let version = if let Some(&"version") = segments.get(2) {
let ver_num = segments
Expand Down Expand Up @@ -204,10 +198,8 @@ impl Downloadable {
let version = if let Some(v) = segments.get(4) {
v.to_owned().to_owned()
} else {
let versions: Vec<ModrinthVersion> = fetch_modrinth_versions(client, &id, None)
.await?
.into_iter()
.collect();
let (versions, _) = app.curserinth().fetch_versions(&id)
.await?;

if versions.is_empty() {
bail!("No compatible versions in curseforge/rinth project");
Expand Down Expand Up @@ -254,6 +246,7 @@ impl Downloadable {

Ok(Downloadable::Spigot {
id: id.to_owned().to_owned(),
version: "latest".to_owned()
})
}
// the code under this domain is awful.. srry
Expand Down Expand Up @@ -307,7 +300,7 @@ impl Downloadable {
}
};

let fetched_tags = fetch_github_releases(&repo, client).await?;
let fetched_tags = app.github().fetch_releases(&repo).await?;

let tag = if let Some(t) = tag_opt {
t
Expand Down
34 changes: 15 additions & 19 deletions src/model/downloadable/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ use regex::Regex;

use crate::{
model::Downloadable,
sources::{
curserinth::fetch_curserinth_project,
github::fetch_repo_description,
jenkins::{fetch_jenkins_description, str_process_job},
modrinth::fetch_modrinth_project,
spigot::fetch_spigot_info,
},
sources::jenkins::{fetch_jenkins_description, str_process_job}, App,
};

impl Downloadable {
Expand Down Expand Up @@ -38,7 +32,7 @@ impl Downloadable {
Self::Maven { url, group, .. } => {
format!("[{g}]({url}/{g})", g = group.replace('.', "/"))
}
Self::Spigot { id } => {
Self::Spigot { id, .. } => {
format!("[{id}](https://www.spigotmc.org/resources/{id})")
}
Self::Modrinth { id, .. } => {
Expand All @@ -52,13 +46,13 @@ impl Downloadable {

pub async fn fetch_info_to_map(
&self,
client: &reqwest::Client,
app: &App,
) -> Result<IndexMap<String, String>> {
let mut map: IndexMap<String, String> = IndexMap::new();

match self {
Self::Modrinth { id, version } => {
let proj = fetch_modrinth_project(client, id).await?;
let proj = app.modrinth().fetch_project(id).await?;

map.insert(
"Name".to_owned(),
Expand All @@ -69,7 +63,7 @@ impl Downloadable {
}

Self::CurseRinth { id, version } => {
let proj = fetch_curserinth_project(client, id).await?;
let proj = app.curserinth().fetch_project(id).await?;

map.insert(
"Name".to_owned(),
Expand All @@ -79,18 +73,19 @@ impl Downloadable {
map.insert("Version".to_owned(), version.clone());
}

Self::Spigot { id } => {
let (name, desc) = fetch_spigot_info(client, id).await?;
Self::Spigot { id, version } => {
let (name, desc) = app.spigot().fetch_info(id).await?;

map.insert(
"Name".to_owned(),
format!("[{name}](https://www.spigotmc.org/resources/{id})"),
);
map.insert("Description".to_owned(), sanitize(&desc)?);
map.insert("Version".to_owned(), version.clone());
}

Self::Hangar { id, version } => {
let proj = mcapi::hangar::fetch_project(client, id).await?;
let proj = mcapi::hangar::fetch_project(&app.http_client, id).await?;

map.insert(
"Name".to_owned(),
Expand All @@ -101,7 +96,7 @@ impl Downloadable {
}

Self::GithubRelease { repo, tag, asset } => {
let desc = fetch_repo_description(client, repo).await?;
let desc = app.github().fetch_repo_description(repo).await?;

map.insert("Name".to_owned(), self.get_md_link());
map.insert("Description".to_owned(), sanitize(&desc)?);
Expand All @@ -114,7 +109,7 @@ impl Downloadable {
build,
artifact,
} => {
let desc = fetch_jenkins_description(client, url, job).await?;
let desc = fetch_jenkins_description(&app.http_client, url, job).await?;

map.insert("Name".to_owned(), self.get_md_link());
map.insert("Description".to_owned(), sanitize(&desc)?);
Expand Down Expand Up @@ -190,8 +185,9 @@ impl Downloadable {
map.insert("Version/Release".to_owned(), version.clone());
}

Self::Spigot { id } => {
Self::Spigot { id, version } => {
map.insert("Project/URL".to_owned(), id.clone());
map.insert("Version/Release".to_owned(), version.clone());
}

Self::Jenkins {
Expand Down Expand Up @@ -229,9 +225,9 @@ impl Downloadable {
Self::Modrinth { id, .. } => format!("Modrinth:{id}"),
Self::Hangar { id, .. } => format!("Hangar:{id}"),
Self::CurseRinth { id, .. } => format!("CurseRinth:{id}"),
Self::Spigot { id } => format!("Spigot:{id}"),
Self::Spigot { id, .. } => format!("Spigot:{id}"),
Self::GithubRelease { repo, .. } => format!("Github:{repo}"),
Self::Jenkins { job, .. } => format!("Jenkins:{job}"),
Self::Jenkins { job, .. } => format!("Jenkins:{job}"),
Self::Url { filename, .. } => {
if let Some(f) = filename {
format!("URL:{f}")
Expand Down
Loading

0 comments on commit 281c65b

Please sign in to comment.