swydd/DESIGN.md

85 lines
1.6 KiB
Markdown
Raw Normal View History

2024-07-22 15:17:58 -05:00
# Swydd Design
Goals:
- Generic task runner
- Portable (single python module)
- library not an exe
basic design:
`tasks.py`:
```python
from swydd import taks, option, cli
2024-10-25 14:27:00 -05:00
2024-07-22 15:17:58 -05:00
@task
2024-10-25 14:27:00 -05:00
@option("program", "name of program to compile")
2024-07-22 15:17:58 -05:00
def build(program: str = "hello"):
2024-10-25 14:27:00 -05:00
"""build a program"""
sub < f"gcc -o {program} {program}.c"
2024-07-22 15:17:58 -05:00
cli()
```
```sh
./tasks.py build
./tasks.py build --program goodbye
```
## Ideas
### Simple shell pipelines
```python
@task
2024-10-25 14:27:00 -05:00
def pipe_commands():
2024-07-22 15:17:58 -05:00
"""run pre-commit (and mypy)"""
p = Pipe(Exec("find . -name file"), Exec("grep 'pattern'")).get()
print(p.stdout)
```
Made even simpler with operator overloading:
2024-10-22 11:53:27 -05:00
2024-07-22 15:17:58 -05:00
```python
@task
def run_commands():
2024-10-25 14:27:00 -05:00
stdout = get < (pipe | "find . -name file" | "grep 'pattern'")
print(stdout)
2024-07-22 15:17:58 -05:00
```
2024-10-22 11:53:27 -05:00
Upon reflection I think the operator overloading is wildly confusing.
I think it will make more sense to try to develop chainable objects:
Make `sub(cmd, **kwargs)` and alias for `proc(cmd).run(**kwargs)`
```python
@task
def run_commands():
2024-10-25 14:27:00 -05:00
stdout = proc("find . -name file").pipe("grep 'pattern'").get()
print(stdout)
```
## Handling a DAG
```python
@task
@needs("src/a-file.txt")
@targets("build/output.txt")
def build():
"""build step with a dependency"""
lines = asset("src/a-filter.txt").read().splitlines()
# or lines = ctx.needs[0].read().splitlines()
ctx.targets.output.write("\n".join([f"{i}: {line}" for i, line in enumerate(lines)]))
2024-10-22 11:53:27 -05:00
```
2024-07-22 15:17:58 -05:00
2024-10-25 14:27:00 -05:00
2024-07-22 15:17:58 -05:00
## Internal CLI
```sh
2024-10-25 14:27:00 -05:00
./tasks.py +swydd --version # <- current favorite (use a none valid function name to possibly prevent overlap)
2024-07-22 15:17:58 -05:00
```