Skip to content

Commit

Permalink
Allow toolchain channel configuration from pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Nov 21, 2023
1 parent 3ccf15f commit 149ee1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
47 changes: 26 additions & 21 deletions crates/huak-package-manager/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::{
};
use huak_toolchain::{Channel, LocalToolchain, LocalToolchainResolver, SettingsDb};
use huak_workspace::{resolve_first, PathMarker};
use std::str::FromStr;
use std::{path::PathBuf, process::Command};
use toml_edit::Item;

/// The `Workspace` is a struct for resolving things like the current `Package`
/// or the current `PythonEnvironment`. It can also provide a snapshot of the `Environment`,
Expand Down Expand Up @@ -91,10 +93,13 @@ impl Workspace {
/// Get the current `PythonEnvironment`. The current `PythonEnvironment` is one
/// found by its configuration file or `Interpreter` nearest baseed on `Config` data.
pub fn current_python_environment(&self) -> HuakResult<PythonEnvironment> {
let path = find_venv_root(&self.config.cwd, &self.root)?;
let env = PythonEnvironment::new(path)?;
let toolchain = self.resolve_local_toolchain(None);
let path = toolchain
.map(|it| it.root().join(".venv"))
.or(find_venv_root(&self.config.cwd, &self.root))?;
let py_env = PythonEnvironment::new(path)?;

Ok(env)
Ok(py_env)
}

/// Create a `PythonEnvironment` for the `Workspace`.
Expand Down Expand Up @@ -211,28 +216,28 @@ fn resolve_local_toolchain(
}

// If a channel is provided then search for it from huak's toolchain directory.
if let Some(channel) = channel.as_ref() {
if let Some(channel) = channel {
let resolver = LocalToolchainResolver::new();
return resolver.from_dir(channel, toolchains);
}

// Use workspace project manifest and return if a toolchain is listed.
if let Ok(manifest) = workspace.current_local_manifest() {
if let Some(table) = manifest
.manifest_data()
.tool_table()
.and_then(|it| it.get("huak"))
{
if let Some(path) = table
.get("toolchain")
.map(std::string::ToString::to_string)
.map(PathBuf::from)
{
if path.exists() {
return Some(LocalToolchain::new(path));
}
};
};
// Use workspace project manifest and return if a toolchain is listed. TODO(cnpryer): May not be channel
if let Some(manifest_channel) = workspace
.current_local_manifest()
.map(|it| {
it.manifest_data()
.tool_table()
.and_then(|tool| tool.get("huak"))
.and_then(Item::as_table)
.and_then(|table| table.get("toolchain"))
.and_then(Item::as_str) // TODO(cnpryer): Support non-string toolchain values
.map(ToString::to_string)
})
.unwrap_or_default()
.and_then(|s| Channel::from_str(s.as_str()).ok())
.as_ref()
{
return resolve_local_toolchain(workspace, Some(manifest_channel));
};

// Attempt to retrieve the toolchain for the current workspace scope by resolving for
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ requires = ["maturin>=0.14,<0.15"]
build-backend = "maturin"

[tool.huak]
toolchain = "3.11"

0 comments on commit 149ee1d

Please sign in to comment.