Codegen Registry
The codegen registry is a simple way to register handlers for generating artifacts.
To use it, create a registry:
# my_site/codegen.py
from alliance_platform.codegen.registry import CodegenRegistry
codegen_registry = CodegenRegistry()
Somewhere, you need to call the registry run_codegen method to generate the artifacts. This can be done whenever
you like, however it is recommended to do it as part of the dev server running. This way the artifacts are generated
in dev as required and can then be committed to the repository.
from my_site.codegen import codegen_registry
codegen_registry.run_codegen()
You can also run codegen via the management command:
# Option 1: pass registry explicitly
./manage.py ap_codegen --registry my_site.codegen.codegen_registry
# Option 2: set ALLIANCE_PLATFORM["CODEGEN"]["REGISTRY"] and omit --registry
./manage.py ap_codegen
# CI-friendly check: exit non-zero if codegen would write files
./manage.py ap_codegen --check
Note
Template project. In the template project this is done currently in the presto/codegen.py file. See the run_codegen function.
You can then register a handler for a specific artifact type:
# my_site/frontend.py
from my_site.codegen import codegen_registry
class FrontendSettingsCodegenRegistration(CodegenRegistration):
def generate_artifacts(self) -> CodegenArtifact | list[CodegenArtifact]:
printer = TypescriptPrinter()
return CodegenArtifact(
settings.PROJECT_DIR / "frontend/src/settings.ts",
printer.print(
SingleLineComment(
f"This file is generated by codegen in {Path(__file__).relative_to(settings.PROJECT_DIR)}. Do not edit this file directly."
)
)
+ "\n\n"
+ printer.print(
VariableDeclaration(
[VariableDeclarator(Identifier("settings"), convert_to_node(some_settings))],
"const",
modifiers=[ExportKeyword()],
),
),
)
codegen_registry.register(FrontendSettingsCodegenRegistration())
When codegen is run, the generate_artifacts method will be called and the artifacts will be written to the file:
// frontend/src/settings.ts
// This file is generated by codegen in my_site/frontend.py. Do not edit this file directly.
export const settings = { setting1: 1234, setting2: 'hello' };
See the Nodes API documentation for a list of the available nodes.