Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to init command for installing project dependencies #867

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ cargo install --git https://github.com/cnpryer/huak.git huak
## Usage

```console
❯ huak help

A Python package manager written in Rust and inspired by Cargo.

Usage: huak [OPTIONS] <COMMAND>
Expand All @@ -60,8 +58,7 @@ Commands:
completion Generates a shell completion script for supported shells
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the existing project
install Install the dependencies of an existing project
init Initialize the current project
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
116 changes: 88 additions & 28 deletions crates/huak-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ enum Commands {
#[arg(last = true)]
trailing: Option<Vec<String>>,
},
/// Initialize the existing project.
/// Initialize the current project.
Init {
/// Use an application template.
#[arg(long, conflicts_with = "lib")]
Expand All @@ -91,12 +91,24 @@ enum Commands {
/// Don't initialize VCS in the project
#[arg(long)]
no_vcs: bool,
},
/// Install the dependencies of an existing project.
Install {
/// Install optional dependency groups
#[arg(long, num_args = 1..)]
groups: Option<Vec<String>>,
/// Initialize with a project manifest.
#[arg(long)]
manifest: Option<PathBuf>,
// TODO(cnpryer): https://github.com/cnpryer/huak/issues/853
// /// Initialize with requirements files.
// #[arg(short, long)]
// requirements: Option<Vec<PathBuf>>,
// /// Initialize with development requirements files.
// dev_requirements: Option<Vec<PathBuf>>,
/// Initialize without setting up a Python environment.
#[arg(long)]
no_env: bool,
/// Optional dependency groups to install.
#[arg(long)]
optional_dependencies: Option<Vec<String>>,
/// Force the initialization.
#[arg(short, long)]
force: bool,
/// Pass trailing arguments with `--`.
#[arg(last = true)]
trailing: Option<Vec<String>>,
Expand Down Expand Up @@ -337,14 +349,36 @@ fn exec_command(cmd: Commands, config: &mut Config) -> HuakResult<()> {
};
fmt(config, &options)
}
Commands::Init { app, lib, no_vcs } => {
Commands::Init {
app,
lib,
no_vcs,
manifest,
no_env,
optional_dependencies,
trailing,
force,
} => {
config.workspace_root = config.cwd.clone();
let options = WorkspaceOptions { uses_git: !no_vcs };
init(app, lib, config, &options)
}
Commands::Install { groups, trailing } => {
let options = InstallOptions { values: trailing };
install(groups, config, &options)
let workspace_options = WorkspaceOptions {
uses_git: !no_vcs,
values: None,
};

let install_options = InstallOptions { values: trailing }; // TODO(cnpryer)

// TODO(cnpryer): Use `WorkspaceOptions` where possible.
init(
app,
lib,
manifest,
no_env,
optional_dependencies,
force,
config,
&workspace_options,
&install_options,
)
}
Commands::Lint {
fix,
Expand Down Expand Up @@ -373,7 +407,10 @@ fn exec_command(cmd: Commands, config: &mut Config) -> HuakResult<()> {
no_vcs,
} => {
config.workspace_root = PathBuf::from(path);
let options = WorkspaceOptions { uses_git: !no_vcs };
let options = WorkspaceOptions {
uses_git: !no_vcs,
values: None,
};
new(app, lib, config, &options)
}
Commands::Publish { trailing } => {
Expand Down Expand Up @@ -478,21 +515,44 @@ fn fmt(config: &Config, options: &FormatOptions) -> HuakResult<()> {
ops::format_project(config, options)
}

fn init(app: bool, _lib: bool, config: &Config, options: &WorkspaceOptions) -> HuakResult<()> {
if app {
ops::init_app_project(config, options)
} else {
ops::init_lib_project(config, options)
}
}

#[allow(clippy::needless_pass_by_value)]
fn install(
groups: Option<Vec<String>>,
#[allow(clippy::too_many_arguments)]
#[allow(clippy::fn_params_excessive_bools)]
fn init(
app: bool,
_lib: bool,
manifest: Option<PathBuf>,
no_env: bool,
optional_dependencies: Option<Vec<String>>,
force: bool,
config: &Config,
options: &InstallOptions,
workspace_options: &WorkspaceOptions,
install_options: &InstallOptions,
) -> HuakResult<()> {
ops::install_project_dependencies(groups.as_ref(), config, options)
let res = if app {
ops::init_app_project(config, workspace_options)
} else {
ops::init_lib_project(config, workspace_options)
};

// If initialization failed because a manifest file already exists and the project
// initialization option 'no-env' is 'false' then we attempt to inititialize the
// project's Python environment.
if res
.as_ref()
.err()
.map_or(true, |it| matches!(it, HuakError::ManifestFileFound))
&& !no_env
{
ops::init_python_env(
manifest,
optional_dependencies,
force,
install_options,
config,
)
} else {
res
}
}

fn lint(config: &Config, options: &LintOptions) -> HuakResult<()> {
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
3 changes: 1 addition & 2 deletions crates/huak-cli/tests/snapshots/r#mod__tests__help-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Commands:
completion Generates a shell completion script for supported shells
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the existing project
install Install the dependencies of an existing project
init Initialize the current project
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
3 changes: 1 addition & 2 deletions crates/huak-cli/tests/snapshots/r#mod__tests__help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Commands:
completion Generates a shell completion script for supported shells
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the existing project
install Install the dependencies of an existing project
init Initialize the current project
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
35 changes: 26 additions & 9 deletions crates/huak-cli/tests/snapshots/r#mod__tests__init_help.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: crates/huak_cli/tests/mod.rs
source: crates/huak-cli/tests/mod.rs
info:
program: huak
args:
Expand All @@ -9,17 +9,34 @@ info:
success: true
exit_code: 0
----- stdout -----
Initialize the existing project
Initialize the current project

Usage: huak init [OPTIONS]
Usage: huak init [OPTIONS] [-- <TRAILING>...]

Arguments:
[TRAILING]... Pass trailing arguments with `--`

Options:
--app Use an application template
--lib Use a library template [default]
--no-vcs Don't initialize VCS in the project
-q, --quiet
--no-color
-h, --help Print help
--app
Use an application template
--lib
Use a library template [default]
--no-vcs
Don't initialize VCS in the project
--manifest <MANIFEST>
Initialize with a project manifest
--no-env
Initialize without setting up a Python environment
--optional-dependencies <OPTIONAL_DEPENDENCIES>
Optional dependency groups to install
-f, --force
Force the initialization
-q, --quiet

--no-color

-h, --help
Print help

----- stderr -----

26 changes: 0 additions & 26 deletions crates/huak-cli/tests/snapshots/r#mod__tests__install_help.snap

This file was deleted.

2 changes: 2 additions & 0 deletions crates/huak-package-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Error {
ManifestFileFound,
#[error("a manifest file could not be found")]
ManifestFileNotFound,
#[error("a manifest file is not supported: {0}")]
ManifestFileNotSupported(PathBuf),
#[error("a package version could not be found")]
PackageVersionNotFound,
#[error("a project already exists")]
Expand Down
37 changes: 1 addition & 36 deletions crates/huak-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,7 @@
//! 2. Making some change to the project
//! 3. Running tests
//! 4. Distributing the project
//!
//!```zsh
//! ❯ huak help
//!
//! A Python package manager written in Rust and inspired by Cargo.
//!
//! Usage: huak [OPTIONS] <COMMAND>
//!
//! Commands:
//! activate Activate the virtual environment
//! add Add dependencies to the project
//! build Build tarball and wheel for the project
//! clean Remove tarball and wheel from the built project
//! completion Generates a shell completion script for supported shells
//! fix Auto-fix fixable lint conflicts
//! fmt Format the project's Python code
//! init Initialize the existing project
//! install Install the dependencies of an existing project
//! lint Lint the project's Python code
//! new Create a new project at <path>
//! publish Builds and uploads current project to a registry
//! python Manage Python installations
//! remove Remove dependencies from the project
//! run Run a command with Huak
//! test Test the project's Python code
//! toolchain Manage toolchains
//! update Update the project's dependencies
//! version Display the version of the project
//! help Print this message or the help of the given subcommand(s)
//!
//! Options:
//! -q, --quiet
//! --no-color
//! -h, --help Print help
//! -V, --version Print version
//!```

mod config;
mod dependency;
mod environment;
Expand Down
Loading
Loading