From f1abe7eb2f6098b7aefa8b68d6192fc2e2a99f59 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 29 Jan 2024 08:43:30 +0100 Subject: [PATCH 1/2] Ask during installation about shims --- rye/src/cli/publish.rs | 6 +++--- rye/src/cli/rye.rs | 30 +++++++++++++++++++++++++++--- rye/src/utils/mod.rs | 7 +++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/rye/src/cli/publish.rs b/rye/src/cli/publish.rs index 0972e51c52..45bc4756e2 100644 --- a/rye/src/cli/publish.rs +++ b/rye/src/cli/publish.rs @@ -14,7 +14,7 @@ use url::Url; use crate::bootstrap::ensure_self_venv; use crate::platform::{get_credentials, write_credentials}; use crate::pyproject::PyProject; -use crate::utils::{escape_string, get_venv_python_bin, CommandOutput}; +use crate::utils::{escape_string, get_venv_python_bin, tui_theme, CommandOutput}; /// Publish packages to a package repository. #[derive(Parser, Debug)] @@ -190,7 +190,7 @@ fn prompt_for_token() -> Result { fn maybe_encrypt(secret: &Secret, yes: bool) -> Result>, Error> { let phrase = if !yes { - dialoguer::Password::new() + dialoguer::Password::with_theme(tui_theme()) .with_prompt("Encrypt with passphrase (optional)") .allow_empty_password(true) .report(false) @@ -218,7 +218,7 @@ fn maybe_encrypt(secret: &Secret, yes: bool) -> Result>, fn maybe_decrypt(secret: &Secret, yes: bool) -> Result, Error> { let phrase = if !yes { - dialoguer::Password::new() + dialoguer::Password::with_theme(tui_theme()) .with_prompt("Decrypt with passphrase (optional)") .allow_empty_password(true) .report(false) diff --git a/rye/src/cli/rye.rs b/rye/src/cli/rye.rs index 659c0844ce..3ac9d9866a 100644 --- a/rye/src/cli/rye.rs +++ b/rye/src/cli/rye.rs @@ -3,6 +3,7 @@ use std::env::consts::{ARCH, EXE_EXTENSION, OS}; use std::env::{join_paths, split_paths}; use std::path::{Path, PathBuf}; use std::process::Command; +use std::sync::Arc; use std::{env, fs}; use anyhow::{bail, Context, Error}; @@ -18,8 +19,9 @@ use crate::bootstrap::{ update_core_shims, }; use crate::cli::toolchain::register_toolchain; +use crate::config::Config; use crate::platform::{get_app_dir, symlinks_supported}; -use crate::utils::{check_checksum, CommandOutput, QuietExit}; +use crate::utils::{check_checksum, tui_theme, CommandOutput, QuietExit}; #[cfg(windows)] const DEFAULT_HOME: &str = "%USERPROFILE%\\.rye"; @@ -268,7 +270,7 @@ fn remove_dir_all_if_exists(path: &Path) -> Result<(), Error> { fn uninstall(args: UninstallCommand) -> Result<(), Error> { if !args.yes - && !dialoguer::Confirm::new() + && !dialoguer::Confirm::with_theme(tui_theme()) .with_prompt("Do you want to uninstall rye?") .interact()? { @@ -341,6 +343,8 @@ fn is_fish() -> bool { } fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<(), Error> { + let mut config = Config::current(); + let config_doc = Arc::make_mut(&mut config).doc_mut(); let exe = env::current_exe()?; let app_dir = get_app_dir(); let shims = app_dir.join("shims"); @@ -386,7 +390,7 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<( echo!(); if !matches!(mode, InstallMode::NoPrompts) - && !dialoguer::Confirm::new() + && !dialoguer::Confirm::with_theme(tui_theme()) .with_prompt("Continue?") .interact()? { @@ -442,6 +446,24 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<( style(self_path.display()).cyan() ); + // If the global-python flag is not in the settings, ask the user if they want to turn + // on global shims upon installation. + if config_doc + .get("behavior") + .and_then(|x| x.get("global-python")) + .is_none() + && (matches!(mode, InstallMode::NoPrompts) + || dialoguer::Select::with_theme(tui_theme()) + .with_prompt("Determine Rye's python Shim behavior outside of Rye managed projects") + .item("Make Rye's own Python distribution available") + .item("Transparently pass through to non Rye (system, pyenv, etc.) Python") + .default(0) + .interact()? + == 0) + { + config_doc.as_item_mut()["behavior"]["global-python"] = toml_edit::value(true); + } + #[cfg(unix)] { if !env::split_paths(&env::var_os("PATH").unwrap()) @@ -478,6 +500,8 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<( echo!(); echo!("{}", style("All done!").green()); + config.save()?; + Ok(()) } diff --git a/rye/src/utils/mod.rs b/rye/src/utils/mod.rs index 57fd275e0e..0388fc4438 100644 --- a/rye/src/utils/mod.rs +++ b/rye/src/utils/mod.rs @@ -6,6 +6,7 @@ use std::process::{Command, ExitStatus, Stdio}; use std::{fmt, fs}; use anyhow::{anyhow, bail, Error}; +use dialoguer::theme::{ColorfulTheme, Theme}; use once_cell::sync::Lazy; use pep508_rs::{Requirement, VersionOrUrl}; use regex::{Captures, Regex}; @@ -22,6 +23,12 @@ pub use std::os::windows::fs::symlink_file; use crate::config::Config; use crate::consts::VENV_BIN; +/// Returns the preferred theme for dialoguer +pub fn tui_theme() -> &'static dyn Theme { + static THEME: Lazy = Lazy::new(ColorfulTheme::default); + Lazy::force(&THEME) as &dyn Theme +} + #[cfg(windows)] pub(crate) mod windows; From cee7ad08beafe15eade9e9767bcc019a69491e2f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 29 Jan 2024 08:45:23 +0100 Subject: [PATCH 2/2] Move prompt up --- rye/src/cli/rye.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/rye/src/cli/rye.rs b/rye/src/cli/rye.rs index 3ac9d9866a..b3ed51bb49 100644 --- a/rye/src/cli/rye.rs +++ b/rye/src/cli/rye.rs @@ -398,6 +398,24 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<( return Err(QuietExit(1).into()); } + // If the global-python flag is not in the settings, ask the user if they want to turn + // on global shims upon installation. + if config_doc + .get("behavior") + .and_then(|x| x.get("global-python")) + .is_none() + && (matches!(mode, InstallMode::NoPrompts) + || dialoguer::Select::with_theme(tui_theme()) + .with_prompt("Determine Rye's python Shim behavior outside of Rye managed projects") + .item("Make Rye's own Python distribution available") + .item("Transparently pass through to non Rye (system, pyenv, etc.) Python") + .default(0) + .interact()? + == 0) + { + config_doc.as_item_mut()["behavior"]["global-python"] = toml_edit::value(true); + } + // place executable in rye home folder fs::create_dir_all(&shims).ok(); if target.is_file() { @@ -446,24 +464,6 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<( style(self_path.display()).cyan() ); - // If the global-python flag is not in the settings, ask the user if they want to turn - // on global shims upon installation. - if config_doc - .get("behavior") - .and_then(|x| x.get("global-python")) - .is_none() - && (matches!(mode, InstallMode::NoPrompts) - || dialoguer::Select::with_theme(tui_theme()) - .with_prompt("Determine Rye's python Shim behavior outside of Rye managed projects") - .item("Make Rye's own Python distribution available") - .item("Transparently pass through to non Rye (system, pyenv, etc.) Python") - .default(0) - .interact()? - == 0) - { - config_doc.as_item_mut()["behavior"]["global-python"] = toml_edit::value(true); - } - #[cfg(unix)] { if !env::split_paths(&env::var_os("PATH").unwrap())