Skip to content

Commit 0089e51

Browse files
author
YesDrX
committed
add macro to rename enum fields to match original bindings
1 parent 6f27ff2 commit 0089e51

File tree

5 files changed

+271
-137
lines changed

5 files changed

+271
-137
lines changed

scripts/generate_bindings.nims

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ exec cmd
3737

3838
## Step 2: add the following code to include webui/bindings_include.nim, which defines some necessary macros and types
3939
# fix the generated bindings file
40-
let code_to_inject = """
40+
let code_prefix = """
4141
## This file is automatically generated by command : "nim scripts/generate_bindings.nims"
4242
## Do not edit this file directly
4343
##
4444
45-
include ./bindings_include.nim
45+
include ./bindings_prefix.nim
4646
4747
"""
4848

49+
let code_suffix = """
50+
51+
include ./bindings_suffix.nim
52+
"""
53+
4954
### Step 3: remove compiler related "when defined" blocks; add webui pragma; fix function names; fix callback annotations
5055

5156
## blocks like when defined(_WIN32) are commented out
@@ -134,13 +139,27 @@ proc fixCallbackAnnotation(code: string): string =
134139
if prev_end < code.len:
135140
result &= code[prev_end ..< code.len]
136141

142+
## add enum pragma to generate legacy constants
143+
proc addEnumPragma(code: string): string =
144+
code.replace(
145+
"* = enum", "* {.renameEnumFields.} = enum"
146+
).replace(
147+
"WEBUI_EVENT_", "WEBUI_EVENTS_"
148+
)
149+
150+
# code.replace(
151+
# "WEBUI_EVENT_", "WEBUI_EVENTS_"
152+
# )
153+
137154
## fix the generated code
138-
let modified_code = (code_to_inject & readFile(bindings_path)).removeWhenDefined(
155+
let modified_code = (code_prefix & readFile(bindings_path) & code_suffix).removeWhenDefined(
139156
).addWebuiPragma(
140157
).fixFunctionName(
141158
).fixCallbackAnnotation(
159+
).addEnumPragma(
142160
)
143161

162+
144163
## output the final code
145164
writeFile(
146165
bindings_path,

webui/bindings.nim

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Do not edit this file directly
33
##
44

5-
include ./bindings_include.nim
5+
include ./bindings_prefix.nim
66

77
const
88
WEBUI_VERSION* = "2.5.0-beta.3" ##
@@ -64,7 +64,7 @@ const
6464
# WEBUI_PCLOSE* = pclose
6565
# WEBUI_MAX_PATH* = PATH_MAX
6666
type
67-
WebuiBrowser* = enum ## -- Enums ---------------------------
67+
WebuiBrowser* {.renameEnumFields.} = enum ## -- Enums ---------------------------
6868
NoBrowser = 0, ## 0. No web browser
6969
AnyBrowser = 1, ## 1. Default recommended web browser
7070
Chrome, ## 2. Google Chrome
@@ -82,28 +82,28 @@ type
8282

8383

8484
type
85-
WebuiRuntime* = enum
85+
WebuiRuntime* {.renameEnumFields.} = enum
8686
None = 0, ## 0. Prevent WebUI from using any runtime for .js and .ts files
8787
Deno, ## 1. Use Deno runtime for .js and .ts files
8888
NodeJS, ## 2. Use Nodejs runtime for .js files
8989
Bun ## 3. Use Bun runtime for .js and .ts files
9090

9191

9292
type
93-
WebuiEvent* = enum
94-
WEBUI_EVENT_DISCONNECTED = 0, ## 0. Window disconnection event
95-
WEBUI_EVENT_CONNECTED, ## 1. Window connection event
96-
WEBUI_EVENT_MOUSE_CLICK, ## 2. Mouse click event
97-
WEBUI_EVENT_NAVIGATION, ## 3. Window navigation event
98-
WEBUI_EVENT_CALLBACK ## 4. Function call event
93+
WebuiEvent* {.renameEnumFields.} = enum
94+
WEBUI_EVENTS_DISCONNECTED = 0, ## 0. Window disconnection event
95+
WEBUI_EVENTS_CONNECTED, ## 1. Window connection event
96+
WEBUI_EVENTS_MOUSE_CLICK, ## 2. Mouse click event
97+
WEBUI_EVENTS_NAVIGATION, ## 3. Window navigation event
98+
WEBUI_EVENTS_CALLBACK ## 4. Function call event
9999

100100

101101
type ## Control if `webui_show()`, `webui_show_browser()` and
102102
## `webui_show_wv()` should wait for the window to connect
103103
## before returns or not.
104104
##
105105
## Default: True
106-
WebuiConfig* = enum
106+
WebuiConfig* {.renameEnumFields.} = enum
107107
show_wait_connection = 0, ## Control if WebUI should block and process the UI events
108108
## one a time in a single thread `True`, or process every
109109
## event in a new non-blocking thread `False`. This updates
@@ -1224,4 +1224,5 @@ proc interface_script_client*(window: csize_t; event_number: csize_t;
12241224
## @return Returns True if there is no execution error
12251225
##
12261226
## @example bool err = webui_script_client(e, "return 4 + 6;", 0, myBuffer, myBufferSize);
1227-
##
1227+
##
1228+
include ./bindings_suffix.nim

webui/bindings_include.nim

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)