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

@ -8,9 +8,6 @@ authors = ["ryan <ryan@kurte.nz>"]
edition = "2021" edition = "2021"
license = "GPL-3.0" license = "GPL-3.0"
[package.metadata.binstall]
bin-dir = "{ bin }{ binary-ext }"
[[bin]] [[bin]]
name = "cargo-binstall" name = "cargo-binstall"
path = "src/main.rs" path = "src/main.rs"

View file

@ -8,9 +8,6 @@ authors = ["ryan <ryan@kurte.nz>"]
edition = "2021" edition = "2021"
license = "GPL-3.0" license = "GPL-3.0"
[package.metadata.binstall]
bin-dir = "{ bin }{ binary-ext }"
[[bin]] [[bin]]
name = "cargo-binstall" name = "cargo-binstall"
path = "src/main.rs" path = "src/main.rs"

View file

@ -0,0 +1,16 @@
[package]
name = "cargo-binstall"
description = "Rust binary package installer for CI integration"
repository = "https://github.com/cargo-bins/cargo-binstall"
version = "0.12.0"
rust-version = "1.61.0"
authors = ["ryan <ryan@kurte.nz>"]
edition = "2021"
license = "GPL-3.0"
[package.metadata.binstall]
bin-dir = "{ bin }{ binary-ext }"
[[bin]]
name = "cargo-binstall"
path = "src/main.rs"

View file

@ -8,9 +8,6 @@ authors = ["ryan <ryan@kurte.nz>"]
edition = "2021" edition = "2021"
license = "GPL-3.0" license = "GPL-3.0"
[package.metadata.binstall]
bin-dir = "{ bin }{ binary-ext }"
[[bin]] [[bin]]
name = "cargo-binstall" name = "cargo-binstall"
path = "src/main.rs" path = "src/main.rs"

View file

@ -7,7 +7,7 @@ unset CARGO_HOME
# Install binaries using cargo-binstall # Install binaries using cargo-binstall
# shellcheck disable=SC2086 # shellcheck disable=SC2086
"./$1" binstall --log-level debug --no-confirm b3sum cargo-release cargo-binstall cargo-watch "./$1" binstall --log-level debug --no-confirm b3sum cargo-release cargo-binstall cargo-watch miniserve
# Test that the installed binaries can be run # Test that the installed binaries can be run
b3sum --version b3sum --version
@ -15,11 +15,13 @@ cargo-release release --version
cargo-binstall --help >/dev/null cargo-binstall --help >/dev/null
cargo binstall --help >/dev/null cargo binstall --help >/dev/null
cargo watch -V cargo watch -V
miniserve -V
test_resources=".github/scripts/cargo-tomls" test_resources=".github/scripts/cargo-tomls"
# Install binaries using `--manifest-path` # Install binaries using `--manifest-path`
"./$1" binstall --force --log-level debug --manifest-path "$test_resources/gitlab-test-Cargo.toml" --no-confirm cargo-binstall # Also test default github template
"./$1" binstall --force --log-level debug --manifest-path "$test_resources/github-test-Cargo.toml" --no-confirm cargo-binstall
# Test that the installed binaries can be run # Test that the installed binaries can be run
cargo binstall --help >/dev/null cargo binstall --help >/dev/null
@ -63,6 +65,7 @@ cargo binstall --help >/dev/null
# FIXME: remove/replace once #136 lands # FIXME: remove/replace once #136 lands
PATH="$test_resources/fake-cargo:$PATH" PATH="$test_resources/fake-cargo:$PATH"
# Test default GitLab pkg-url templates
"./$1" binstall \ "./$1" binstall \
--force \ --force \
--manifest-path "$test_resources/gitlab-test-Cargo.toml" \ --manifest-path "$test_resources/gitlab-test-Cargo.toml" \
@ -77,3 +80,12 @@ PATH="$test_resources/fake-cargo:$PATH"
--log-level debug \ --log-level debug \
--no-confirm \ --no-confirm \
cargo-binstall cargo-binstall
# Test default Github pkg-url templates,
# with bin-dir provided
"./$1" binstall \
--force \
--manifest-path "$test_resources/github-test-Cargo2.toml" \
--log-level debug \
--no-confirm \
cargo-binstall

View file

@ -11,7 +11,7 @@ As an example, the configuration would be like this:
```toml ```toml
[package.metadata.binstall] [package.metadata.binstall]
pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.{ archive-format }" pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }{ archive-suffix }"
bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }" bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }"
pkg-fmt = "tgz" pkg-fmt = "tgz"
``` ```
@ -30,7 +30,8 @@ Template variables use the format `{ VAR }` where `VAR` is the name of the varia
- `repo` is the repository linked in `Cargo.toml` - `repo` is the repository linked in `Cargo.toml`
- `bin` is the name of a specific binary, inferred from the crate configuration - `bin` is the name of a specific binary, inferred from the crate configuration
- `target` is the rust target name (defaults to your architecture, but can be overridden using the `--target` command line option if required() - `target` is the rust target name (defaults to your architecture, but can be overridden using the `--target` command line option if required()
- `archive-format` is the filename extension of the package archive format - `archive-suffix` is the filename extension of the package archive format that includes the prefix `.`, e.g. `.tgz` for tgz or `.exe`/`""` for bin.
- `archive-format` is the soft-deprecated filename extension of the package archive format that does not include the prefix `.`, e.g. `tgz` for tgz or `exe`/`""` for bin.
- `binary-ext` is the string `.exe` if the `target` is for Windows, or the empty string otherwise - `binary-ext` is the string `.exe` if the `target` is for Windows, or the empty string otherwise
- `format` is a soft-deprecated alias for `archive-format` in `pkg-url`, and alias for `binary-ext` in `bin-dir`; in the future, this may warn at install time. - `format` is a soft-deprecated alias for `archive-format` in `pkg-url`, and alias for `binary-ext` in `bin-dir`; in the future, this may warn at install time.
@ -61,13 +62,13 @@ The URLs are derived from a set of filenames and a set of paths, which are
"multiplied together": every filename appended to every path. The filenames "multiplied together": every filename appended to every path. The filenames
are: are:
- `{ name }-{ target }-{ version }.{ archive-format }` - `{ name }-{ target }-{ version }{ archive-suffix }`
- `{ name }-{ target }-v{ version }.{ archive-format }` - `{ name }-{ target }-v{ version }{ archive-suffix }`
- `{ name }-{ version }-{ target }.{ archive-format }` - `{ name }-{ version }-{ target }{ archive-suffix }`
- `{ name }-v{ version }-{ target }.{ archive-format }` - `{ name }-v{ version }-{ target }{ archive-suffix }`
- `{ name }-{ version }-{ target }.{ archive-format }` - `{ name }-{ version }-{ target }{ archive-suffix }`
- `{ name }-v{ version }-{ target }.{ archive-format }` - `{ name }-v{ version }-{ target }{ archive-suffix }`
- `{ name }-{ target }.{ archive-format }` ("versionless") - `{ name }-{ target }{ archive-suffix }` ("versionless")
The paths are: The paths are:
@ -130,7 +131,7 @@ As is common with libraries/utilities (and the `radio-sx128x` example), this can
```toml ```toml
[package.metadata.binstall] [package.metadata.binstall]
pkg-url = "{ repo }/releases/download/v{ version }/sx128x-util-{ target }-v{ version }.{ archive-format }" pkg-url = "{ repo }/releases/download/v{ version }/sx128x-util-{ target }-v{ version }{ archive-suffix }"
``` ```
Which provides a download URL of `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` Which provides a download URL of `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`

View file

@ -1,5 +1,6 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
fs,
path::{Component, Path, PathBuf}, path::{Component, Path, PathBuf},
}; };
@ -47,7 +48,7 @@ pub fn infer_bin_dir_template(data: &Data) -> Cow<'static, str> {
name.to_string(), 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 possible_dirs
.into_iter() .into_iter()
@ -67,6 +68,7 @@ pub struct BinFile {
pub source: PathBuf, pub source: PathBuf,
pub dest: PathBuf, pub dest: PathBuf,
pub link: Option<PathBuf>, pub link: Option<PathBuf>,
pub pkg_fmt: Option<PkgFmt>,
} }
impl BinFile { impl BinFile {
@ -94,7 +96,9 @@ impl BinFile {
binary_ext, 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() data.bin_path.clone()
} else { } else {
// Generate install paths // Generate install paths
@ -113,12 +117,7 @@ impl BinFile {
}); });
} }
let source_file_path = match path_normalized { data.bin_path.join(path_normalized.as_ref())
Cow::Borrowed(..) => path,
Cow::Owned(path) => path.to_string_lossy().into_owned(),
};
data.bin_path.join(&source_file_path)
}; };
// Destination at install dir + base-name{.extension} // Destination at install dir + base-name{.extension}
@ -141,6 +140,7 @@ impl BinFile {
source, source,
dest, dest,
link, link,
pkg_fmt,
}) })
} }
@ -183,6 +183,15 @@ impl BinFile {
self.source.display(), self.source.display(),
self.dest.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)?; atomic_install(&self.source, &self.dest)?;
Ok(()) Ok(())

View file

@ -203,6 +203,9 @@ struct Context<'c> {
#[serde(rename = "archive-format")] #[serde(rename = "archive-format")]
pub archive_format: &'c str, 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 /// Filename extension on the binary, i.e. .exe on Windows, nothing otherwise
#[serde(rename = "binary-ext")] #[serde(rename = "binary-ext")]
pub binary_ext: &'c str, pub binary_ext: &'c str,
@ -211,9 +214,18 @@ struct Context<'c> {
impl<'c> Context<'c> { impl<'c> Context<'c> {
pub(self) fn from_data_with_repo( pub(self) fn from_data_with_repo(
data: &'c Data, data: &'c Data,
archive_format: &'c str, archive_suffix: &'c str,
repo: Option<&'c str>, repo: Option<&'c str>,
) -> Self { ) -> 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 { Self {
name: &data.name, name: &data.name,
repo, repo,
@ -221,6 +233,7 @@ impl<'c> Context<'c> {
version: &data.version, version: &data.version,
format: archive_format, format: archive_format,
archive_format, archive_format,
archive_suffix,
binary_ext: if data.target.contains("windows") { binary_ext: if data.target.contains("windows") {
".exe" ".exe"
} else { } else {
@ -267,7 +280,7 @@ mod test {
meta, meta,
}; };
let ctx = Context::from_data(&data, "tgz"); let ctx = Context::from_data(&data, ".tgz");
assert_eq!( assert_eq!(
ctx.render_url(DEFAULT_PKG_URL).unwrap(), 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") 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, 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()) ctx.render_url(data.meta.pkg_url.as_deref().unwrap())
.unwrap(); .unwrap();
} }
@ -306,7 +319,7 @@ mod test {
meta, meta,
}; };
let ctx = Context::from_data(&data, "tgz"); let ctx = Context::from_data(&data, ".tgz");
assert_eq!( assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(), 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") 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, meta,
}; };
let ctx = Context::from_data(&data, "tgz"); let ctx = Context::from_data(&data, ".tgz");
assert_eq!( assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(), 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") 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, meta,
}; };
let ctx = Context::from_data(&data, "tgz"); let ctx = Context::from_data(&data, ".tgz");
assert_eq!( assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(), 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") 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, meta,
}; };
let ctx = Context::from_data(&data, "txz"); let ctx = Context::from_data(&data, ".txz");
assert_eq!( assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(), 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") 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, meta,
}; };
let ctx = Context::from_data(&data, "bin"); let ctx = Context::from_data(&data, ".bin");
assert_eq!( assert_eq!(
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(), 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") 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` /// Make sure to update possible_dirs in `bins::infer_bin_dir_template`
/// if you modified FULL_FILENAMES or NOVERSION_FILENAMES. /// if you modified FULL_FILENAMES or NOVERSION_FILENAMES.
pub const FULL_FILENAMES: &[&str] = &[ pub const FULL_FILENAMES: &[&str] = &[
"{ name }-{ target }-v{ version }.{ archive-format }", "{ name }-{ target }-v{ version }{ archive-suffix }",
"{ name }-{ target }-{ version }.{ archive-format }", "{ name }-{ target }-{ version }{ archive-suffix }",
"{ name }-{ version }-{ target }.{ archive-format }", "{ name }-{ version }-{ target }{ archive-suffix }",
"{ name }-v{ version }-{ target }.{ archive-format }", "{ name }-v{ version }-{ target }{ archive-suffix }",
]; ];
pub const NOVERSION_FILENAMES: &[&str] = &[ pub const NOVERSION_FILENAMES: &[&str] = &[
"{ name }-{ target }.{ archive-format }", "{ name }-{ target }{ archive-suffix }",
"{ name }.{ archive-format }", "{ name }{ archive-suffix }",
]; ];
impl RepositoryHost { 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] { pub fn extensions(self) -> &'static [&'static str] {
match self { match self {
PkgFmt::Tar => &["tar"], PkgFmt::Tar => &[".tar"],
PkgFmt::Tbz2 => &["tbz2", "tar.bz2"], PkgFmt::Tbz2 => &[".tbz2", ".tar.bz2"],
PkgFmt::Tgz => &["tgz", "tar.gz"], PkgFmt::Tgz => &[".tgz", ".tar.gz"],
PkgFmt::Txz => &["txz", "tar.xz"], PkgFmt::Txz => &[".txz", ".tar.xz"],
PkgFmt::Tzstd => &["tzstd", "tzst", "tar.zst"], PkgFmt::Tzstd => &[".tzstd", ".tzst", ".tar.zst"],
PkgFmt::Bin => &["bin", "exe"], PkgFmt::Bin => &[".bin", ".exe", ""],
PkgFmt::Zip => &["zip"], PkgFmt::Zip => &[".zip"],
} }
} }
} }