Skip to content

Commit

Permalink
improve plugin removal logic, other nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
dxbednarczyk committed Mar 6, 2024
1 parent ac28b0e commit ae01b00
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
16 changes: 9 additions & 7 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Dependency {
#[serde(alias = "project_id")]
pub id: String,
#[serde(skip)]
required: bool,
pub required: bool,
}

impl PartialEq for Dependency {
Expand Down Expand Up @@ -138,14 +138,16 @@ pub fn add(

let info = info.unwrap();

if !no_deps {
for dep in &info.dependencies {
if !dep.required && !optional_deps {
continue;
}
for dep in &info.dependencies {
if no_deps {
break;
}

add(provider, &dep.id, "latest", false, false)?;
if !dep.required && !optional_deps {
continue;
}

add(provider, &dep.id, "latest", false, false)?;
}

download(&info.source, &lockfile.loader.name, info.checksum.as_ref())?;
Expand Down
10 changes: 5 additions & 5 deletions src/plugin/modrinth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn fetch(lockfile: &Lockfile, id: &str, version: &str) -> Result<super::Info
)?
} else {
get_specific_version(
&project_info,
&project_info.slug,
version,
&lockfile.loader.minecraft_version,
&lockfile.loader.name,
Expand Down Expand Up @@ -127,24 +127,24 @@ pub fn fetch(lockfile: &Lockfile, id: &str, version: &str) -> Result<super::Info
}

fn get_specific_version(
project: &ProjectInfo,
slug: &str,
version: &str,
minecraft_version: &String,
loader: &String,
) -> Result<Version> {
let formatted_url = format!("{BASE_URL}/version/{version}");

info!("fetching version {version} of {}", project.slug);
info!("fetching version {version} of {slug}");

let resp: Version = ureq::get(&formatted_url)
.set("User-Agent", FAKE_USER_AGENT)
.call()?
.into_json()?;

if project.slug != resp.project_id {
if slug != resp.project_id {
return Err(anyhow!(
"version id {version} is not a part of project {}",
project.slug
slug
));
}

Expand Down
29 changes: 15 additions & 14 deletions src/server/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const LOCKFILE_PATH: &str = "pap.lock";
#[derive(Deserialize, Default, Serialize)]
pub struct Lockfile {
pub loader: Loader,
pub projects: Vec<plugin::Info>,
pub plugins: Vec<plugin::Info>,
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Lockfile {

Ok(Self {
loader: Loader::default(),
projects: vec![],
plugins: vec![],
})
}

Expand All @@ -74,7 +74,7 @@ impl Lockfile {

let mut lf = Self {
loader: l,
projects: vec![],
plugins: vec![],
};

lf.save()?;
Expand All @@ -83,14 +83,14 @@ impl Lockfile {
}

pub fn get(&self, project_id: &str) -> Result<&plugin::Info> {
self.projects
self.plugins
.iter()
.find(|p| p.slug == project_id)
.ok_or_else(|| anyhow!("key {project_id} not found"))
}

pub fn add(&mut self, info: plugin::Info) -> Result<()> {
self.projects.push(info);
self.plugins.push(info);

self.save()?;

Expand All @@ -102,13 +102,13 @@ impl Lockfile {
return Err(anyhow!("project {slug} does not exist in the lockfile"));
}

let mut projects = self.projects.iter();
let mut plugins = self.plugins.iter();

let idx = projects
let idx = plugins
.position(|p| p.slug == slug)
.ok_or_else(|| anyhow!("{slug} does not exist in the lockfile"))?;

let entry = self.projects[idx].clone();
let entry = self.plugins[idx].clone();

let mut to_remove = vec![entry.slug];

Expand All @@ -117,10 +117,11 @@ impl Lockfile {
break;
}

let cant_be_removed = projects.any(|p| {
let contains = p.dependencies.iter().find(|d| **d == dep);
let cant_be_removed = plugins.any(|p| {
let is_different = p.slug != slug;
let requires_dep = p.dependencies.iter().any(|d| *d == dep && d.required);

contains.is_some() && p.slug != slug
is_different && requires_dep
});

if !cant_be_removed {
Expand All @@ -130,16 +131,16 @@ impl Lockfile {

for slug in to_remove {
let idx = self
.projects
.plugins
.iter()
.position(|p| p.slug == slug || p.id == slug)
.ok_or_else(|| anyhow!("{slug} does not exist in the lockfile"))?;

if !keep_jarfile {
fs::remove_file(&self.projects[idx].get_file_path(&self.loader.name))?;
fs::remove_file(&self.plugins[idx].get_file_path(&self.loader.name))?;
}

self.projects.remove(idx);
self.plugins.remove(idx);
}

self.save()?;
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn install() -> Result<()> {
&lf.loader.version,
)?;

for entry in &lf.projects {
for entry in &lf.plugins {
plugin::download(&entry.source, &lf.loader.name, entry.checksum.as_ref())?;
}

Expand Down

0 comments on commit ae01b00

Please sign in to comment.