Skip to content

Commit

Permalink
Propagate trailing command to operations
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Mar 21, 2023
1 parent 90e3387 commit 5ef9c4f
Showing 1 changed file with 58 additions and 45 deletions.
103 changes: 58 additions & 45 deletions src/huak/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
error::HuakResult,
find_venv_root, git,
sys::{self, get_shell_name, Terminal, Verbosity},
sys::{self, Terminal, Verbosity},
Error, Installer, Package, Project, ProjectType, PyProjectToml,
VirtualEnvironment,
};
Expand All @@ -12,6 +12,7 @@ use std::{path::PathBuf, process::Command, str::FromStr};
#[derive(Default)]
pub struct OperationConfig {
pub workspace_root: PathBuf,
pub trailing_command_parts: Option<Vec<String>>,
pub project_options: Option<ProjectOptions>,
pub build_options: Option<BuildOptions>,
pub format_options: Option<FormatOptions>,
Expand Down Expand Up @@ -106,15 +107,17 @@ pub fn build_project(config: &OperationConfig) -> HuakResult<()> {
let mut terminal = Terminal::new();
terminal.set_verbosity(Verbosity::Quiet);
venv.install_packages(&[Package::from_str("build")?], &mut terminal)?;
let mut cmd = Command::new(get_shell_name()?);
let cmd = command_with_venv_context(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
let cmd = cmd.arg("/C");
// TODO: Propagate CLI config
let mut cmd = Command::new(venv.python_path());
let cmd = command_with_venv_env(&mut cmd, &venv)?;
let cmd = cmd
.arg("python -m build")
.arg("-m")
.arg("build")
.args(
config
.trailing_command_parts
.as_ref()
.unwrap_or(&Vec::new()),
)
.current_dir(&config.workspace_root);
terminal.run_command(cmd)
}
Expand Down Expand Up @@ -177,15 +180,18 @@ pub fn format_project(config: &OperationConfig) -> HuakResult<()> {
let mut terminal = Terminal::new();
terminal.set_verbosity(Verbosity::Quiet);
venv.install_packages(&[Package::from_str("black")?], &mut terminal)?;
let mut cmd = Command::new(get_shell_name()?);
let cmd = command_with_venv_context(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
let cmd = cmd.arg("/C");
// TODO: Propagate CLI config
let mut cmd = Command::new(venv.python_path());
let cmd = command_with_venv_env(&mut cmd, &venv)?;
let cmd = cmd
.arg("python -m black .")
.arg("-m")
.arg("black")
.arg(".")
.args(
config
.trailing_command_parts
.as_ref()
.unwrap_or(&Vec::new()),
)
.current_dir(&config.workspace_root);
terminal.run_command(cmd)
}
Expand Down Expand Up @@ -252,15 +258,18 @@ pub fn lint_project(config: &OperationConfig) -> HuakResult<()> {
let mut terminal = Terminal::new();
terminal.set_verbosity(Verbosity::Quiet);
venv.install_packages(&[Package::from_str("ruff")?], &mut terminal)?;
let mut cmd = Command::new(get_shell_name()?);
let cmd = command_with_venv_context(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
let cmd = cmd.arg("/C");
// TODO: Propagate CLI config (including --fix)
let mut cmd = Command::new(venv.python_path());
let cmd = command_with_venv_env(&mut cmd, &venv)?;
let cmd = cmd
.arg("python -m ruff .")
.arg("-m")
.arg("ruff")
.arg(".")
.args(
config
.trailing_command_parts
.as_ref()
.unwrap_or(&Vec::new()),
)
.current_dir(&config.workspace_root);
terminal.run_command(cmd)
}
Expand Down Expand Up @@ -295,15 +304,19 @@ pub fn publish_project(config: &OperationConfig) -> HuakResult<()> {
let mut terminal = Terminal::new();
terminal.set_verbosity(Verbosity::Quiet);
venv.install_packages(&[Package::from_str("twine")?], &mut terminal)?;
let mut cmd = Command::new(get_shell_name()?);
let cmd = command_with_venv_context(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
let cmd = cmd.arg("/C");
// TODO: Propagate CLI config
let mut cmd = Command::new(venv.python_path());
let cmd = command_with_venv_env(&mut cmd, &venv)?;
let cmd = cmd
.arg("python -m twine upload dist/*")
.arg("-m")
.arg("twine")
.arg("upload")
.arg("dist/*")
.args(
config
.trailing_command_parts
.as_ref()
.unwrap_or(&Vec::new()),
)
.current_dir(&config.workspace_root);
terminal.run_command(cmd)
}
Expand Down Expand Up @@ -355,8 +368,8 @@ pub fn run_command_str(
if let Some(options) = config.terminal_options.as_ref() {
terminal.set_verbosity(options.verbosity);
}
let mut cmd = Command::new(get_shell_name()?);
let mut cmd = command_with_venv_context(&mut cmd, &venv)?;
let mut cmd = Command::new(venv.python_path());
let mut cmd = command_with_venv_env(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
Expand All @@ -371,14 +384,14 @@ pub fn test_project(config: &OperationConfig) -> HuakResult<()> {
let mut terminal = Terminal::new();
terminal.set_verbosity(Verbosity::Quiet);
venv.install_packages(&[Package::from_str("pytest")?], &mut terminal)?;
let mut cmd = Command::new(get_shell_name()?);
let mut cmd = command_with_venv_context(&mut cmd, &venv)?;
#[cfg(unix)]
let cmd = cmd.arg("-c");
#[cfg(windows)]
let cmd = cmd.arg("/C");
// TODO: Propagate CLI config
let cmd = cmd.arg("python -m pytest");
let mut cmd = Command::new(venv.python_path());
let mut cmd = command_with_venv_env(&mut cmd, &venv)?;
let cmd = cmd.arg("-m").arg("pytest").args(
config
.trailing_command_parts
.as_ref()
.unwrap_or(&Vec::new()),
);
terminal.run_command(cmd)
}

Expand All @@ -402,7 +415,7 @@ pub fn display_project_version(config: &OperationConfig) -> HuakResult<()> {
}

/// Modify a process::Command to use a virtual environment's context.
fn command_with_venv_context<'a>(
fn command_with_venv_env<'a>(
cmd: &'a mut Command,
venv: &VirtualEnvironment,
) -> HuakResult<&'a mut Command> {
Expand Down Expand Up @@ -733,6 +746,7 @@ def fn( ):
.unwrap();
let config = OperationConfig {
workspace_root: dir.join("mock-project"),
trailing_command_parts: Some(vec!["--fix".to_string()]),
..Default::default()
};
let project = Project::from_manifest(
Expand All @@ -759,7 +773,6 @@ def fn():
"#;
std::fs::write(&lint_fix_filepath, pre_fix_str).unwrap();

// TODO: Add --fix to LintOptions
lint_project(&config).unwrap();

let post_fix_str = std::fs::read_to_string(&lint_fix_filepath).unwrap();
Expand Down

0 comments on commit 5ef9c4f

Please sign in to comment.