Skip to content

Commit

Permalink
feat: add docs + ci/cd + poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
cafadev committed Oct 11, 2023
1 parent 72fef0b commit ad54479
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 15 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Prerelease

on:
push:
branches: [alpha, beta, next]

jobs:

compute-new-tag-version:
timeout-minutes: 15
runs-on: ubuntu-latest
outputs:
fromTag: ${{ steps.fromTag.outputs.tag }}
toTag: ${{ steps.toTag.outputs.tag }}
steps:
- name: checkout code repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: 'Get Previous tag'
id: fromTag
uses: "WyriHaximus/github-action-get-previous-tag@v1"

- name: Installing python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Installing poetry
uses: abatilo/actions-poetry@v2
with:
poetry-version: '1.6.1'

- name: Install deps
run: poetry install

- name: Run matic-release
run: poetry run python scripts/release.py --ci

- name: Publish tag
run: git push --tags

- name: 'Get lastest tag'
id: toTag
uses: "WyriHaximus/github-action-get-previous-tag@v1"

generate-release:
runs-on: ubuntu-latest
needs:
- compute-new-tag-version
if: ${{ needs.compute-new-tag-version.outputs.fromTag != needs.compute-new-tag-version.outputs.toTag }}
steps:
- name: checkout code repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up git-cliff
uses: kenji-miyake/setup-git-cliff@v1

- name: Run git-cliff
run: |
{
echo 'CHANGELOG<<EOF'
git cliff --latest
echo EOF
} >> "$GITHUB_ENV"
- name: Create Release
uses: softprops/action-gh-release@v1
with:
body: ${{ env.CHANGELOG }}
prerelease: true
tag_name: ${{ needs.compute-new-tag-version.outputs.toTag }}
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# TurboBus

TurboBus is an opinionated implementation of Command Responsibility Segregation pattern in python.

## Simple usage
Let's see an example using python typings. You can omit all the typing stuffs if you want to.

**God Mode ⚡**
```python
from dataclasses import dataclass
from typing import TypeAlias

from turbobus.bus import Command, CommandHandler, CommandBus
from turbobus.decorators import command, injectable

LogHandlerType: TypeAlias = "ILogHandler"

@dataclass
class LogCommand(Command[LogHandlerType]):
content: str


class ILogHandler(CommandHandler[str]):
...


@command(LogCommand)
class LogHandler(ILogHandler):
def execute(self, cmd: LogCommand) -> str:
return cmd.content


if __name__ == '__main__':
bus = CommandBus()

result = bus.execute(
LogCommand('Hello dude!')
)
print(result) # Hello dude
```

**Human Mode 🥱**
```python
from dataclasses import dataclass

from turbobus.bus import Command, CommandHandler
from turbobus.decorators import command, injectable

@dataclass
class LogCommand(Command):
content


class ILogHandler(CommandHandler):
...

@command(LogCommand)
class LogHandler(ILogHandler):
def execute(self, cmd: LogCommand) -> str:
return cmd.content


if __name__ == '__main__':
bus = CommandBus()

result = bus.execute(
LogCommand('Hello dude!')
)
print(result) # Hello dude
```

## Dependency injection
In many cases we're going to need to inject dependencies to our command handler. To accomplish that we have two important tools: `@injectable` decorator and `injecting` function.

With the injectable decorator we can specify a class that is implementing the functionalities of the dependency. For example:

```python
from turbobus.decorators import injectable
from log.axioma.log import ILogger


class ILogger(ABC):

@abstractmethod
def logger(self, text: str) -> None:
...


@injectable(ILogger)
class Logger:

def logger(self, text: str) -> None:
print('from logger', text)


@command(LogCommand)
@dataclass(kw_only=True)
class LogHandler(ILogHandler):

logger = injecting(ILogger)

def execute(self, cmd: LogCommand) -> str:
if self.logger is not None:
self.logger.logger(cmd.content)

return cmd.content

```

As you can see in the example above, we're defining an abstract class with the logger method. Then we're doing the implementation of the `ILogger` and we're indicating that in the `@injectable(ILogger)`.

Then, using the `injecting` function, TurboBus is going to map that dependency and inject the instance in the attribute.
12 changes: 0 additions & 12 deletions main.py

This file was deleted.

89 changes: 89 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "turbobus"
version = "1.0.0-alpha.0"
description = "TurboBus is an opinionated implementation of Command Responsibility Segregation pattern in python"
authors = ["Christopher A. Flores <christopher.flores@codexitos.com>"]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.2"
matic-release = {git = "https://github.com/fancy-crud/matic-release.git"}

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
26 changes: 26 additions & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
from matic_release.axioma.version import Version
from matic_release.capabilities.commit_analyzer import CommitAnalyzer
from matic_release.capabilities.compute_tag import ComputeTag
from matic_release.capabilities.publish_tag import PublishTag
from matic_release.integration.git import GitService


args = sys.argv[1:]
git = GitService()

latest_tag = git.get_latest_tag()

version = Version(latest_tag)

commit_analyzer = CommitAnalyzer()
compute_tag = ComputeTag(git, commit_analyzer)

compute_tag.execute(version)


if '--ci' in args:
publish = PublishTag(git)
publish.execute(version)

print(version.future_tag.value)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions log/capabilities/log.py → tests/log/capabilities/log.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from dataclasses import dataclass
from turbobus.decorators import command, injectable, injecting
from ..axioma.log import ILogger, LogCommand
from turbobus.decorators import command, injecting
from ..axioma.log import ILogHandler, ILogger, LogCommand


@command(LogCommand)
@dataclass(kw_only=True)
class LogHandler:
class LogHandler(ILogHandler):

dependency = injecting(ILogger)

Expand Down
File renamed without changes.

0 comments on commit ad54479

Please sign in to comment.