Skip to content

Commit

Permalink
Fix handling of release artifacts where the platform looks like an ex…
Browse files Browse the repository at this point in the history
…tension

The `direnv` project does this, with names like "direnv.linux-amd64".
  • Loading branch information
autarch committed Oct 26, 2024
1 parent c77252c commit 9973ddb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
extensions. This caused `ubi` to fail when trying to install `shfmt` 3.10.0, and probably many
other tools. Reported by @jimeh (Jim Myhrberg). GH #67.

- Work around release artifacts that put the platform name after a period, so it looks like an
extension. This is the case with `direnv`, at least with the v2.35.0 release, which has releast
artifacts like "direnv.linux-amd64". Reported by @jimeh (Jim Myhrberg). GH #67.

## 0.2.0 - 2024-09-02

- For this release, the library and CLI code have been split into two crates. The library code now
Expand Down
9 changes: 9 additions & 0 deletions ubi-cli/tests/ubi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ fn integration_tests() -> Result<()> {
make_exe_pathbuf(&["bin", "yt-dlp"]),
)?;

// This project releases bare binaries with the platform name looking like an extension,
// e..g. "direnv.darwin-amd64".
run_test(
td.path(),
ubi.as_ref(),
&["--project", "direnv/direnv", "--tag", "v2.35.0"],
make_exe_pathbuf(&["bin", "direnv"]),
)?;

Ok(())
}

Expand Down
10 changes: 10 additions & 0 deletions ubi/src/extension.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::os::all_oses_re;
use anyhow::Result;
use itertools::Itertools;
use lazy_regex::regex;
Expand Down Expand Up @@ -71,6 +72,11 @@ impl Extension {
return Ok(None);
}

if extension_is_platform(ext_str) {
debug!("the extension {ext_str:?} is a platform name, ignoring");
return Ok(None);
}

Err(ExtensionError::UnknownExtension {
path: path.to_string(),
ext: ext_str.to_string_lossy().to_string(),
Expand Down Expand Up @@ -101,6 +107,10 @@ fn extension_is_part_of_version(path: &str, ext_str: &OsStr) -> bool {
ext_str == dot_num.as_str()
}

fn extension_is_platform(ext_str: &OsStr) -> bool {
all_oses_re().is_match(ext_str.to_string_lossy().as_ref())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions ubi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod arch;
mod extension;
mod fetcher;
mod installer;
mod os;
mod picker;
mod release;

Expand Down
54 changes: 54 additions & 0 deletions ubi/src/os.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use itertools::Itertools;
use lazy_regex::{regex, Lazy};
use regex::Regex;

pub(crate) fn freebsd_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)freebsd(?:\b|_))")
}

pub(crate) fn fuchsia() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)fuchsia(?:\b|_))")
}

pub(crate) fn illumos_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)illumos(?:\b|_))")
}

pub(crate) fn linux_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)linux(?:\b|_|32|64))")
}

pub(crate) fn macos_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)(?:darwin|macos|osx)(?:\b|_))")
}

pub(crate) fn netbsd_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)netbsd(?:\b|_))")
}

pub(crate) fn solaris_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)solaris(?:\b|_))")
}

pub(crate) fn windows_re() -> &'static Lazy<Regex> {
regex!(r"(?i:(?:\b|_)win(?:32|64|dows)?(?:\b|_))")
}

pub(crate) fn all_oses_re() -> Regex {
Regex::new(
&[
freebsd_re(),
fuchsia(),
illumos_re(),
linux_re(),
macos_re(),
netbsd_re(),
solaris_re(),
windows_re(),
]
.iter()
.map(|r| format!("({})", r.as_str()))
.join("|"),
)
.unwrap()
}
17 changes: 9 additions & 8 deletions ubi/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
x86_64_re,
},
extension::Extension,
os::{freebsd_re, fuchsia, illumos_re, linux_re, macos_re, netbsd_re, solaris_re, windows_re},
release::Asset,
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -378,17 +379,17 @@ impl<'a> AssetPicker<'a> {
// commented out here.
//
//OS::Dragonfly => regex!(r"(?i:(?:\b|_)dragonfly(?:\b|_))"),
OS::FreeBSD => regex!(r"(?i:(?:\b|_)freebsd(?:\b|_))"),
OS::Fuchsia => regex!(r"(?i:(?:\b|_)fuchsia(?:\b|_))"),
OS::FreeBSD => freebsd_re(),
OS::Fuchsia => fuchsia(),
//OS::Haiku => regex!(r"(?i:(?:\b|_)haiku(?:\b|_))"),
OS::IllumOS => regex!(r"(?i:(?:\b|_)illumos(?:\b|_))"),
OS::Linux => regex!(r"(?i:(?:\b|_)linux(?:\b|_|32|64))"),
OS::MacOS => regex!(r"(?i:(?:\b|_)(?:darwin|macos|osx)(?:\b|_))"),
OS::NetBSD => regex!(r"(?i:(?:\b|_)netbsd(?:\b|_))"),
OS::IllumOS => illumos_re(),
OS::Linux => linux_re(),
OS::MacOS => macos_re(),
OS::NetBSD => netbsd_re(),
//OS::OpenBSD => regex!(r"(?i:(?:\b|_)openbsd(?:\b|_))"),
OS::Solaris => regex!(r"(?i:(?:\b|_)solaris(?:\b|_))"),
OS::Solaris => solaris_re(),
//OS::VxWorks => regex!(r"(?i:(?:\b|_)vxworks(?:\b|_))"),
OS::Windows => regex!(r"(?i:(?:\b|_)win(?:32|64|dows)?(?:\b|_))"),
OS::Windows => windows_re(),
_ => unreachable!(
"Cannot determine what type of compiled binary to use for this platform"
),
Expand Down

0 comments on commit 9973ddb

Please sign in to comment.