Skip to content

Commit

Permalink
Add new install command (#872)
Browse files Browse the repository at this point in the history
* Add new install command

* Snapshots

* Fix tests
  • Loading branch information
cnpryer authored Nov 23, 2023
1 parent 389fe05 commit f7c4c64
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 80 deletions.
18 changes: 10 additions & 8 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Commands:
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the current project
install Install a Python package (defaults to $HOME/.huak/bin)
lint Lint the project's Python code
new Create a new project at <path>
publish Builds and uploads current project to a registry
Expand Down
2 changes: 2 additions & 0 deletions crates/huak-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ huak-workspace = { path = "../huak-workspace" }
human-panic.workspace = true
# included to build PyPi Wheels (see .github/workflow/README.md)
openssl = { version = "0.10.57", features = ["vendored"], optional = true }
pep508_rs.workspace = true
termcolor.workspace = true
thiserror.workspace = true
url = "2.5.0"

[dev-dependencies]
huak-dev = { path = "../huak-dev" }
Expand Down
37 changes: 35 additions & 2 deletions crates/huak-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{self, Shell};
use huak_home::huak_home_dir;
use huak_package_manager::ops::{
self, AddOptions, BuildOptions, CleanOptions, FormatOptions, LintOptions, PublishOptions,
RemoveOptions, TestOptions, UpdateOptions,
self, install as install_op, AddOptions, BuildOptions, CleanOptions, FormatOptions,
LintOptions, PublishOptions, RemoveOptions, TestOptions, UpdateOptions,
};
use huak_package_manager::{
Config, Error as HuakError, HuakResult, InstallOptions, TerminalOptions, Verbosity,
Expand All @@ -13,8 +13,10 @@ use huak_package_manager::{
use huak_python_manager::RequestedVersion;
use huak_toolchain::{Channel, LocalTool};
use huak_workspace::{resolve_root, PathMarker};
use pep508_rs::Requirement;
use std::{env::current_dir, path::PathBuf, process::ExitCode, str::FromStr};
use termcolor::ColorChoice;
use url::Url;

/// A Python package manager written in Rust inspired by Cargo.
#[derive(Parser)]
Expand Down Expand Up @@ -113,6 +115,23 @@ enum Commands {
#[arg(last = true)]
trailing: Option<Vec<String>>,
},
/// Install a Python package (defaults to $HOME/.huak/bin).
Install {
/// The Python package to install.
#[arg(required = true)]
package: Requirement,
/// The Python version to use. TODO(cnpryer): https://github.com/cnpryer/huak/issues/850
#[arg(long, alias = "py", required = false)]
python_version: Option<RequestedVersion>,
/// The package index to use. TODO(cnpryer): Deps (document this)
#[arg(
long,
alias = "index-url",
default_value = "https://pypi.python.org/simple",
required = false
)] // TODO(cnpryer): Names
package_index_url: Url,
},
/// Lint the project's Python code.
Lint {
/// Address any fixable lints.
Expand Down Expand Up @@ -380,6 +399,11 @@ fn exec_command(cmd: Commands, config: &mut Config) -> HuakResult<()> {
&install_options,
)
}
Commands::Install {
package,
python_version,
package_index_url,
} => install(&package, python_version, &package_index_url, config),
Commands::Lint {
fix,
no_types,
Expand Down Expand Up @@ -555,6 +579,15 @@ fn init(
}
}

fn install(
package: &Requirement,
python_version: Option<RequestedVersion>,
package_index_url: &Url,
config: &Config,
) -> HuakResult<()> {
install_op(package, python_version, package_index_url.as_str(), config)
}

fn lint(config: &Config, options: &LintOptions) -> HuakResult<()> {
ops::lint_project(config, options)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/huak-cli/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ mod tests {
assert_cmd_snapshot!(Command::new("huak").arg("init").arg("--help"));
}

// #[test]
// fn test_install_help() {
// assert_cmd_snapshot!(Command::new("huak").arg("install").arg("--help"));
// }
#[test]
fn test_install_help() {
assert_cmd_snapshot!(Command::new("huak").arg("install").arg("--help"));
}

#[test]
fn test_lint_help() {
Expand Down
1 change: 1 addition & 0 deletions crates/huak-cli/tests/snapshots/r#mod__tests__help-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Commands:
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the current project
install Install a Python package (defaults to $HOME/.huak/bin)
lint Lint the project's Python code
new Create a new project at <path>
publish Builds and uploads current project to a registry
Expand Down
1 change: 1 addition & 0 deletions crates/huak-cli/tests/snapshots/r#mod__tests__help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Commands:
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the current project
install Install a Python package (defaults to $HOME/.huak/bin)
lint Lint the project's Python code
new Create a new project at <path>
publish Builds and uploads current project to a registry
Expand Down
32 changes: 32 additions & 0 deletions crates/huak-cli/tests/snapshots/r#mod__tests__install_help.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/huak-cli/tests/mod.rs
info:
program: huak
args:
- install
- "--help"
---
success: true
exit_code: 0
----- stdout -----
Install a Python package (defaults to $HOME/.huak/bin)

Usage: huak install [OPTIONS] <PACKAGE>

Arguments:
<PACKAGE> The Python package to install

Options:
--python-version <PYTHON_VERSION>
The Python version to use. TODO(cnpryer): https://github.com/cnpryer/huak/issues/850
--package-index-url <PACKAGE_INDEX_URL>
The package index to use. TODO(cnpryer): Deps (document this) [default: https://pypi.python.org/simple]
-q, --quiet

--no-color

-h, --help
Print help

----- stderr -----

51 changes: 48 additions & 3 deletions crates/huak-package-manager/src/ops/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
use crate::HuakResult;
use huak_python_manager::{RequestedVersion, Version};
use huak_toolchain::{Channel, LocalTool, LocalToolchain};
use pep508_rs::Requirement;

use super::toolchain::{add_tool_to_toolchain, install_minimal_toolchain};
use crate::{Config, Error, HuakResult};

// TODO(cnpryer): https://github.com/cnpryer/huak/issues/850
pub fn _install() -> HuakResult<()> {
todo!()
pub fn install(
package: &Requirement,
python_version: Option<RequestedVersion>,
_package_index_url: &str,
config: &Config,
) -> HuakResult<()> {
// TODO(cnpryer): Since we're treating the bin dir as a toolchain that'd mean Huak home is
// the root of that toolchain (given toolchain bins are standard at roots).
let Some(home) = config.home.as_ref() else {
return Err(Error::HuakHomeNotFound);
};

// TODO(cnpryer): Smarter installs
if home.join("bin").join(&package.name).exists() {
return config
.terminal()
.print_warning(format!("'{}' is already installed", &package.name));
}

if !home.join("bin").exists() {
std::fs::create_dir_all(home)?;

// TODO(cnpryer): https://github.com/cnpryer/huak/issues/871
let channel = python_version
.map(|it| {
Channel::Version(Version {
major: it.major,
minor: it.minor,
patch: it.patch,
})
})
.unwrap_or_default();

install_minimal_toolchain(home, channel, config)?;
}

// TODO(cnpryer): Toolchains have names. The bin directory is used as a toolchain
// but there's no intention behind a toolchain named 'bin'.
let bin = LocalToolchain::new(home);
let package = LocalTool::from_spec(package.name.clone(), package.to_string());

add_tool_to_toolchain(&package, &bin, config)
}
1 change: 1 addition & 0 deletions crates/huak-package-manager/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use build::{build_project, BuildOptions};
pub use clean::{clean_project, CleanOptions};
pub use format::{format_project, FormatOptions};
pub use init::{init_app_project, init_lib_project, init_python_env};
pub use install::install;
pub use lint::{lint_project, LintOptions};
pub use new::{new_app_project, new_lib_project};
pub use publish::{publish_project, PublishOptions};
Expand Down
7 changes: 3 additions & 4 deletions crates/huak-package-manager/src/ops/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,9 @@ where
cmd.envs(env);
}

cmd.args(args).current_dir(&config.cwd);
dbg!(&cmd);

config.terminal().run_command(&mut cmd)
config
.terminal()
.run_command(cmd.args(args).current_dir(&config.cwd))
}

fn item_as_args(item: &Item) -> Option<Vec<String>> {
Expand Down
Loading

0 comments on commit f7c4c64

Please sign in to comment.