From e70c8ebc011ead01cd0674192bb64d02b0c0c14c Mon Sep 17 00:00:00 2001 From: Stefanuk12 <42220813+Stefanuk12@users.noreply.github.com> Date: Sat, 19 Apr 2025 23:49:19 +0100 Subject: [PATCH] feat: add dev flag --- docs/src/content/docs/reference/cli.mdx | 1 + src/cli/commands/execute.rs | 4 +-- src/cli/commands/install.rs | 15 +++++++-- src/cli/commands/update.rs | 4 +-- src/cli/install.rs | 6 ++-- src/download_and_link.rs | 45 ++++++++++++++++++------- 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/docs/src/content/docs/reference/cli.mdx b/docs/src/content/docs/reference/cli.mdx index 8874d30..9c0aa95 100644 --- a/docs/src/content/docs/reference/cli.mdx +++ b/docs/src/content/docs/reference/cli.mdx @@ -110,6 +110,7 @@ Installs dependencies for the current project. - `--locked`: Whether to error if the lockfile is out of date. - `--prod`: Whether to not linking dev dependencies. +- `--dev`: Whether to only link dev dependencies. - `--network-concurrency `: The number of concurrent network requests to make at most. Defaults to 16. - `--force`: Whether to force reinstall all packages even if they are already diff --git a/src/cli/commands/execute.rs b/src/cli/commands/execute.rs index 030de72..3b71493 100644 --- a/src/cli/commands/execute.rs +++ b/src/cli/commands/execute.rs @@ -9,7 +9,7 @@ use console::style; use fs_err::tokio as fs; use indicatif::MultiProgress; use pesde::{ - download_and_link::DownloadAndLinkOptions, + download_and_link::{DownloadAndLinkOptions, InstallDependenciesMode}, linking::generator::generate_bin_linking_module, manifest::target::TargetKind, names::{PackageName, PackageNames}, @@ -178,7 +178,7 @@ impl ExecuteCommand { DownloadAndLinkOptions::, ()>::new(reqwest) .reporter(reporter) .refreshed_sources(refreshed_sources) - .prod(true), + .install_dependencies_mode(InstallDependenciesMode::Prod), ) .await .context("failed to download and link dependencies")?; diff --git a/src/cli/commands/install.rs b/src/cli/commands/install.rs index c307b35..c811fb3 100644 --- a/src/cli/commands/install.rs +++ b/src/cli/commands/install.rs @@ -3,7 +3,7 @@ use crate::cli::{ run_on_workspace_members, }; use clap::Args; -use pesde::Project; +use pesde::{download_and_link::InstallDependenciesMode, Project}; use std::num::NonZeroUsize; #[derive(Debug, Args, Copy, Clone)] @@ -16,6 +16,10 @@ pub struct InstallCommand { #[arg(long)] prod: bool, + /// Whether to only install dev dependencies + #[arg(long)] + dev: bool, + /// The maximum number of concurrent network requests #[arg(long, default_value = "16")] network_concurrency: NonZeroUsize, @@ -30,9 +34,16 @@ pub struct InstallCommand { struct CallbackError(#[from] anyhow::Error); impl InstallCommand { pub async fn run(self, project: Project, reqwest: reqwest::Client) -> anyhow::Result<()> { + let install_dependencies_mode = match (self.prod, self.dev) { + (true, true) => anyhow::bail!("cannot have both prod and dev flags enabled"), + (true, false) => InstallDependenciesMode::Prod, + (false, true) => InstallDependenciesMode::Dev, + (false, false) => InstallDependenciesMode::All, + }; + let options = InstallOptions { locked: self.locked, - prod: self.prod, + install_dependencies_mode, write: true, network_concurrency: self.network_concurrency, use_lockfile: true, diff --git a/src/cli/commands/update.rs b/src/cli/commands/update.rs index ffb065c..7bf9473 100644 --- a/src/cli/commands/update.rs +++ b/src/cli/commands/update.rs @@ -3,7 +3,7 @@ use crate::cli::{ run_on_workspace_members, }; use clap::Args; -use pesde::Project; +use pesde::{download_and_link::InstallDependenciesMode, Project}; use std::num::NonZeroUsize; #[derive(Debug, Args, Copy, Clone)] @@ -25,7 +25,7 @@ impl UpdateCommand { pub async fn run(self, project: Project, reqwest: reqwest::Client) -> anyhow::Result<()> { let options = InstallOptions { locked: false, - prod: false, + install_dependencies_mode: InstallDependenciesMode::All, write: !self.no_install, network_concurrency: self.network_concurrency, use_lockfile: false, diff --git a/src/cli/install.rs b/src/cli/install.rs index d757525..d797c8f 100644 --- a/src/cli/install.rs +++ b/src/cli/install.rs @@ -10,7 +10,7 @@ use anyhow::Context as _; use console::style; use fs_err::tokio as fs; use pesde::{ - download_and_link::{DownloadAndLinkHooks, DownloadAndLinkOptions}, + download_and_link::{DownloadAndLinkHooks, DownloadAndLinkOptions, InstallDependenciesMode}, engine::EngineKind, graph::{DependencyGraph, DependencyGraphWithTarget}, lockfile::Lockfile, @@ -123,7 +123,7 @@ impl DownloadAndLinkHooks for InstallHooks { #[derive(Debug, Clone, Copy)] pub struct InstallOptions { pub locked: bool, - pub prod: bool, + pub install_dependencies_mode: InstallDependenciesMode, pub write: bool, pub use_lockfile: bool, pub network_concurrency: NonZeroUsize, @@ -287,7 +287,7 @@ pub async fn install( .reporter(reporter) .hooks(hooks) .refreshed_sources(refreshed_sources.clone()) - .prod(options.prod) + .install_dependencies_mode(options.install_dependencies_mode) .network_concurrency(options.network_concurrency) .force(options.force || has_irrecoverable_changes), ) diff --git a/src/download_and_link.rs b/src/download_and_link.rs index a696b2e..f045aa6 100644 --- a/src/download_and_link.rs +++ b/src/download_and_link.rs @@ -64,6 +64,18 @@ impl DownloadAndLinkHooks for () { type Error = Infallible; } +/// Options for which dependencies to install. +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] +pub enum InstallDependenciesMode { + /// Install all dependencies. + #[default] + All, + /// Install only dependencies, not dev_dependencies. + Prod, + /// Install only dev_dependencies. + Dev, +} + /// Options for downloading and linking. #[derive(Debug)] pub struct DownloadAndLinkOptions { @@ -75,8 +87,8 @@ pub struct DownloadAndLinkOptions { pub hooks: Option>, /// The refreshed sources. pub refreshed_sources: RefreshedSources, - /// Whether to skip dev dependencies. - pub prod: bool, + /// Which dependencies to install. + pub install_dependencies_mode: InstallDependenciesMode, /// The max number of concurrent network requests. pub network_concurrency: NonZeroUsize, /// Whether to re-install all dependencies even if they are already installed @@ -96,7 +108,7 @@ where reporter: None, hooks: None, refreshed_sources: Default::default(), - prod: false, + install_dependencies_mode: InstallDependenciesMode::All, network_concurrency: NonZeroUsize::new(16).unwrap(), force: false, } @@ -123,10 +135,13 @@ where self } - /// Sets whether to skip dev dependencies. + /// Sets which dependencies to install #[must_use] - pub fn prod(mut self, prod: bool) -> Self { - self.prod = prod; + pub fn install_dependencies_mode( + mut self, + install_dependencies: InstallDependenciesMode, + ) -> Self { + self.install_dependencies_mode = install_dependencies; self } @@ -152,7 +167,7 @@ impl Clone for DownloadAndLinkOptions { reporter: self.reporter.clone(), hooks: self.hooks.clone(), refreshed_sources: self.refreshed_sources.clone(), - prod: self.prod, + install_dependencies_mode: self.install_dependencies_mode, network_concurrency: self.network_concurrency, force: self.force, } @@ -161,7 +176,7 @@ impl Clone for DownloadAndLinkOptions { impl Project { /// Downloads a graph of dependencies and links them in the correct order - #[instrument(skip_all, fields(prod = options.prod), level = "debug")] + #[instrument(skip_all, fields(install_dependencies = debug(options.install_dependencies_mode)), level = "debug")] pub async fn download_and_link( &self, graph: &Arc, @@ -176,7 +191,7 @@ impl Project { reporter, hooks, refreshed_sources, - prod, + install_dependencies_mode, network_concurrency, force, } = options; @@ -420,11 +435,17 @@ impl Project { let mut graph = Arc::into_inner(graph).unwrap(); - if prod { - graph.retain(|_, node| node.node.resolved_ty != DependencyType::Dev); + match install_dependencies_mode { + InstallDependenciesMode::All => {} + InstallDependenciesMode::Prod => { + graph.retain(|_, node| node.node.resolved_ty != DependencyType::Dev); + } + InstallDependenciesMode::Dev => { + graph.retain(|_, node| node.node.resolved_ty == DependencyType::Dev); + } } - if prod || !force { + if install_dependencies_mode != InstallDependenciesMode::All || !force { self.remove_unused(&graph).await?; }