|
3 | 3 |
|
4 | 4 | import toml
|
5 | 5 | from invoke import Context, task
|
| 6 | +from invoke.exceptions import UnexpectedExit |
6 | 7 |
|
7 | 8 | import monkey_patch_invoke as _ # noqa: F401
|
8 | 9 |
|
@@ -78,3 +79,37 @@ def rename_project(_context: Context, project_name: str):
|
78 | 79 |
|
79 | 80 | with open('pyproject.toml', 'w', encoding='utf-8') as file:
|
80 | 81 | toml.dump(data, file)
|
| 82 | + |
| 83 | + |
| 84 | +@task |
| 85 | +def release(context: Context, version: str) -> None: |
| 86 | + '''Build & Publish to PyPI.''' |
| 87 | + |
| 88 | + # load pyproject |
| 89 | + pyproject_path = 'pyproject.toml' |
| 90 | + pyproject_string = '' |
| 91 | + with open(pyproject_path, 'r', encoding='utf-8') as pyproject_file: |
| 92 | + pyproject_string = pyproject_file.read() |
| 93 | + pyproject = toml.loads(pyproject_string) |
| 94 | + # change version to today datetime |
| 95 | + pyproject['tool']['poetry']['version'] = version |
| 96 | + with open(pyproject_path, 'w', encoding='utf-8') as pyproject_file: |
| 97 | + toml.dump(pyproject, pyproject_file) |
| 98 | + |
| 99 | + # build & publish |
| 100 | + try: |
| 101 | + context.run( |
| 102 | + ''' |
| 103 | + poetry build --no-interaction |
| 104 | + poetry publish --no-interaction |
| 105 | + ''', |
| 106 | + pty=True, |
| 107 | + ) |
| 108 | + except UnexpectedExit as exception: |
| 109 | + with open(pyproject_path, 'w', encoding='utf-8') as pyproject_file: |
| 110 | + pyproject_file.write(pyproject_string) |
| 111 | + raise exception from exception |
| 112 | + |
| 113 | + # recover to original |
| 114 | + with open(pyproject_path, 'w', encoding='utf-8') as pyproject_file: |
| 115 | + pyproject_file.write(pyproject_string) |
0 commit comments