Improve auto discover (#433)

* Add new field `gh_crate_meta::Context::archive_suffix`
* Support `archive_suffix = ""` for unix
* Use `archive-suffix` in `FULL_FILENAMES` & `NOVERSION_FILENAMES` to support `PkgFmt::Bin`
* Simplify `PkgFmt::extensions`
* Fix `default_bin_dir_template` in `infer_bin_dir_template`: Fix typo `archive_suffix`
* Test `default_bin_dir_template` in CI
* Fix installation of `PkgFmt::Bin`: `chmod +x $bin` on unix if fmt == Bin
* Add miniserve to `.github/scripts/tests.sh`
* Update `SUPPORT.md`
* Add github-test-Cargo2.toml to test Github pkg-url with bin-dir provided
* Avoid allocation on processing `path_normalized` in `BinFile::from_product`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-09-28 12:52:14 +10:00 committed by GitHub
parent c15903684f
commit 4ba1e221ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 94 additions and 51 deletions

View file

@ -1,5 +1,6 @@
use std::{
borrow::Cow,
fs,
path::{Component, Path, PathBuf},
};
@ -47,7 +48,7 @@ pub fn infer_bin_dir_template(data: &Data) -> Cow<'static, str> {
name.to_string(),
];
let default_bin_dir_template = Cow::Borrowed("{ bin }{ binary_ext }");
let default_bin_dir_template = Cow::Borrowed("{ bin }{ binary-ext }");
possible_dirs
.into_iter()
@ -67,6 +68,7 @@ pub struct BinFile {
pub source: PathBuf,
pub dest: PathBuf,
pub link: Option<PathBuf>,
pub pkg_fmt: Option<PkgFmt>,
}
impl BinFile {
@ -94,7 +96,9 @@ impl BinFile {
binary_ext,
};
let source = if data.meta.pkg_fmt == Some(PkgFmt::Bin) {
let pkg_fmt = data.meta.pkg_fmt;
let source = if pkg_fmt == Some(PkgFmt::Bin) {
data.bin_path.clone()
} else {
// Generate install paths
@ -113,12 +117,7 @@ impl BinFile {
});
}
let source_file_path = match path_normalized {
Cow::Borrowed(..) => path,
Cow::Owned(path) => path.to_string_lossy().into_owned(),
};
data.bin_path.join(&source_file_path)
data.bin_path.join(path_normalized.as_ref())
};
// Destination at install dir + base-name{.extension}
@ -141,6 +140,7 @@ impl BinFile {
source,
dest,
link,
pkg_fmt,
})
}
@ -183,6 +183,15 @@ impl BinFile {
self.source.display(),
self.dest.display()
);
if self.pkg_fmt == Some(PkgFmt::Bin) {
#[cfg(unix)]
fs::set_permissions(
&self.source,
std::os::unix::fs::PermissionsExt::from_mode(0o755),
)?;
}
atomic_install(&self.source, &self.dest)?;
Ok(())

View file

@ -203,6 +203,9 @@ struct Context<'c> {
#[serde(rename = "archive-format")]
pub archive_format: &'c str,
#[serde(rename = "archive-suffix")]
pub archive_suffix: &'c str,
/// Filename extension on the binary, i.e. .exe on Windows, nothing otherwise
#[serde(rename = "binary-ext")]
pub binary_ext: &'c str,
@ -211,9 +214,18 @@ struct Context<'c> {
impl<'c> Context<'c> {
pub(self) fn from_data_with_repo(
data: &'c Data,
archive_format: &'c str,
archive_suffix: &'c str,
repo: Option<&'c str>,
) -> Self {
let archive_format = if archive_suffix.is_empty() {
// Empty archive_suffix means PkgFmt::Bin
"bin"
} else {
debug_assert!(archive_suffix.starts_with('.'), "{archive_suffix}");
&archive_suffix[1..]
};
Self {
name: &data.name,
repo,
@ -221,6 +233,7 @@ impl<'c> Context<'c> {
version: &data.version,
format: archive_format,
archive_format,
archive_suffix,
binary_ext: if data.target.contains("windows") {
".exe"
} else {
@ -267,7 +280,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "tgz");
let ctx = Context::from_data(&data, ".tgz");
assert_eq!(
ctx.render_url(DEFAULT_PKG_URL).unwrap(),
url("https://github.com/ryankurte/cargo-binstall/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
@ -286,7 +299,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "tgz");
let ctx = Context::from_data(&data, ".tgz");
ctx.render_url(data.meta.pkg_url.as_deref().unwrap())
.unwrap();
}
@ -306,7 +319,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "tgz");
let ctx = Context::from_data(&data, ".tgz");
assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
url("https://example.com/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
@ -330,7 +343,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "tgz");
let ctx = Context::from_data(&data, ".tgz");
assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
@ -352,7 +365,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "tgz");
let ctx = Context::from_data(&data, ".tgz");
assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
@ -378,7 +391,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "txz");
let ctx = Context::from_data(&data, ".txz");
assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-apple-darwin.tar.xz")
@ -401,7 +414,7 @@ mod test {
meta,
};
let ctx = Context::from_data(&data, "bin");
let ctx = Context::from_data(&data, ".bin");
assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-pc-windows-msvc.exe")

View file

@ -14,15 +14,15 @@ pub enum RepositoryHost {
/// Make sure to update possible_dirs in `bins::infer_bin_dir_template`
/// if you modified FULL_FILENAMES or NOVERSION_FILENAMES.
pub const FULL_FILENAMES: &[&str] = &[
"{ name }-{ target }-v{ version }.{ archive-format }",
"{ name }-{ target }-{ version }.{ archive-format }",
"{ name }-{ version }-{ target }.{ archive-format }",
"{ name }-v{ version }-{ target }.{ archive-format }",
"{ name }-{ target }-v{ version }{ archive-suffix }",
"{ name }-{ target }-{ version }{ archive-suffix }",
"{ name }-{ version }-{ target }{ archive-suffix }",
"{ name }-v{ version }-{ target }{ archive-suffix }",
];
pub const NOVERSION_FILENAMES: &[&str] = &[
"{ name }-{ target }.{ archive-format }",
"{ name }.{ archive-format }",
"{ name }-{ target }{ archive-suffix }",
"{ name }{ archive-suffix }",
];
impl RepositoryHost {

View file

@ -44,16 +44,17 @@ impl PkgFmt {
}
}
/// List of possible file extensions for the format.
/// List of possible file extensions for the format
/// (with prefix `.`).
pub fn extensions(self) -> &'static [&'static str] {
match self {
PkgFmt::Tar => &["tar"],
PkgFmt::Tbz2 => &["tbz2", "tar.bz2"],
PkgFmt::Tgz => &["tgz", "tar.gz"],
PkgFmt::Txz => &["txz", "tar.xz"],
PkgFmt::Tzstd => &["tzstd", "tzst", "tar.zst"],
PkgFmt::Bin => &["bin", "exe"],
PkgFmt::Zip => &["zip"],
PkgFmt::Tar => &[".tar"],
PkgFmt::Tbz2 => &[".tbz2", ".tar.bz2"],
PkgFmt::Tgz => &[".tgz", ".tar.gz"],
PkgFmt::Txz => &[".txz", ".tar.xz"],
PkgFmt::Tzstd => &[".tzstd", ".tzst", ".tar.zst"],
PkgFmt::Bin => &[".bin", ".exe", ""],
PkgFmt::Zip => &[".zip"],
}
}
}