Skip to content

Commit

Permalink
feat: support for configuring dependency constraint strategy (#11)
Browse files Browse the repository at this point in the history
Add support for configuring dependency constraint strategy

Co-authored-by: Matthew Gamble <[email protected]>
  • Loading branch information
mwgamble and mwg-rea authored Feb 23, 2023
1 parent 907d6d5 commit 58dc30e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ the dependency will be rewritten as if it had been defined as:
b = "^1.2.3"
```

## Configuration

You can define a section in your `pyproject.toml` file named `tool.stickywheel`, to configure various options.

### Dependency constraint strategy

The default strategy is `semver` (described in the "Help" section above), but there are other choices:

| strategy | version | result |
|-----------|---------|-----------|
| `semver` | `1.2.3` | `^1.2.3` |
| `minimum` | `1.2.3` | `>=1.2.3` |
| `exact` | `1.2.3` | `1.2.3` |

To override the default, add `strategy` to the configuration. For example:

```toml
[tool.stickywheel]
strategy = "exact"
```

## ⚖️ Licence

This project is licensed under the [MIT licence][mit_licence].
Expand All @@ -61,4 +82,4 @@ This project uses [Semantic Versioning][semvar].
[discussions]: https://github.com/artisanofcode/poetry-stickywheel-plugin/discussions
[mit_licence]: http://dan.mit-license.org/
[cc_by_sa]: https://creativecommons.org/licenses/by-sa/4.0/
[semvar]: http://semver.org/
[semvar]: http://semver.org/
27 changes: 25 additions & 2 deletions src/poetry_stickywheel_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,45 @@
import poetry.core.packages.dependency
import poetry.core.packages.dependency_group
import poetry.core.packages.directory_dependency
import poetry.core.pyproject.exceptions
import poetry.core.pyproject.toml
import poetry.plugins.application_plugin


class StickyWheelsConfig:
def __init__(self, config: poetry.core.pyproject.toml.PyProjectTOML) -> None:
self._config = config.data.get("tool", {}).get("stickywheel", {})

@property
def strategy(self) -> str:
return self._config.get("strategy", "semver")

def prepare_constraint(self, version: str) -> str:
if self.strategy == "semver":
return f"^{version}"
elif self.strategy == "minimum":
return f">={version}"
elif self.strategy == "exact":
return version
else:
raise poetry.core.pyproject.exceptions.PyProjectException(
"Invalid StickyWheel strategy configured."
)


class StickyWheelsPlugin(poetry.plugins.application_plugin.ApplicationPlugin):
COMMANDS = (
poetry.console.commands.build.BuildCommand,
poetry.console.commands.publish.PublishCommand,
)

def activate(self, application: poetry.console.application.Application):
def activate(self, application: poetry.console.application.Application) -> None:
application.event_dispatcher.add_listener(
cleo.events.console_events.COMMAND,
self.event_listener,
)
self.package = application.poetry.package
self.config = StickyWheelsConfig(application.poetry.pyproject)

def event_listener(
self,
Expand Down Expand Up @@ -87,6 +110,6 @@ def pin_dependency(

return poetry.core.packages.dependency.Dependency(
name,
f"^{version}",
self.config.prepare_constraint(version),
groups=dependency.groups,
)

0 comments on commit 58dc30e

Please sign in to comment.