Skip to content

Commit

Permalink
feat(cargo): allow specifying features via tool options (#2515)
Browse files Browse the repository at this point in the history
Fixes #1635
  • Loading branch information
jdx authored Aug 27, 2024
1 parent de5d26b commit b6c900a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
28 changes: 23 additions & 5 deletions src/backend/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::env::{self, GITHUB_TOKEN};
use crate::file;
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::toolset::ToolRequest;
use crate::toolset::{ToolRequest, ToolVersion};

#[derive(Debug)]
pub struct CargoBackend {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Backend for CargoBackend {
let install_arg = format!("{}@{}", self.name(), ctx.tv.version);

let cmd = CmdLineRunner::new("cargo").arg("install");
let cmd = if let Some(url) = self.git_url() {
let mut cmd = if let Some(url) = self.git_url() {
let mut cmd = cmd.arg(format!("--git={url}"));
if let Some(rev) = ctx.tv.version.strip_prefix("rev:") {
cmd = cmd.arg(format!("--rev={rev}"));
Expand All @@ -79,7 +79,7 @@ impl Backend for CargoBackend {
))?;
}
cmd
} else if self.is_binstall_enabled() {
} else if self.is_binstall_enabled(&ctx.tv) {
let mut cmd = CmdLineRunner::new("cargo-binstall").arg("-y");
if let Some(token) = &*GITHUB_TOKEN {
cmd = cmd.env("GITHUB_TOKEN", token)
Expand All @@ -89,6 +89,16 @@ impl Backend for CargoBackend {
cmd.arg(install_arg)
};

let opts = ctx.tv.request.options();
if let Some(features) = opts.get("features") {
cmd = cmd.arg(format!("--features={}", features));
}
if let Some(default_features) = opts.get("default-features") {
if default_features.to_lowercase() == "false" {
cmd = cmd.arg("--no-default-features");
}
}

cmd.arg("--locked")
.arg("--root")
.arg(ctx.tv.install_path())
Expand All @@ -112,9 +122,17 @@ impl CargoBackend {
}
}

fn is_binstall_enabled(&self) -> bool {
fn is_binstall_enabled(&self, tv: &ToolVersion) -> bool {
let settings = Settings::get();
settings.cargo_binstall && file::which_non_pristine("cargo-binstall").is_some()
if !settings.cargo_binstall || file::which_non_pristine("cargo-binstall").is_none() {
return false;
}
let opts = tv.request.options();
if opts.contains_key("features") || opts.contains_key("default-features") {
info!("not using cargo-binstall because features are specified");
return false;
}
true
}

/// if the name is a git repo, return the git url
Expand Down
3 changes: 1 addition & 2 deletions src/backend/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use crate::cache::CacheManager;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings};
use crate::file;
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::toolset::ToolRequest;
use crate::{env, github};
use crate::{env, file, github};

#[derive(Debug)]
pub struct PIPXBackend {
Expand Down
3 changes: 1 addition & 2 deletions src/path_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env::join_paths;
use std::env::split_paths;
use std::env::{join_paths, split_paths};
use std::ffi::OsString;
use std::fmt;
use std::fmt::{Display, Formatter};
Expand Down

0 comments on commit b6c900a

Please sign in to comment.