Skip to content

Commit

Permalink
Sort artifacts by known format (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
meruiden committed Aug 28, 2024
1 parent 3f54987 commit 96e8695
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Sort artifacts by known format ([#76])

[#76]: https://github.com/rojo-rbx/rokit/pull/76

## `0.2.5` - August 28th, 2024

### Added
Expand Down
37 changes: 34 additions & 3 deletions lib/sources/artifact/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use super::util::split_filename_and_extensions;
/**
An artifact format supported by Rokit.
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ArtifactFormat {
Zip,
Tar,
TarGz,
Tar,
Zip,
Gz,
}

Expand Down Expand Up @@ -146,4 +146,35 @@ mod tests {
assert_eq!(format_from_str("file.GZ"), Some(ArtifactFormat::Gz));
assert_eq!(format_from_str("file.Gz"), Some(ArtifactFormat::Gz));
}

#[test]
fn test_ordering() {
let artifact_names = [
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.gz",
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
];

let mut artifact_formats: Vec<Option<ArtifactFormat>> = artifact_names
.iter()
.map(|name| format_from_str(name))
.collect();

artifact_formats.sort();

assert_eq!(
artifact_formats,
vec![
None,
None,
Some(ArtifactFormat::TarGz),
Some(ArtifactFormat::Tar),
Some(ArtifactFormat::Zip),
Some(ArtifactFormat::Gz),
]
);
}
}
2 changes: 2 additions & 0 deletions lib/sources/artifact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod sorting;
mod util;

use self::sorting::sort_preferred_artifact;
use self::sorting::sort_preferred_formats;
use self::util::split_filename_and_extensions;

pub use self::format::ArtifactFormat;
Expand Down Expand Up @@ -180,6 +181,7 @@ impl Artifact {
current_desc
.sort_by_preferred_compat(desc_a, desc_b)
.then_with(|| sort_preferred_artifact(artifact_a, artifact_b))
.then_with(|| sort_preferred_formats(artifact_a, artifact_b))
});

compatible_artifacts
Expand Down
54 changes: 54 additions & 0 deletions lib/sources/artifact/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,19 @@ fn word_is_not_arch_or_os_or_version_or_numeric(word: impl AsRef<str>) -> bool {
&& !word.chars().all(char::is_numeric)
}

pub(super) fn sort_preferred_formats(artifact_a: &Artifact, artifact_b: &Artifact) -> Ordering {
match (artifact_a.format, artifact_b.format) {
(None, None) => std::cmp::Ordering::Equal,
(None, _) => std::cmp::Ordering::Greater,
(_, None) => std::cmp::Ordering::Less,
(Some(format_a), Some(format_b)) => format_a.cmp(&format_b),
}
}

#[cfg(test)]
mod tests {
use crate::sources::{ArtifactFormat, ArtifactProvider};

use super::*;

fn new_id(author: &str, name: &str) -> ToolId {
Expand Down Expand Up @@ -163,4 +174,47 @@ mod tests {
test_no_mentions("sentry-cli-linux-i686-2.32.1", "sentry-cli");
test_no_mentions("selene-light-0.27.1-linux.zip", "selene-light");
}

#[test]
fn test_prefers_known_format() {
let artifact_names = vec![
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.gz",
];

let mut artifacts = artifact_names
.into_iter()
.map(|name| Artifact {
provider: ArtifactProvider::GitHub,
format: ArtifactFormat::from_path_or_url(name),
id: Some("id".to_string()),
url: Some("https://github.com".parse().unwrap()),
name: Some(name.to_string()),
tool_spec: new_id("author", name).into_spec(Version::parse("1.0.0").unwrap()),
})
.collect::<Vec<_>>();

artifacts.sort_by(sort_preferred_formats);

let artifact_names_sorted = artifacts
.iter()
.map(|artifact| artifact.name.as_deref().unwrap())
.collect::<Vec<_>>();

assert_eq!(
artifact_names_sorted,
vec![
"tool-v1.0.0-x86_64-linux.tar.gz",
"tool-v1.0.0-x86_64-linux.tar",
"tool-v1.0.0-x86_64-linux.zip",
"tool-v1.0.0-x86_64-linux.gz",
"tool-v1.0.0-x86_64-linux",
"tool-v1.0.0-x86_64-linux.elf",
]
);
}
}

0 comments on commit 96e8695

Please sign in to comment.