Skip to content

Commit

Permalink
Add basic update command (#550)
Browse files Browse the repository at this point in the history
* Add basic update command

* Update docs

* Suppress update operation tests
  • Loading branch information
cnpryer authored Mar 30, 2023
1 parent 6319a0b commit 1a2864d
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Commands:
remove Remove dependencies from the project
run Run a command within the project's environment context
test Test the project's Python code
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)

Expand Down
97 changes: 60 additions & 37 deletions docs/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Use `pip` to install `huak` from [PyPI](https://pypi.org).

```
```zsh
~/github
❯ pip install huak
```
Expand All @@ -23,14 +23,14 @@ Use `pip` to install `huak` from [PyPI](https://pypi.org).

To create a new project use the `new` command.

```
```zsh
~/github took 2s
❯ huak new my-project
```

### Or initialize an existing project

```
```zsh
~/github/existing-project
❯ huak init
```
Expand All @@ -39,8 +39,8 @@ To create a new project use the `new` command.

Initializing an existing project adds a `pyproject.toml` to the current directory. Bootstrapping the project with the `new` command creates a Python project with the following structure:

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ tree .
.
├── pyproject.toml
Expand All @@ -59,17 +59,17 @@ Note that without `--no-vcs` `huak` generates a `git`-initialized project.

Use `huak` to add dependencies to your Python project.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak add xlcsv
```

#### Installer Options

Currently `huak` uses `pip` under the hood for package installation. You can pass additional arguments onto `pip`. Any arguments after `--` are handed off to `pip install`.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak add torch torchvision torchaudio -- --extra-index-url https://download.pytorch.org/whl/cu117
```

Expand All @@ -81,17 +81,17 @@ You can also assign dependencies to a group using `--group`.

Use the `install` command to install the project's dependencies.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak install
```

#### Using --groups

To install all dependencies (including optional dependencies) use the group name "all".

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak install --groups all
```

Expand All @@ -101,22 +101,45 @@ If you already have an optional dependency group named "all" then `--groups` wil

`huak install` would trigger a standard `pip install` on your group's packages. So without PEP 508 you won't install the pytorch.org packages as demonstrated with `huak add` earlier. To do this you could pass the same options given to `huak add`. Use a group to isolate these options to those specific dependencies.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak add torch torchvision torchaudio --group torch -- --extra-index-url https://download.pytorch.org/whl/cu117

my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak install --groups torch -- --extra-index-url https://download.pytorch.org/whl/cu117
```

In the future `huak` will manage translating installer options to PEP 508 strings for you.

### Update dependencies

To update a dependency use the `update` command.

```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak update xlcsv
```

You can also update a group of dependencies using `--group`.

```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak update --group dev
```

Or everything with "all".

```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak update --group all
```

### Remove dependencies

To remove a dependency from the project use the `remove` command.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak remove xlcsv
```

Expand All @@ -128,8 +151,8 @@ Huak ships commands allowing you to format your python code, lint it, and test i

Use the `fmt` command to format your Python project's code.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak fmt
```

Expand All @@ -143,8 +166,8 @@ Use `--check` if all you want to do is verify your code is already formatted. No

Use the `lint` command to lint your Python project's code.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0 took 2s
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0 took 2s
❯ huak lint
```

Expand All @@ -159,8 +182,8 @@ The `--fix` flag can be used to address any auto-fixable issues.

`huak` also uses `mypy` for type-checking. To disable this behavior use `--no-types`.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0 took 2s
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0 took 2s
❯ huak lint --no-types
```

Expand All @@ -170,17 +193,17 @@ Currently, since `ruff` is the default tool used by `huak lint`, passing additio

Use the `test` command to test your project.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak test
```

### Run commands within your project's environment context

You can use `huak` to run a command within the Python environment your project uses.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak run which python
/Users/chrispryer/github/my-project/.venv/bin/python
```
Expand All @@ -191,20 +214,20 @@ my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0

If you're building a Python package you'd like to share, use `huak build` and `huak publish` to build and publish the project to [PyPI](https://pypi.org).

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak build

my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak publish
```

### Cleaning up

Use `huak clean` to clean out the dist/ directory.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0 took 26s
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0 took 26s
❯ huak clean
```

Expand All @@ -216,8 +239,8 @@ Use `huak config` commands to configure `huak`.

With `huak config completion` you can setup shell completion for `huak`.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak config completion -h
Generates a shell completion script for supported shells. See the help menu for more information on supported shells

Expand All @@ -234,8 +257,8 @@ Options:

Any bugs or suggestions can be submitted as issues [here](https://github.com/cnpryer/huak/issues/new). All feedback is welcome and greatly appreciated ❤️.

```
my-project on  master [?] is 📦 v0.0.1 via 🐍 v3.11.0
```zsh
my-project on  master 📦 v0.0.1 via 🐍 v3.11.0
❯ huak --version
huak 0.0.10-alpha.6
```
37 changes: 37 additions & 0 deletions src/bin/huak/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ pub enum Commands {
#[arg(last = true)]
trailing: Option<Vec<String>>,
},
/// Update the project's dependencies.
Update {
#[arg(num_args = 0..)]
dependencies: Option<Vec<String>>,
/// Update an optional dependency group
#[arg(long)]
group: Option<String>,
/// Pass trailing arguments with `--`.
#[arg(last = true)]
trailing: Option<Vec<String>>,
},
/// Display the version of the project.
Version,
}
Expand Down Expand Up @@ -285,6 +296,15 @@ impl Cli {
Some(TestOptions { args: trailing });
test(operation_config)
}
Commands::Update {
dependencies,
group,
trailing,
} => {
operation_config.installer_options =
Some(InstallerOptions { args: trailing });
update(dependencies, group, operation_config)
}
Commands::Version => version(operation_config),
}
.map_err(|e| Error::new(e, ExitCode::FAILURE))
Expand Down Expand Up @@ -422,6 +442,23 @@ fn test(operation_config: OperationConfig) -> HuakResult<()> {
ops::test_project(&operation_config)
}

fn update(
dependencies: Option<Vec<String>>,
groups: Option<String>,
operation_config: OperationConfig,
) -> HuakResult<()> {
match groups.as_ref() {
Some(it) => ops::update_project_optional_dependencies(
dependencies,
it,
&operation_config,
),
None => {
ops::update_project_dependencies(dependencies, &operation_config)
}
}
}

fn version(operation_config: OperationConfig) -> HuakResult<()> {
ops::display_project_version(&operation_config)
}
Expand Down
26 changes: 26 additions & 0 deletions src/huak/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,16 @@ impl VirtualEnvironment {
.uninstall(packages, installer_options, terminal)
}

/// Update many Python packages in the environment.
pub fn update_packages(
&self,
packages: &[&str],
installer_options: Option<&InstallerOptions>,
terminal: &mut Terminal,
) -> HuakResult<()> {
self.installer.update(packages, installer_options, terminal)
}

/// Check if the environment is already activated.
pub fn is_active(&self) -> bool {
if let Some(path) = active_virtual_env_path() {
Expand Down Expand Up @@ -803,6 +813,22 @@ impl Installer {
}
terminal.run_command(&mut cmd)
}

pub fn update(
&self,
packages: &[&str],
options: Option<&InstallerOptions>,
terminal: &mut Terminal,
) -> HuakResult<()> {
let mut cmd = Command::new(self.config.path.clone());
cmd.args(["install", "--upgrade"]).args(packages);
if let Some(it) = options {
if let Some(args) = it.args.as_ref() {
cmd.args(args.iter().map(|item| item.as_str()));
}
}
terminal.run_command(&mut cmd)
}
}

#[derive(Default, Clone)]
Expand Down
Loading

0 comments on commit 1a2864d

Please sign in to comment.