Skip to content

Commit

Permalink
0.1.3 - Initial file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Aug 11, 2023
1 parent 663d0f9 commit 613345e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions ifgen/data/schemas/Config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ properties:
items:
type: string

command:
type: string
default: "\\"

namespace:
type: array
default: [Coral]
items:
type: string

structs:
type: object
additionalProperties: false
Expand Down
2 changes: 1 addition & 1 deletion ifgen/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
vcorelib>=2.4.5
vcorelib>=2.4.6
36 changes: 34 additions & 2 deletions ifgen/struct/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

# built-in
from json import dumps
from multiprocessing import Pool
from pathlib import Path
from typing import Any, Dict, NamedTuple
Expand All @@ -11,6 +12,7 @@
from vcorelib.io import IndentedFileWriter

# internal
from ifgen import PKG_NAME, VERSION
from ifgen.config import Config

StructConfig = Dict[str, Any]
Expand All @@ -22,14 +24,42 @@ class GenerateStructTask(NamedTuple):
root: Path
path: Path
struct: StructConfig
config: Dict[str, Any]

def command(self, command: str, data: str = "", space: str = " ") -> str:
"""Get a command string."""
return (
str(self.config["command"])
+ command
+ (space if data else "")
+ data
)


def create_struct(task: GenerateStructTask) -> None:
"""Create a header file based on a struct definition."""

with IndentedFileWriter.from_path(task.path, per_indent=4) as writer:
# Write file header.
with writer.javadoc():
writer.write("Test.")
writer.write(task.command("file"))
writer.write(
task.command("brief", f"Generated by {PKG_NAME} ({VERSION}).")
)
writer.empty()
writer.write(dumps(task.struct, indent=4))

writer.write("#pragma once")
writer.empty()

# Write namespace.
namespace = "::".join(task.config["namespace"])
writer.write(f"namespace {namespace}")
with writer.scope(suffix=f"; // namespace {namespace}"):
# Write struct definition.
writer.empty()
writer.cpp_comment("Testing.")
writer.empty()


def generate_structs(root: Path, output: Path, config: Config) -> None:
Expand All @@ -42,7 +72,9 @@ def generate_structs(root: Path, output: Path, config: Config) -> None:
pool.map(
create_struct,
(
GenerateStructTask(root, output.joinpath(f"{name}.h"), data)
GenerateStructTask(
root, output.joinpath(f"{name}.h"), data, config.data
)
for name, data in config.data["structs"].items()
),
)
Expand Down
2 changes: 1 addition & 1 deletion local/configs/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: An interface generator for distributed computing.
entry: {{entry}}

requirements:
- vcorelib>=2.4.5
- vcorelib>=2.4.6

dev_requirements:
- pytest-cov
Expand Down
2 changes: 2 additions & 0 deletions tests/data/valid/scenarios/sample/ifgen.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
namespace: [A, B]

structs:
Test1: {}
Test2: {}

0 comments on commit 613345e

Please sign in to comment.