Skip to content

Commit f0226ce

Browse files
committed
refactor project, paths and definitionbuilder classes
extract functionality into classes and rework import structure
1 parent 60db977 commit f0226ce

30 files changed

+434
-402
lines changed

dummy/_godot_defs/dynamic/@actions.d.ts

-1
This file was deleted.

dummy/_godot_defs/dynamic/@asset_paths.d.ts

-11
This file was deleted.

dummy/_godot_defs/dynamic/@groups.d.ts

-2
This file was deleted.

dummy/_godot_defs/dynamic/@scenes.d.ts

-1
This file was deleted.

generate_library_defs/generate_base.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs"
22
import path from "path"
33

4-
import { TsGdProjectClass } from "../project/project"
4+
import { Paths } from "../project"
55

66
import { ArrayDefinition } from "./custom_defs/array_def"
77
import { DictionaryDefinition } from "./custom_defs/dictionary_def"
@@ -266,9 +266,9 @@ declare class Signal<T extends (...args: any[]) => any = () => void> {
266266
}
267267
`
268268

269-
export const buildBase = () => {
269+
export default function writeBaseDefinitions(paths: Paths) {
270270
fs.writeFileSync(
271-
path.join(TsGdProjectClass.Paths.staticGodotDefsPath, "@base.d.ts"),
271+
path.join(paths.staticGodotDefsPath, "@base.d.ts"),
272272
baseFileContent
273273
)
274274
}

generate_library_defs/generate_gdscript_lib.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
formatJsDoc,
77
godotTypeToTsType,
88
sanitizeGodotNameForTs,
9-
} from "./generate_library"
9+
} from "./generation_utils"
1010

1111
export type GodotXMLMethod = {
1212
$: {
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
export function sanitizeGodotNameForTs(
2+
name: string,
3+
type: "argument" | "property"
4+
): string {
5+
if (
6+
name === "with" ||
7+
name === "var" ||
8+
name === "class" ||
9+
name === "enum" ||
10+
name === "default" ||
11+
name === "in"
12+
) {
13+
if (type === "argument") {
14+
return "_" + name
15+
} else {
16+
return `"${name}"`
17+
}
18+
}
19+
20+
// for enum names in @GlobalScope
21+
name = name.replace(".", "_")
22+
23+
// Bizarre case in SliderJoint3D.xml
24+
if (name.includes("/")) {
25+
if (type === "argument") {
26+
return name.replace("/", "_")
27+
} else {
28+
return `"${name}"`
29+
}
30+
}
31+
32+
return name
33+
}
34+
35+
export function godotTypeToTsType(godotType: string): string {
36+
if (godotType === "int") {
37+
return "int"
38+
}
39+
40+
if (godotType === "float") {
41+
return "float"
42+
}
43+
44+
if (godotType === "bool") {
45+
return "boolean"
46+
}
47+
48+
if (godotType === "Array") {
49+
return "any[]"
50+
}
51+
52+
if (godotType === "PackedScene") {
53+
return "PackedScene<any>"
54+
}
55+
56+
if (godotType === "Variant") {
57+
return "any"
58+
}
59+
60+
if (godotType === "String") {
61+
return "string"
62+
}
63+
64+
if (godotType.startsWith("Transform2D")) {
65+
return "Transform2D"
66+
}
67+
68+
if (godotType === "Dictionary") {
69+
return "Dictionary<any, any>"
70+
}
71+
72+
if (godotType === "NodePath") {
73+
// TODO
74+
return "NodePathType"
75+
}
76+
77+
if (godotType.match(/^[0-9]+$/)) {
78+
return "int"
79+
}
80+
81+
return godotType
82+
}
83+
84+
export function formatJsDoc(input: string): string {
85+
if (!input) {
86+
return `/** No documentation provided. */`
87+
}
88+
89+
let lines = input.split("\n")
90+
91+
if (lines.length === 1) {
92+
return `/** ${input} */`
93+
}
94+
95+
const indentationLength = lines[1].length - lines[1].trimStart().length
96+
97+
// All lines are indented except the first one for some reason.
98+
lines = [
99+
lines[0],
100+
...lines.slice(1).map((line) => line.slice(indentationLength)),
101+
]
102+
103+
lines = lines.filter((line) => line.trim() !== "")
104+
105+
let result = "/**\n"
106+
107+
let insideCodeBlock = false
108+
109+
for (let line of lines) {
110+
if (line.includes("[codeblock]")) {
111+
result += " * @example \n"
112+
insideCodeBlock = true
113+
}
114+
115+
if (line.includes("[/codeblock]")) {
116+
result += " * @summary \n"
117+
insideCodeBlock = false
118+
}
119+
120+
if (line.includes("[codeblocks]")) {
121+
result += " * @example \n"
122+
insideCodeBlock = true
123+
}
124+
125+
if (line.includes("[/codeblocks]")) {
126+
result += " * @summary \n"
127+
insideCodeBlock = false
128+
}
129+
130+
line = line.replaceAll("[gdscript]", "")
131+
line = line.replaceAll("[/gdscript]", "")
132+
line = line.replaceAll("[csharp]", "")
133+
line = line.replaceAll("[/csharp]", "")
134+
line = line.replaceAll("[b]", "**")
135+
line = line.replaceAll("[/b]", "**")
136+
line = line.replaceAll("[i]", "**")
137+
line = line.replaceAll("[/i]", "**")
138+
line = line.replaceAll("[code]", "`")
139+
line = line.replaceAll("[/code]", "`")
140+
line = line.replaceAll("[codeblock]", "")
141+
line = line.replaceAll("[/codeblock]", "")
142+
line = line.replaceAll("[codeblocks]", "")
143+
line = line.replaceAll("[/codeblocks]", "")
144+
145+
// This is the most fun edge case of all time - in RichTextLabel.xml
146+
line = line.replaceAll("*/", "")
147+
148+
result += " * " + line + "\n" + (!insideCodeBlock ? " *\n" : "")
149+
}
150+
151+
result += "*/"
152+
153+
return result
154+
}

generate_library_defs/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./library_builder"
2+
export { default } from "./library_builder"

0 commit comments

Comments
 (0)