Skip to content

Commit

Permalink
Add new install command
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Nov 22, 2023
1 parent 389fe05 commit 518b634
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 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
43 changes: 40 additions & 3 deletions crates/huak-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ 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,
Config, Error as HuakError, HuakResult, InstallOptions, Package, TerminalOptions, Verbosity,
WorkspaceOptions,
};
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,22 @@ 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(short, long, required = true)]
package: Requirement,
/// The Python version to use. TODO(cnpryer): https://github.com/cnpryer/huak/issues/850
#[arg(long, alias = "py")]
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"
)] // TODO(cnpryer): Names
package_index_url: Url,
},
/// Lint the project's Python code.
Lint {
/// Address any fixable lints.
Expand Down Expand Up @@ -380,6 +398,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 +578,20 @@ 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.to_string(),
config,
)
}

fn lint(config: &Config, options: &LintOptions) -> HuakResult<()> {
ops::lint_project(config, options)
}
Expand Down
42 changes: 40 additions & 2 deletions crates/huak-package-manager/src/ops/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
use crate::HuakResult;
use huak_python_manager::{RequestedVersion, Version};
use huak_toolchain::Channel;
use pep508_rs::Requirement;

use super::toolchain::install as install_toolchain_inner;
use crate::{Config, Error, HuakResult};

// TODO(cnpryer): https://github.com/cnpryer/huak/issues/850
pub fn _install() -> HuakResult<()> {
pub fn install(
package: Requirement,
python_version: Option<RequestedVersion>,
_package_index_url: String,
config: &Config,
) -> HuakResult<()> {
let Some(bin) = config.home.as_ref().map(|it| it.join("bin")) else {
return Err(Error::HuakHomeNotFound);
};

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

if !bin.exists() {
std::fs::create_dir_all(&bin)?;

// 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_toolchain_inner(bin, channel, config)?;
}

todo!()
}
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
4 changes: 2 additions & 2 deletions crates/huak-package-manager/src/ops/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn install_toolchain(
return Err(Error::LocalToolchainExists(path));
}

if let Err(e) = install(path.clone(), channel, config) {
if let Err(e) = install(path, channel, config) {
teardown(parent.join(&channel_string), config)?;
Err(e)
} else {
Expand All @@ -124,7 +124,7 @@ pub fn install_toolchain(
}

#[allow(clippy::too_many_lines)]
fn install(path: PathBuf, channel: Channel, config: &Config) -> HuakResult<()> {
pub(crate) fn install(path: PathBuf, channel: Channel, config: &Config) -> HuakResult<()> {
let mut toolchain = LocalToolchain::new(path);

toolchain.set_channel(channel);
Expand Down

0 comments on commit 518b634

Please sign in to comment.