Skip to content

Commit

Permalink
Preparing enum generation from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
KaruroChori committed Jan 19, 2025
1 parent 0bc039d commit 2348683
Show file tree
Hide file tree
Showing 24 changed files with 75 additions and 27 deletions.
4 changes: 4 additions & 0 deletions commons/schemas/enum.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "object",
"properties": {}
}
File renamed without changes.
15 changes: 15 additions & 0 deletions docs/developers/code-gen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Code generation is extremely important in this project, to ensure:

- Multiple targets can be automatically constructed from the same specifications
- They will be aligned, simplifying versioning

Code generation tasks are performed by typescript code. They are based on information defined in `/schemas` and run off scripts in `/scripts/codegen`.
This choice of technologies means that installing the [bun js runtime](https://bun.sh/) is needed to build the project (and only that).

## Targets for generation

### Database

### Widgets

### Enumerations
9 changes: 9 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ At the end of this process the XML is freed.
XML files are assumed to be fully loaded in memory during processing. However, it is possible to write a streaming parser with much lower memory footprint, as long as the XML is following the strict schema shipped with this repo (it has a rigid order for some tags).
This strategy was not employed as it makes the loading process much slower for multithreaded systems with reasonable amounts of memory, and would only be better for low-end embedded systems where fltk is unlikely to be suitable.

### Why codegen is in typescript? Why bun?

The main reason is expressiveness. Primitives like `map`, `split`, `join` and string templates are extremely useful in simplifying the process.
Trying to implement the same in C++ would have been a bit of a nightmare to be honest, even with modern features like `std::format`. There are libraries replicating a good chunk of the js standard library in C++, but at that point I would just use javascript.

As for "why bun?", the main reason is convenience. It has native support to run typescript without transpilation, and a good library to read/write files which is much more compact compared to most alternatives.
I considered using [txiki.js](https://github.com/saghul/txiki.js) (no less because I am somewhat involved with the project), which is much more lightweight and can be easily built without extra dependencies if not shipped already on the developer's machine.
Still, it does not use semantic versioning (so features will break every now and then), and it cannot use typescript natively. Hence, no txiki (for now).

## Licensing

### Can I contribute to this project?
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ vs_fltk = shared_library(
'./src/themes/default-cute.cpp',

'./src/cbindings/vs.cpp',
'./src/cbindings/components.autogen.cpp',
'./src/cbindings/widgets.autogen.cpp',

'./src/globals.cpp',
],
Expand Down Expand Up @@ -391,7 +391,7 @@ endif
install_headers(
[
'./include/cbindings/vs.h',
'./include/cbindings/components.autogen.h',
'./include/cbindings/widgets.autogen.h',
'./include/cbindings/module.modulemap',
],
preserve_path: false,
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:base.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Base element to provide basic props/computed for all those which are derived from `fltk_widget`",
"exposed": false,
"type": "node",
Expand Down
18 changes: 9 additions & 9 deletions schemas/widgets/fl:button.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"description": "Basic button element",
"exposed": true,
"type": "leaf",
"headers": ["FL/Fl_Button.H"],
"extends": "fl:base",
"codegen": {"extends": "ui<Fl_Button>"},
"fields": {}
}
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Basic button element",
"exposed": true,
"type": "leaf",
"headers": ["FL/Fl_Button.H"],
"extends": "fl:base",
"codegen": { "extends": "ui<Fl_Button>" },
"fields": {}
}
2 changes: 1 addition & 1 deletion schemas/widgets/fl:button.toggle.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Toggle button element",
"exposed": true,
"type": "leaf",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:flex.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Flex container",
"exposed": true,
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:grid.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Grid container",
"exposed": true,
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:group.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Basic group element",
"exposed": true,
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:input.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Basic input element",
"exposed": true,
"type": "leaf",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:label.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Label element to a window",
"exposed": true,
"type": "leaf",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:pack.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Pack container",
"exposed": true,
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:scroll.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Scroll container",
"exposed": true,
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:textbox.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Blabla bla",
"type": "leaf",
"exposed": true,
Expand Down
2 changes: 1 addition & 1 deletion schemas/widgets/fl:window.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../commons/schemas/json-component.schema.json",
"$schema": "../../commons/schemas/widget.schema.json",
"description": "Base element to a window",
"exposed": true,
"type": "node",
Expand Down
1 change: 0 additions & 1 deletion scripts/codegen/enum.schema.ts

This file was deleted.

16 changes: 16 additions & 0 deletions scripts/codegen/gen-enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Code generator for code and derived specs out of the json schemas representing enumerations.
Use `quick` as optional arg to avoid generating the meson file again.
This will speed up compilation considerably and is fine as long as you have not added or removed files in the schema folder.
*/

import type { Static } from '@sinclair/typebox';
import { $ } from 'bun'
import { Glob } from "bun";
import { parse } from "node:path"
import { enum_schema } from './schemas/enum.schema';
import { Value } from '@sinclair/typebox/value';

import { render, JSXXML } from 'jsx-xml'

await Bun.write('./commons/schemas/enum.schema.json', JSON.stringify(enum_schema, null, 4))
7 changes: 4 additions & 3 deletions scripts/codegen/gen-widgets.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Code generator for code and derived specs from json schemas.
Code generator for code and derived specs from json schemas representing widgets.
Use `quick` as optional arg to avoid generating the meson file again.
This will speed up compilation considerably and is fine as long as you have not added or removed files in the schema folder.
*/
Expand All @@ -8,7 +8,7 @@ import type { Static } from '@sinclair/typebox';
import { $ } from 'bun'
import { Glob } from "bun";
import { parse } from "node:path"
import { widget_schema, type_schema } from './widget.schema';
import { widget_schema, type_schema } from './schemas/widget.schema';
import { Value } from '@sinclair/typebox/value';

import { render, JSXXML } from 'jsx-xml'
Expand All @@ -25,6 +25,7 @@ function make_type_code(type: Static<typeof type_schema>, subtype: string, code:
else if (type === 'scalar-1') return `size_t computed[1]; if((ok = field_types::h_px(1,computed,value,that))){${code}}`
else if (type === 'scalar-2') return `size_t computed[2]; if((ok = field_types::h_px(2,computed,value,that))){${code}}`
else if (type === 'scalar-4') return `size_t computed[4]; if((ok = field_types::h_px(4,computed,value,that))){${code}}`
else throw "Type not yet supported, add it to `make_type_code`"
}

function gen_cpp(data: Static<typeof widget_schema>) {
Expand Down Expand Up @@ -154,7 +155,7 @@ await $`rm -rf ./commons/schemas/widgets/`
await $`mkdir -p ./commons/schemas/widgets/`

//Save the schema, so that our json files can all be validated in the editor while writing them.
await Bun.write('./commons/schemas/json-component.schema.json', JSON.stringify(widget_schema, null, 4))
await Bun.write('./commons/schemas/widget.schema.json', JSON.stringify(widget_schema, null, 4))

const glob = new Glob("./schemas/widgets/**/*.json");

Expand Down
3 changes: 2 additions & 1 deletion scripts/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ if (process.argv[2] != 'quick') {
import "./gen-widgets"
import "./gen-ns-strings"
import "./gen-policies"
import "./gen-enums"


// database construciton
await $`bun drizzle-kit generate && bun drizzle-kit push`;
3 changes: 3 additions & 0 deletions scripts/codegen/schemas/enum.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Type as t } from "@sinclair/typebox"

export const enum_schema = t.Object({})
File renamed without changes.

0 comments on commit 2348683

Please sign in to comment.