Skip to content

Commit

Permalink
Support shell command in windows (#363)
Browse files Browse the repository at this point in the history

Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
  • Loading branch information
aisk and mitsuhiko committed Jul 18, 2023
1 parent f282319 commit df417ea
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ that were not yet released.

_Unreleased_

- Added support for `shell` command on Windows. #363

- Pin down pip to an older version to avoid issues with an incompatible
`pip-tools` version. This does not yet update pip-tools to 7.0 as there
are significant regressions in 7.x. #374
Expand Down
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rye/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ self-replace = "1.3.2"
configparser = "3.0.2"
monotrail-utils = { git = "https://github.com/konstin/poc-monotrail", version = "0.0.1" }
python-pkginfo = { version = "0.5.6", features = ["serde"] }
sysinfo = { version = "0.29.4", default-features = false, features = [] }

[target."cfg(windows)".dependencies]
winapi = { version = "0.3.9", default-features = false, features = [] }
51 changes: 47 additions & 4 deletions rye/src/cli/shell.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::env;
use std::path::PathBuf;
use std::process;
use std::process::Command;

use anyhow::{bail, Context, Error};
use clap::Parser;
use console::style;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

use crate::pyproject::PyProject;
use crate::sync::{sync, SyncOptions};
Expand All @@ -25,6 +27,36 @@ pub struct Args {
pyproject: Option<PathBuf>,
}

fn get_shell() -> Result<String, Error> {
let shell_env = env::var("SHELL");
if let Ok(shell) = shell_env {
return Ok(shell);
}

let mut system = System::default();
system.refresh_processes();

let mut pid = Some(Pid::from_u32(process::id()));
while let Some(p) = pid {
if let Some(process) = system.process(p) {
let name = process.name();
if let "cmd.exe" | "powershell.exe" | "pwsh.exe" = name {
return Ok(String::from(name));
} else {
pid = process.parent();
continue;
}
}
break;
}

Err(anyhow::anyhow!("don't know which shell is used"))
}

fn is_ms_shells(shell: &str) -> bool {
matches!(shell, "cmd.exe" | "powershell.exe" | "pwsh.exe")
}

pub fn execute(cmd: Args) -> Result<(), Error> {
if !cmd.allow_nested && env::var("__RYE_SHELL").ok().as_deref() == Some("1") {
bail!("cannot invoke recursive rye shell");
Expand All @@ -36,14 +68,25 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
.context("failed to sync ahead of shell")?;

let venv_path = pyproject.venv_path();
let venv_bin = venv_path.join("bin");
let venv_bin = if env::consts::OS == "windows" {
venv_path.join("Scripts")
} else {
venv_path.join("bin")
};

let mut shell = Command::new(env::var("SHELL")?);
shell.arg("-l").env("VIRTUAL_ENV", &*venv_path);
let s = get_shell()?;
let sep = if is_ms_shells(s.as_str()) { ";" } else { ":" };
let args = if !is_ms_shells(s.as_str()) {
vec!["-l"]
} else {
vec![]
};
let mut shell = Command::new(s.as_str());
shell.args(args).env("VIRTUAL_ENV", &*venv_path);

if let Some(path) = env::var_os("PATH") {
let mut new_path = venv_bin.as_os_str().to_owned();
new_path.push(":");
new_path.push(sep);
new_path.push(path);
shell.env("PATH", new_path);
} else {
Expand Down

0 comments on commit df417ea

Please sign in to comment.