Skip to content

Commit 5a29679

Browse files
authored
Reenable Editor Plugin. (#690)
* Add project generation to editor plugin * Generate the templates as part of the Scons build
1 parent 08a7565 commit 5a29679

38 files changed

+558
-616
lines changed

SCsub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import os
2+
import generate_templates
23

34
Import("env")
45

56
java_home = os.environ["JAVA_HOME"]
67

78
module_env = env.Clone()
89

10+
# Generate templates when building the engine.
11+
generate_templates.generate_header_from_files("kt/plugins/godot-intellij-plugin/src/main/resources/template", "src/editor/project/templates.h")
12+
913
# For header includes
1014
module_env.Append(CPPPATH=["src/"])
1115

@@ -23,6 +27,7 @@ if env["platform"] != "android":
2327
module_env.add_source_files(module_env.modules_sources, "register_types.cpp")
2428
module_env.add_source_files(module_env.modules_sources, "src/*.cpp")
2529
module_env.add_source_files(module_env.modules_sources, "src/editor/*.cpp")
30+
module_env.add_source_files(module_env.modules_sources, "src/editor/project/*.cpp")
2631
module_env.add_source_files(module_env.modules_sources, "src/editor/build/*.cpp")
2732
module_env.add_source_files(module_env.modules_sources, "src/editor/dialog/*.cpp")
2833
module_env.add_source_files(module_env.modules_sources, "src/editor/panel/*.cpp")
17.3 KB
Loading
46.5 KB
Loading
90.4 KB
Loading

docs/src/doc/getting-started/setting-up.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
# Setting-up
22

33
This page illustrates how to set-up a project with Godot/Kotlin JVM. Currently, there are
4-
two ways to create a new project: [using a custom IntelliJ IDEA plugin](#setting-up-using-intellij-idea-project-wizard)
4+
three ways to create a new project: [directly from the Godot editor](#setting-up-using-the-godot-editor), [using a custom IntelliJ IDEA plugin](#setting-up-using-intellij-idea-project-wizard)
55
or [manually](#setting-up-manually).
66

7-
The recommended solution is using the custom IntelliJ IDEA plugin.
7+
## Setting-up using the Godot editor.
88

9+
You can simply create a regular new Godot project.
10+
Once done, you can go to `Project/Tools/Kotlin/JVM/Generate JVM Project`.
11+
![Godot menu](../assets/img/editor-plugin/generation_menu.png)
12+
13+
The following choice will appear:
14+
![Project dialog](../assets/img/editor-plugin/generation_choice.png)
15+
16+
After this action, all required files should be generated, and you can safely build the project using the `Build` button at the top-right of the editor window.
17+
![Build button](../assets/img/editor-plugin/build_button.png)
918
## Setting-up using IntelliJ IDEA project wizard
1019

11-
The easiest way to setup a new project is via IntelliJ's project wizard (the user needs to install our IntelliJ IDEA [plugin](./requirements.md#intellij-idea-plugin)).
20+
This is the recommended solution as it creates the entire project in one action and import it in your IDE.
21+
It's done via IntelliJ's project wizard (the user needs to install our IntelliJ IDEA [plugin](./requirements.md#intellij-idea-plugin)).
1222

1323
### Installing IntelliJ IDEA's plugin
1424

generate_templates.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import base64
2+
import os
3+
4+
# Define maximum length for each chunk
5+
MAX_CHUNK_SIZE = 8 * 1024 # 8KB
6+
7+
def encode_file_to_base64(file_path):
8+
"""Encode the content of a file to a Base64 string."""
9+
with open(file_path, 'rb') as file:
10+
file_content = file.read()
11+
encoded_content = base64.b64encode(file_content).decode('utf-8')
12+
return encoded_content
13+
14+
def split_string_into_chunks(string, chunk_size):
15+
"""Split a string into chunks of a specified size."""
16+
return [string[i:i + chunk_size] for i in range(0, len(string), chunk_size)]
17+
18+
def generate_header_from_files(directory, header_file):
19+
"""Generate a C++ header file with Base64 encoded file contents from a directory and subdirectories."""
20+
file_names = []
21+
file_contents = []
22+
file_is_binary = []
23+
24+
with open(header_file, 'w') as header:
25+
header.write(f'#ifdef TOOLS_ENABLED \n\n')
26+
header.write(f'// Auto-generated templates from {directory} directory \n\n')
27+
header.write("#ifndef FILE_CONTENTS_H\n")
28+
header.write("#define FILE_CONTENTS_H\n\n")
29+
30+
for root, _, files in os.walk(directory):
31+
for file_name in files:
32+
# Only process files with .template or .godot_template extensions
33+
if not(file_name.endswith('.template')) and not(file_name.endswith('.godot_template')):
34+
continue
35+
36+
# Compute relative path
37+
relative_path = os.path.relpath(os.path.join(root, file_name), directory)
38+
encoded_content = encode_file_to_base64(os.path.join(root, file_name))
39+
40+
# Split the encoded content into chunks
41+
chunks = split_string_into_chunks(encoded_content, MAX_CHUNK_SIZE)
42+
43+
# Create the variable names
44+
final_file_name = relative_path.replace('.template', '').replace('.godot_template', '').replace('\\', '/')
45+
var_name = final_file_name.replace('.', '_').replace('-', '_').replace('/', '_').replace('__', '_')
46+
name_var_name = f"{var_name}_file_name"
47+
file_var_name = f"{var_name}_file_content"
48+
49+
file_names.append(name_var_name)
50+
file_contents.append(file_var_name)
51+
file_is_binary.append('true' if '.jar' in file_name else 'false')
52+
53+
# Write the file name constant
54+
header.write(f'constexpr const char* {name_var_name} = R"({final_file_name})";\n')
55+
# Write the chunks with automatic concatenation
56+
header.write(f'constexpr const char* {file_var_name} = ')
57+
if chunks:
58+
chunk_lines = [f'"{chunk}"' for chunk in chunks]
59+
else:
60+
chunk_lines = ['""']
61+
header.write(' '.join(chunk_lines) + ';\n\n')
62+
63+
# Generate arrays for file names and contents
64+
header.write(f'constexpr const int number_of_files = {len(file_names)};\n')
65+
66+
header.write('constexpr const bool file_is_binary[] = {')
67+
header.write(', '.join(file_is_binary))
68+
header.write('};\n')
69+
70+
header.write('constexpr const char* file_names[] = {')
71+
header.write(', '.join(file_names))
72+
header.write('};\n')
73+
74+
header.write('constexpr const char* file_contents[] = {')
75+
header.write(', '.join(file_contents))
76+
header.write('};\n\n')
77+
78+
header.write("#endif // FILE_CONTENTS_H\n\n")
79+
80+
header.write("#endif// TOOLS_ENABLED\n")
81+
82+
print(f"{header_file} generated successfully.")
83+
84+
if __name__ == "__main__":
85+
directory = "kt/plugins/godot-intellij-plugin/src/main/resources/template" # Change this to your relative directory
86+
header_file = "src/editor/project/templates.h" # Output header file name
87+
generate_header_from_files(directory, header_file)

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/module/GodotModuleBuilder.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ class GodotModuleBuilder : ModuleBuilder(), ModuleBuilderListener {
198198
File(basePath, "src/main/kotlin/$packagePathAsFolderStructure").mkdirs()
199199

200200
if (wizardContext.isCreatingNewProject) {
201-
copyTemplateFile(basePath, "gradle/wrapper/gradle-wrapper.jar")
202-
copyTemplateFile(basePath, "gradle/wrapper/gradle-wrapper.properties")
203-
copyTemplateFile(basePath, "gradlew")
204-
copyTemplateFile(basePath, "gradlew.bat")
205-
copyTemplateFile(basePath, "build.gradle.kts") { outFile ->
201+
copyTemplateFile(basePath, "gradle/wrapper/gradle-wrapper.jar.template")
202+
copyTemplateFile(basePath, "gradle/wrapper/gradle-wrapper.properties.template")
203+
copyTemplateFile(basePath, "gradlew.template")
204+
copyTemplateFile(basePath, "gradlew.bat.template")
205+
copyTemplateFile(basePath, "build.gradle.kts.template") { outFile ->
206206
outFile.writeText(
207207
outFile
208208
.readText()
@@ -219,7 +219,7 @@ class GodotModuleBuilder : ModuleBuilder(), ModuleBuilderListener {
219219
.replace("IS_IOS_ENABLED", wizardContext.getUserData(isIOSEnabledKey)?.toString() ?: "false")
220220
)
221221
}
222-
copyTemplateFile(basePath, "settings.gradle.kts") { outFile ->
222+
copyTemplateFile(basePath, "settings.gradle.kts.template") { outFile ->
223223
outFile.writeText(
224224
outFile
225225
.readText()
@@ -240,7 +240,7 @@ class GodotModuleBuilder : ModuleBuilder(), ModuleBuilderListener {
240240

241241
copyTemplateFile(
242242
basePath,
243-
"build.gradle.kts.submodule",
243+
"build.gradle.kts.submodule.intellij_template",
244244
"build.gradle.kts"
245245
) { outFile ->
246246
outFile.writeText(
@@ -269,44 +269,44 @@ class GodotModuleBuilder : ModuleBuilder(), ModuleBuilderListener {
269269
)
270270
}
271271
}
272-
copyTemplateFile(basePath, "src/main/kotlin/$packagePathAsFolderStructure/Simple.kt") { outFile ->
272+
copyTemplateFile(basePath, "Simple.kt.intellij_template", "src/main/kotlin/$packagePathAsFolderStructure/Simple.kt") { outFile ->
273273
outFile.writeText(
274274
outFile
275275
.readText()
276276
.replace("GROUP_ID", group)
277277
.replace("ARTEFACT_ID", artifact)
278278
)
279279
}
280-
copyTemplateFile(basePath, "icon.svg")
281-
copyTemplateFile(basePath, "project.godot") { outFile ->
280+
copyTemplateFile(basePath, "icon.svg.intellij_template")
281+
copyTemplateFile(basePath, "project.godot.intellij_template") { outFile ->
282282
outFile.writeText(
283283
outFile
284284
.readText()
285285
.replace("PROJECT_NAME", module.project.name)
286286
)
287287
}
288-
copyTemplateFile(basePath, ".gitattributes")
289-
copyTemplateFile(basePath, ".gitignore")
288+
copyTemplateFile(basePath, ".gitattributes.template")
289+
copyTemplateFile(basePath, ".gitignore.template")
290290
}
291291

292292
private fun copyTemplateFile(
293293
baseDir: String,
294-
sourceFileName: String,
295-
targetFileName: String = sourceFileName.substringAfterLast("/"),
294+
sourcePath: String,
295+
targetPath: String = sourcePath.removeSuffix(".template").removeSuffix(".intellij_template"),
296296
modifyOutFile: (File) -> Unit = {}
297297
) {
298-
val templateFile = "${sourceFileName.substringAfterLast("/")}.template"
299-
val parentDir = sourceFileName.substringBeforeLast("/")
300298

301-
val parentFile = if (parentDir.isNotEmpty() && parentDir != sourceFileName) {
299+
val parentDir = targetPath.substringBeforeLast("/")
300+
val targetDir = if (parentDir.isNotEmpty() && parentDir != targetPath) {
302301
File(baseDir, parentDir).apply {
303302
mkdirs()
304303
}
305304
} else File(baseDir)
306305

307-
val outFile = File(parentFile, targetFileName)
306+
val targetFile = targetPath.substringAfterLast("/")
307+
val outFile = File(targetDir, targetFile)
308308

309-
this::class.java.getResourceAsStream("/template/$templateFile").use { inputStream ->
309+
this::class.java.getResourceAsStream("/template/$sourcePath").use { inputStream ->
310310
inputStream?.copyTo(outFile.outputStream())
311311
}
312312

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
# Ignore Gradle project-specific cache directory
1+
.godot/
2+
/android/
23
.gradle
3-
4-
# Ignore Gradle build output directory
5-
build
6-
7-
# Ignore Intellij directory
4+
.kotlin
85
.idea
9-
10-
# Ignore embedded jre
11-
jre
6+
build
7+
jre

kt/plugins/godot-intellij-plugin/src/main/resources/template/Simple.kt.template renamed to kt/plugins/godot-intellij-plugin/src/main/resources/template/Simple.kt.intellij_template

File renamed without changes.

kt/plugins/godot-intellij-plugin/src/main/resources/template/build.gradle.kts.submodule.template renamed to kt/plugins/godot-intellij-plugin/src/main/resources/template/build.gradle.kts.submodule.intellij_template

File renamed without changes.

0 commit comments

Comments
 (0)