Skip to content

Commit

Permalink
Merge pull request #5 from Svtter:feat/task-api
Browse files Browse the repository at this point in the history
add task api. Feat.
  • Loading branch information
Svtter authored Nov 18, 2024
2 parents 785d290 + 36fb4f7 commit 8a0a6e2
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
13 changes: 13 additions & 0 deletions docs/api/checker.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
checker
========

用于检查 git 仓库是否干净。

Usage
------

.. code-block:: python
from easybuilder.checker import check_clean
check_clean() # 检查 git 仓库是否干净
8 changes: 8 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
API
=====

.. toctree::
:maxdepth: 2

task
checker
25 changes: 25 additions & 0 deletions docs/api/task.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Task
======

We create task api to define the task of easybuilder.

预定义的 task 是 :class:`CheckCleanTask`。如果想要替换预定义的 task,可以修改 Worker 的 ``default_tasks``。

Usage
-----------

如果想要定义自己的 task,可以通过以下方式进行。

.. code-block:: python
from easybuilder.task import Task
class MyTask(Task):
pass
class MyWorker(Worker):
prev_tasks = [MyTask]
def main(self):
pass
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Install easybuilder
:caption: Contents:

usage
api/index

7 changes: 0 additions & 7 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,3 @@ easybuilder 是一个简单的构建工具,主要包含以下几个组件:
# 在临时文件夹中进行操作
pass
**checker**: 检查工具
-----------------------------------
.. code-block:: python
from easybuilder.checker import check_clean
check_clean() # 检查 git 仓库是否干净
27 changes: 27 additions & 0 deletions src/easybuilder/task/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import abc
import os

from easybuilder.checker import check_clean


class Task(abc.ABC):
@abc.abstractmethod
def action(self):
pass


class PdmLockTask(Task):
"""test if pdm.lock is latest"""

def action(self):
ret = os.system("pdm lock --check")
if ret != 0:
print("pdm.lock is not up to date, please run 'pdm lock' first")
exit(1)


class CheckCleanTask(Task):
"""check if git repo is clean"""

def action(self):
check_clean()
11 changes: 9 additions & 2 deletions src/easybuilder/worker.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from .checker import check_clean
from . import task


class Worker(object):
default_tasks: list[type[task.Task]] = [task.CheckCleanTask]
prev_tasks: list[type[task.Task]] = []
next_tasks: list[type[task.Task]] = []

def __init__(self):
pass

def before_run(self):
check_clean()
for t in self.default_tasks:
t().action()
for t in self.prev_tasks:
t().action()

def after_run(self):
pass
Expand Down

0 comments on commit 8a0a6e2

Please sign in to comment.