Skip to content

Commit

Permalink
Add makefile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
smy20011 committed Sep 30, 2024
1 parent d1456d9 commit 715f569
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
16 changes: 11 additions & 5 deletions llmake/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import llmake.context as ctx
from llmake.context import Context, LinkType
from llmake.makefile import create_makefile
from llmake.markdown import Task, parse_markdown
from llmake.naming import slugify
from llmake.ninja import create_ninja_file
Expand All @@ -18,13 +19,18 @@


@app.default
def create_ninja(file):
def create_ninja(file, builder="makefile"):
with Path(file).open() as f:
proj = parse_markdown(f.read())
buildfile = create_ninja_file(file, proj)

with Path("build.ninja").open("w") as f:
f.write(buildfile)
if builder == "ninja":
buildfile = create_ninja_file(file, proj)
with Path("build.ninja").open("w") as f:
f.write(buildfile)
else:
buildfile = create_makefile(file, proj)
with Path("makefile").open("w") as f:
f.write(buildfile)


@app.command
Expand Down Expand Up @@ -62,7 +68,7 @@ def create_prompt(input_file: str, task_name: str):
result.append("# Task")
result.extend(task.prompt)

maybe_write(task_name + ".prompt.md", "\n".join(result))
maybe_write(task.filename(), "\n".join(result))


@app.command
Expand Down
53 changes: 53 additions & 0 deletions llmake/makefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from io import StringIO

from llmake.context import Context, LinkType
from llmake.markdown import Project, Task


def create_makefile(projfile: str, proj: Project):
buildfile = StringIO()

all_tasks = [task.result_filename() for task in proj.tasks]
all_files = []
print("all:", " ".join(all_tasks), file=buildfile)

all_contexts = proj.context + [ctx for task in proj.tasks for ctx in task.context]
for ctx in all_contexts:
if ctx.context_type == LinkType.WEB_LINK:
buildfile.write(make_context(ctx.target, ctx.filename()))
all_files.append(ctx.filename())

for task in proj.tasks:
contexts = proj.context + task.context
context_deps = [ctx.filename() for ctx in contexts]
task_deps = [t.result_filename() for t in proj.get_dependent_tasks(task)]
buildfile.write(make_task(projfile, task, context_deps + task_deps))
all_files.append(task.filename())
all_files.append(task.result_filename())

print(f"""
clean:
\t@echo "Cleaning up generated files..."
\t@rm "{'" "'.join(all_files)}"
""", file=buildfile)

return buildfile.getvalue()

def make_context(url, output):
return f"""
{output}:
\t@echo "Fetching webpage from {url}..."
\t@llmake fetch-context web-link {url} {output}
"""

def make_task(projfile, task: Task, deps):
return f"""
{task.filename()}: {projfile} {" ".join(deps)}
\t@echo "Generating prompt file: {task.filename()}..."
\t@llmake create-prompt {projfile} {task.slug()}
{task.result_filename()}: {task.filename()}
\t@echo "Querying LLM to generate result for task: {task.name}..."
\t@llmake query {task.filename()} {task.result_filename()}
"""

0 comments on commit 715f569

Please sign in to comment.