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

Support shell command in windows #363

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
161 changes: 159 additions & 2 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 = [] }
59 changes: 55 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,44 @@ 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) {
match process.name() {
"cmd.exe" => {
return Ok(String::from("cmd.exe"));
}
"powershell.exe" => {
return Ok(String::from("powershell.exe"));
}
"pwsh.exe" => {
return Ok(String::from("pwsh.exe"));
}
&_ => {
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 +76,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" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be determined in runtime instead of compile time, so using env::consts::OS == "windows" instead of cfg!(windows).

Maybe we will using binaries build in windows and runs on linux (wine?).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this conditional - bin vs Scripts - is also done once already in rye/src/consts.rs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks for the review!

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