Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the installation wizard to prompt about global shims #566

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions rye/src/cli/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -190,7 +190,7 @@ fn prompt_for_token() -> Result<String, Error> {

fn maybe_encrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<Vec<u8>>, 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)
Expand Down Expand Up @@ -218,7 +218,7 @@ fn maybe_encrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<Vec<u8>>,

fn maybe_decrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<String>, 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)
Expand Down
30 changes: 27 additions & 3 deletions rye/src/cli/rye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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";
Expand Down Expand Up @@ -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()?
{
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -386,14 +390,32 @@ 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()?
{
elog!("Installation cancelled!");
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() {
Expand Down Expand Up @@ -478,6 +500,8 @@ fn perform_install(mode: InstallMode, toolchain_path: Option<&Path>) -> Result<(
echo!();
echo!("{}", style("All done!").green());

config.save()?;

Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions rye/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<ColorfulTheme> = Lazy::new(ColorfulTheme::default);
Lazy::force(&THEME) as &dyn Theme
}

#[cfg(windows)]
pub(crate) mod windows;

Expand Down