Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort artifacts by known format #76

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
]
);
}
}