Skip to content

Commit

Permalink
Implement Huak's task table
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Nov 20, 2023
1 parent 85636b9 commit 2a59e6d
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Commands:
publish Builds and uploads current project to a registry
python Manage Python installations
remove Remove dependencies from the project
run Run a command within the project's environment context
run Run a command with Huak
test Test the project's Python code
update Update the project's dependencies
version Display the version of the project
Expand Down
49 changes: 45 additions & 4 deletions crates/huak-package-manager/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{ffi::OsString, path::PathBuf};
use std::{
collections::HashMap,
env::Vars,
ffi::OsString,
path::{Path, PathBuf},
};

use crate::python_environment::{
parse_python_version_from_command, python_paths, Interpreter, Interpreters,
Expand All @@ -19,15 +24,51 @@ use crate::python_environment::{
pub struct Environment {
/// Python `Interpreters` installed on the system.
interpreters: Interpreters,
/// Environment variables
vars: HashMap<String, String>,
}

impl Environment {
/// Initialize an `Environment`.
#[must_use]
pub fn new() -> Environment {
let interpreters = Environment::resolve_python_interpreters();
pub fn new() -> Self {
let interpreters = Self::resolve_python_interpreters();

Self {
interpreters,
vars: HashMap::new(),
}
}

#[must_use]
pub fn vars(&self) -> Vec<(&String, &String)> {
self.vars.iter().collect()
}

pub fn with_vars(&mut self, vars: Vars) -> &mut Self {
for (k, v) in vars {
self.vars.insert(k, v);
}

self
}

pub fn with_path<T: AsRef<Path>>(&mut self, path: T) -> &mut Self {
// Update the PATH environment variable
if let Some(curr) = self.vars.get("PATH") {
let paths = std::env::split_paths(&curr)
.chain([path.as_ref().to_path_buf()])
.collect::<Vec<_>>();
let value = std::env::join_paths(paths)
.map(|it| it.to_string_lossy().to_string())
.expect("Failed to join paths");
self.vars.insert("PATH".into(), value);
} else {
self.vars
.insert("PATH".into(), path.as_ref().to_string_lossy().to_string());
}

Environment { interpreters }
self
}

/// Get an `Iterator` over the Python `Interpreter` `PathBuf`s found.
Expand Down
4 changes: 4 additions & 0 deletions crates/huak-package-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub enum Error {
InternalError(String),
#[error("a checksum is invalid: {0}")]
InvalidChecksum(String),
#[error("a program is invalid: {0}")]
InvalidProgram(String),
#[error("a run command is invalid: {0}")]
InvalidRunCommand(String),
#[error("a version number could not be parsed: {0}")]
InvalidVersionString(String),
#[error("a problem occurred with json deserialization: {0}")]
Expand Down
2 changes: 1 addition & 1 deletion crates/huak-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! lish Builds and uploads current project to a registry
//! python Manage Python installations
//! remove Remove dependencies from the project
//! run Run a command within the project's environment context
//! run Run a command with Huak
//! test Test the project's Python code
//! update Update the project's dependencies
//! version Display the version of the project
Expand Down
16 changes: 15 additions & 1 deletion crates/huak-package-manager/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ dev = [
"black ==22.8.0",
"isort ==5.12.0",
]
"#
[tool.huak.task]
hello-world = "python -c 'print(\"hello, world.\")'""#
);
}

Expand Down Expand Up @@ -238,6 +240,9 @@ dev = [
"pytest == 7.4.3",
"ruff",
]
[tool.huak.task]
hello-world = "python -c 'print(\"hello, world.\")'"
"#
);
}
Expand Down Expand Up @@ -282,6 +287,9 @@ dev = [
new-group = [
"test2",
]
[tool.huak.task]
hello-world = "python -c 'print(\"hello, world.\")'"
"#
);
}
Expand Down Expand Up @@ -317,6 +325,9 @@ dev = [
"pytest == 7.4.3",
"ruff",
]
[tool.huak.task]
hello-world = "python -c 'print(\"hello, world.\")'"
"#
);
}
Expand Down Expand Up @@ -353,6 +364,9 @@ email = "[email protected]"
dev = [
"pytest == 7.4.3",
]
[tool.huak.task]
hello-world = "python -c 'print(\"hello, world.\")'"
"#
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/huak-package-manager/src/ops/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::make_venv_command;
use super::add_venv_to_command;
use crate::{Config, Dependency, HuakResult, InstallOptions};
use std::{process::Command, str::FromStr};

Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn build_project(config: &Config, options: &BuildOptions) -> HuakResult<()>
if let Some(it) = options.values.as_ref() {
args.extend(it.iter().map(std::string::String::as_str));
}
make_venv_command(&mut cmd, &python_env)?;
add_venv_to_command(&mut cmd, &python_env)?;
cmd.args(args).current_dir(workspace.root());

config.terminal().run_command(&mut cmd)
Expand Down
7 changes: 4 additions & 3 deletions crates/huak-package-manager/src/ops/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::make_venv_command;
use crate::{Config, Dependency, HuakResult, InstallOptions};
use std::{process::Command, str::FromStr};

use super::add_venv_to_command;

pub struct FormatOptions {
/// A values vector of format options typically used for passing on arguments.
pub values: Option<Vec<String>>,
Expand Down Expand Up @@ -56,8 +57,8 @@ pub fn format_project(config: &Config, options: &FormatOptions) -> HuakResult<()
let mut cmd = Command::new(python_env.python_path());
let mut ruff_cmd = Command::new(python_env.python_path());
let mut ruff_args = vec!["-m", "ruff", "check", ".", "--select", "I", "--fix"];
make_venv_command(&mut cmd, &python_env)?;
make_venv_command(&mut ruff_cmd, &python_env)?;
add_venv_to_command(&mut cmd, &python_env)?;
add_venv_to_command(&mut ruff_cmd, &python_env)?;
let mut args = vec!["-m", "ruff", "format", "."];
if let Some(v) = options.values.as_ref() {
args.extend(v.iter().map(String::as_str));
Expand Down
6 changes: 3 additions & 3 deletions crates/huak-package-manager/src/ops/lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::make_venv_command;
use super::add_venv_to_command;
use crate::{Config, Dependency, HuakResult, InstallOptions};
use std::{process::Command, str::FromStr};

Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn lint_project(config: &Config, options: &LintOptions) -> HuakResult<()> {

// Run `mypy` excluding the workspace's Python environment directory.
let mut mypy_cmd = Command::new(python_env.python_path());
make_venv_command(&mut mypy_cmd, &python_env)?;
add_venv_to_command(&mut mypy_cmd, &python_env)?;
mypy_cmd
.args(vec!["-m", "mypy", ".", "--exclude", &python_env.name()?])
.current_dir(workspace.root());
Expand All @@ -48,7 +48,7 @@ pub fn lint_project(config: &Config, options: &LintOptions) -> HuakResult<()> {
if let Some(v) = options.values.as_ref() {
args.extend(v.iter().map(String::as_str));
}
make_venv_command(&mut cmd, &python_env)?;
add_venv_to_command(&mut cmd, &python_env)?;
cmd.args(args).current_dir(workspace.root());
terminal.run_command(&mut cmd)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/huak-package-manager/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if __name__ == "__main__":
/// `PATH` environment variable.
/// - Adds `VIRTUAL_ENV` environment variable to the command pointing at the virtual environment's
/// root.
fn make_venv_command(cmd: &mut Command, venv: &PythonEnvironment) -> HuakResult<()> {
fn add_venv_to_command(cmd: &mut Command, venv: &PythonEnvironment) -> HuakResult<()> {
let mut paths = env_path_values().unwrap_or_default();

paths.insert(0, venv.executables_dir_path().clone());
Expand Down
4 changes: 2 additions & 2 deletions crates/huak-package-manager/src/ops/publish.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::make_venv_command;
use super::add_venv_to_command;
use crate::{Config, Dependency, HuakResult, InstallOptions};
use std::{process::Command, str::FromStr};

Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn publish_project(config: &Config, options: &PublishOptions) -> HuakResult<
if let Some(v) = options.values.as_ref() {
args.extend(v.iter().map(String::as_str));
}
make_venv_command(&mut cmd, &python_env)?;
add_venv_to_command(&mut cmd, &python_env)?;
cmd.args(args).current_dir(workspace.root());
config.terminal().run_command(&mut cmd)
}
Loading

0 comments on commit 2a59e6d

Please sign in to comment.