Skip to content

Commit

Permalink
feature: Merge disable strategies (#1868)
Browse files Browse the repository at this point in the history
* feat: Merge --disable-strategies with ones in cargo manifest

Signed-off-by: Jiahao XU <[email protected]>

* Update doc

Signed-off-by: Jiahao XU <[email protected]>

* Update e2e-test-strategies

Signed-off-by: Jiahao XU <[email protected]>

* Fix typo in option doc in crates/bin/src/args.rs

Signed-off-by: Jiahao XU <[email protected]>

---------

Signed-off-by: Jiahao XU <[email protected]>
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu authored Aug 7, 2024
1 parent 90d47f7 commit b854f3f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
7 changes: 6 additions & 1 deletion SUPPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ As an example, the configuration would be like this:
pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }{ archive-suffix }"
bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }"
pkg-fmt = "tgz"
disabled-strategies = ["quick-install", "compile"]
```

With the following configuration keys:

- `pkg-url` specifies the package download URL for a given target/version, templated
- `bin-dir` specifies the binary path within the package, templated (with an `.exe` suffix on windows)
- `pkg-fmt` overrides the package format for download/extraction (defaults to: `tgz`)
- `disabled-strategies` to disable specific strategies (e.g. `crate-meta-data` for trying to find pre-built on your repository, `quick-install` for pre-built from third-party cargo-bins/cargo-quickinstall, `compile` for falling back to `cargo-install`) for your crate (defaults to empty array). The user can override this by explicitly specifying --strategies on the command line.
- `disabled-strategies` to disable specific strategies (e.g. `crate-meta-data` for trying to find pre-built on your repository,
`quick-install` for pre-built from third-party cargo-bins/cargo-quickinstall, `compile` for falling back to `cargo-install`)
for your crate (defaults to empty array).
If `--strategies` is passed on the command line, then the `disabled-strategies` in `package.metadata` will be ignored.
Otherwise, the `disabled-strategies` in `package.metadata` and `--disable-strategies` will be merged.


`pkg-url` and `bin-dir` are templated to support different names for different versions / architectures / etc.
Expand Down
27 changes: 17 additions & 10 deletions crates/bin/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
env,
ffi::OsString,
fmt,
fmt, mem,
num::{NonZeroU16, NonZeroU64, ParseIntError},
path::PathBuf,
str::FromStr,
Expand Down Expand Up @@ -155,6 +155,10 @@ pub struct Args {
/// Specify the strategies to be used,
/// binstall will run the strategies specified in order.
///
/// If this option is specified, then cargo-binstall will ignore
/// `disabled-strategies` in `package.metadata` in the cargo manifest
/// of the installed packages.
///
/// Default value is "crate-meta-data,quick-install,compile".
#[clap(
help_heading = "Overrides",
Expand All @@ -167,6 +171,10 @@ pub struct Args {
/// Disable the strategies specified.
/// If a strategy is specified in `--strategies` and `--disable-strategies`,
/// then it will be removed.
///
/// If `--strategies` is not specified, then the strategies specified in this
/// option will be merged with the disabled-strategies` in `package.metadata`
/// in the cargo manifest of the installed packages.
#[clap(
help_heading = "Overrides",
long,
Expand Down Expand Up @@ -570,7 +578,7 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
}
}

let has_strategies_override = !opts.strategies.is_empty();
let ignore_disabled_strategies = !opts.strategies.is_empty();

// Default strategies if empty
if opts.strategies.is_empty() {
Expand Down Expand Up @@ -626,15 +634,14 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
pkg_url: opts.pkg_url.take(),
pkg_fmt: opts.pkg_fmt.take(),
bin_dir: opts.bin_dir.take(),
disabled_strategies: (!opts.disable_strategies.is_empty() || has_strategies_override).then(
|| {
opts.disable_strategies
.iter()
.map(|strategy| strategy.0)
.collect::<Vec<_>>()
.into_boxed_slice()
},
disabled_strategies: Some(
mem::take(&mut opts.disable_strategies)
.into_iter()
.map(|strategy| strategy.0)
.collect::<Vec<_>>()
.into_boxed_slice(),
),
ignore_disabled_strategies,
signing: None,
};

Expand Down
28 changes: 24 additions & 4 deletions crates/binstalk-types/src/cargo_toml_binstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ impl PkgMeta {
where
It: IntoIterator<Item = &'a PkgOverride> + Clone,
{
let ignore_disabled_strategies = pkg_overrides
.clone()
.into_iter()
.any(|pkg_override| pkg_override.ignore_disabled_strategies);

Self {
pkg_url: pkg_overrides
.clone()
Expand All @@ -126,10 +131,22 @@ impl PkgMeta {
.find_map(|pkg_override| pkg_override.signing.clone())
.or_else(|| self.signing.clone()),

disabled_strategies: pkg_overrides
.into_iter()
.find_map(|pkg_override| pkg_override.disabled_strategies.clone())
.or_else(|| self.disabled_strategies.clone()),
disabled_strategies: if ignore_disabled_strategies {
None
} else {
let mut disabled_strategies = pkg_overrides
.into_iter()
.filter_map(|pkg_override| pkg_override.disabled_strategies.as_deref())
.flatten()
.chain(self.disabled_strategies.as_deref().into_iter().flatten())
.copied()
.collect::<Vec<Strategy>>();

disabled_strategies.sort_unstable();
disabled_strategies.dedup();

Some(disabled_strategies.into_boxed_slice())
},

overrides: Default::default(),
}
Expand All @@ -156,6 +173,9 @@ pub struct PkgOverride {

/// Package signing configuration
pub signing: Option<PkgSigning>,

#[serde(skip)]
pub ignore_disabled_strategies: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
19 changes: 19 additions & 0 deletions e2e-tests/manifests/strategies-test-Cargo2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "cargo-update"
repository = "https://github.com/nabijaczleweli/cargo-update"
version = "11.1.2"

[[bin]]
name = "cargo-install-update"
path = "src/main.rs"
test = false
doc = false

[[bin]]
name = "cargo-install-update-config"
path = "src/main-config.rs"
test = false
doc = false

[package.metadata.binstall]
disabled-strategies = ["quick-install"]
15 changes: 11 additions & 4 deletions e2e-tests/strategies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ if [ "$exit_code" != 94 ]; then
exit 1
fi

set -euxo pipefail
set +e

"./$1" binstall --disable-strategies compile --no-confirm --manifest-path "manifests/strategies-test-Cargo2.toml" [email protected]
exit_code="$?"

set -e

if [ "$exit_code" != 94 ]; then
echo "Expected exit code 94, but actual exit code $exit_code"
exit 1
fi

## Test --strategies overriding `disabled-strategies=["compile"]` in Cargo.toml
"./$1" binstall --no-confirm --manifest-path "manifests/strategies-test-override-Cargo.toml" --strategies compile [email protected]

## Test --disable-strategies overriding `disabled-strategies=["compile"]` in Cargo.toml
"./$1" binstall --no-confirm --manifest-path "manifests/strategies-test-override-Cargo.toml" --disable-strategies crate-meta-data,quick-install --force [email protected]

0 comments on commit b854f3f

Please sign in to comment.