Skip to content

Commit

Permalink
Add huak-toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Oct 31, 2023
1 parent 5c21339 commit 5651f46
Show file tree
Hide file tree
Showing 36 changed files with 747 additions and 19 deletions.
11 changes: 11 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 crates/huak-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ colored.workspace = true
huak-home = { path = "../huak-home" }
huak-package-manager = { path = "../huak-package-manager"}
huak-python-manager = { path = "../huak-python-manager" }
huak-toolchain = { path = "../huak-toolchain" }
human-panic.workspace = true
# included to build PyPi Wheels (see .github/workflow/README.md)
openssl = { version = "0.10.57", features = ["vendored"], optional = true }
Expand Down
90 changes: 79 additions & 11 deletions crates/huak-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use huak_package_manager::{
Verbosity, WorkspaceOptions,
};
use huak_python_manager::RequestedVersion;
use huak_toolchain::{Channel, Tool};
use std::{env::current_dir, path::PathBuf, process::ExitCode, str::FromStr};
use termcolor::ColorChoice;

Expand Down Expand Up @@ -156,6 +157,12 @@ enum Commands {
#[arg(last = true)]
trailing: Option<Vec<String>>,
},
/// Manage toolchains.
#[clap(alias = "tc")]
Toolchain {
#[command(subcommand)]
command: Toolchain,
},
/// Update the project's dependencies.
Update {
#[arg(num_args = 0..)]
Expand Down Expand Up @@ -188,21 +195,66 @@ enum Python {

#[derive(Subcommand)]
enum Toolchain {
/// List available toolchains.
List,
/// Use an available toolchain.
Use {
/// The version of Python to use.
#[arg(required = true)]
version: RequestedVersion,
/// Add a tool to a toolchain.
Add {
/// A tool to add.
tool: Tool,
/// Add a tool to a specific channel.
#[arg(long, required = false)]
channel: Option<Channel>,
},
/// Display information about a toolchain.
Info {
/// The toolchain channel to display information for.
#[arg(required = false)]
channel: Option<Channel>,
},
/// Install a toolchain.
Install {
/// The version of Python to install.
#[arg(required = true)]
version: RequestedVersion,
/// The toolchain channel to install.
#[arg(required = false)]
channel: Option<Channel>,
/// The path to install a toolchain to.
target: PathBuf,
#[arg(required = false)]
target: Option<PathBuf>, // TODO(cnpryer): Could default to home dir toolchains dir.
},
/// List available toolchains.
List,
/// Remove a tool from a toolchain.
Remove {
/// A tool to add.
tool: Tool,
/// Remove a tool from a specific channel.
#[arg(long, required = false)]
channel: Option<Channel>,
},
/// Run a tool installed to a toolchain.
Run {
/// The tool to run.
tool: Tool,
/// The toolchain channel to run a tool from.
#[arg(long, required = false)]
channel: Option<Channel>,
},
/// Uninstall a toolchain.
Uninstall {
/// The toolchain channel to uninstall.
#[arg(required = false)]
channel: Option<Channel>,
},
/// Update the current toolchain.
Update {
/// A tool to update.
#[arg(required = false)]
tool: Option<Tool>, // TODO(cnpryer): Either include @version or add version arg.
/// The toolchain channel to update.
#[arg(long, required = false)]
channel: Option<Channel>,
},
/// Use an available toolchain.
Use {
/// The toolchain channel to use.
channel: Channel,
},
}

Expand Down Expand Up @@ -345,6 +397,7 @@ fn exec_command(cmd: Commands, config: &mut Config) -> HuakResult<()> {
};
test(config, &options)
}
Commands::Toolchain { command } => toolchain(command, config),
Commands::Update {
dependencies,
trailing,
Expand Down Expand Up @@ -375,6 +428,7 @@ fn get_config(cwd: PathBuf, cli: &Cli) -> Config {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
if cli.no_color {
config.terminal_options = TerminalOptions {
Expand Down Expand Up @@ -474,6 +528,20 @@ fn test(config: &Config, options: &TestOptions) -> HuakResult<()> {
ops::test_project(config, options)
}

fn toolchain(command: Toolchain, config: &Config) -> HuakResult<()> {
match command {
Toolchain::Add { tool, channel } => ops::add_to_toolchain(tool, channel, config),
Toolchain::Info { channel } => ops::display_toolchain_info(channel, config),
Toolchain::Install { channel, target } => ops::install_toolchain(channel, target, config),
Toolchain::List => ops::list_toolchains(config),
Toolchain::Remove { tool, channel } => ops::remove_from_toolchain(tool, channel, config),
Toolchain::Run { tool, channel } => ops::run_from_toolchain(tool, channel, config),
Toolchain::Uninstall { channel } => ops::uninstall_toolchain(channel, config),
Toolchain::Update { tool, channel } => ops::update_toolchain(tool, channel, config),
Toolchain::Use { channel } => ops::use_toolchain(channel, config),
}
}

fn update(
dependencies: Option<Vec<String>>,
config: &Config,
Expand Down
6 changes: 6 additions & 0 deletions crates/huak-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod error;
#[must_use]
pub fn main() -> ExitCode {
setup_panic!();
setup_home();

match Cli::parse().run() {
Ok(0) => ExitCode::SUCCESS,
Expand All @@ -33,3 +34,8 @@ pub fn main() -> ExitCode {
}
}
}

// TODO(cnpryer): On install
fn setup_home() {
todo!()
}
1 change: 1 addition & 0 deletions crates/huak-package-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ toml_edit = "0.19.4"
regex = "1.9.5"
huak-python-manager = { path = "../huak-python-manager" }
huak-home = { path = "../huak-home" }
huak-toolchain = { path = "../huak-toolchain" }

[dev-dependencies]
huak-dev = { path = "../huak-dev" }
Expand Down
18 changes: 17 additions & 1 deletion crates/huak-package-manager/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::PathBuf;

use huak_home::huak_home_dir;

use crate::{sys::Terminal, workspace::Workspace, TerminalOptions};

/// The main `Config` for Huak.
Expand All @@ -21,14 +23,16 @@ use crate::{sys::Terminal, workspace::Workspace, TerminalOptions};
///
/// let workspace = config.workspace();
/// ```
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct Config {
/// The configured `Workspace` root path.
pub workspace_root: PathBuf,
/// The current working directory.
pub cwd: PathBuf,
/// `Terminal` options to use.
pub terminal_options: TerminalOptions,
/// Huak's home directory.
pub home: Option<PathBuf>,
}

impl Config {
Expand All @@ -51,6 +55,18 @@ impl Config {
workspace_root: self.workspace_root,
cwd: self.cwd,
terminal_options,
..Default::default()
}
}
}

impl Default for Config {
fn default() -> Self {
Self {
workspace_root: Default::default(),
cwd: Default::default(),
terminal_options: Default::default(),
home: huak_home_dir(),
}
}
}
10 changes: 10 additions & 0 deletions crates/huak-package-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use thiserror::Error as ThisError;

pub type HuakResult<T> = Result<T, Error>;

// TODO(cnpryer): If errors are given "a problem..." prompts there could be redundancy in messages.
// These prompts feel more like application experience than library needs.
#[derive(ThisError, Debug)]
pub enum Error {
#[error("a problem with argument parsing occurred: {0}")]
Expand All @@ -22,6 +24,12 @@ pub enum Error {
HuakConfigurationError(String),
#[error("a problem occurred resolving huak's home directory")]
HuakHomeNotFound,
#[error("a toolchain cannot be found")]
HuakToolchainNotFound,
#[error("{0}")] // See TODO note above.
HuakToolchainError(#[from] huak_toolchain::Error),
#[error("a toolchain already exists: {0}")]
HuakToolchainExistsError(PathBuf),
#[error("a problem with huak's internals occurred: {0}")]
InternalError(String),
#[error("a version number could not be parsed: {0}")]
Expand Down Expand Up @@ -58,6 +66,8 @@ pub enum Error {
TOMLDeserializationError(#[from] toml::de::Error),
#[error("a problem with toml serialization occurred {0}")]
TOMLSerializationError(#[from] toml::ser::Error),
#[error("{0}")]
TOMLEditError(#[from] toml_edit::TomlError),
#[error("a problem with toml deserialization occurred: {0}")]
TOMLEditDeserializationError(#[from] toml_edit::de::Error),
#[error("a problem with toml serialization occurred {0}")]
Expand Down
5 changes: 5 additions & 0 deletions crates/huak-package-manager/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub fn last_path_component<T: Into<PathBuf>>(path: T) -> HuakResult<String> {
Ok(path)
}

/// A helper for determining if an &str is a valid path.
pub(crate) fn is_path(value: &str) -> bool {
PathBuf::from(value).exists()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ mod metadata;
pub mod ops;
mod package;
mod python_environment;
mod settings;
mod sys;
mod toolchain;
mod version;
mod workspace;

Expand Down
4 changes: 4 additions & 0 deletions crates/huak-package-manager/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ impl Metadata {
.entry(name.to_string())
.or_insert(entrypoint.to_string());
}

pub fn tool(&self) -> Option<&Table> {
self.tool.as_ref()
}
}

impl Default for Metadata {
Expand Down
2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/ops/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
let venv = ws.resolve_python_environment().unwrap();
Expand Down Expand Up @@ -162,6 +163,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/huak-package-manager/src/ops/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/huak-package-manager/src/ops/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let options = CleanOptions {
include_pycache: true,
Expand Down
1 change: 1 addition & 0 deletions crates/huak-package-manager/src/ops/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/ops/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let options = WorkspaceOptions { uses_git: false };
init_lib_project(&config, &options).unwrap();
Expand Down Expand Up @@ -84,6 +85,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let options = WorkspaceOptions { uses_git: false };

Expand Down
2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/ops/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down Expand Up @@ -118,6 +119,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/ops/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let options = LintOptions {
values: None,
Expand Down Expand Up @@ -134,6 +135,7 @@ mod tests {
workspace_root,
cwd,
terminal_options,
..Default::default()
};
let ws = config.workspace();
initialize_venv(ws.root().join(".venv"), &ws.environment()).unwrap();
Expand Down
Loading

0 comments on commit 5651f46

Please sign in to comment.