Skip to content

Instantly share code, notes, and snippets.

@imjp94
Last active January 10, 2023 18:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save imjp94/d4ec7e4f2be2b09e4804439c35649ed5 to your computer and use it in GitHub Desktop.
Save imjp94/d4ec7e4f2be2b09e4804439c35649ed5 to your computer and use it in GitHub Desktop.
Build script to help exporting Godot project.
tool
extends SceneTree
const PLATFORM_ANDROID = "Android"
const PLATFORM_HTML5 = "HTML5"
const PLATFORM_IOS = "iOS"
const PLATFORM_LINUX = "Linux/X11"
const PLATFORM_MAC = "Mac OSX"
const PLATFORM_UWP = "UWP"
const PLATFORM_WINDOWS = "Windows Desktop"
const ALL_PLATFORMS = [
PLATFORM_ANDROID, PLATFORM_HTML5, PLATFORM_IOS, PLATFORM_LINUX, PLATFORM_MAC, PLATFORM_UWP, PLATFORM_WINDOWS
]
var include_platforms = []
var cwd = ProjectSettings.globalize_path("res://")
var _start_time = 0
# Run "godot --no-window -s build.gd" to build. Pass platform names(case-sensitive), otherwise, all preset will be exported.
# Always "export" runnable("Export Project" in editor) and "pack" non-runnable("Export PCK/ZIP" in editor)
# Export will be ignored if "export_path" is not set.
func _initialize():
var args = OS.get_cmdline_args()
# Trim unwanted args passed to godot executable
for arg in Array(args):
args.remove(0)
if "build.gd" in arg:
break
if args.empty():
include_platforms = ALL_PLATFORMS.duplicate()
else:
include_platforms = []
for arg in args:
var platform = platform_alias_to_key(arg)
if platform:
include_platforms.append(platform)
print("Included platforms: ", include_platforms)
_start_time = OS.get_system_time_msecs()
var export_preset = ConfigFile.new()
var result = export_preset.load("res://export_presets.cfg")
if result != OK:
printerr("Failed to load export_presets.cfg(%d)" % result)
return
var sections = []
# Filter export & Remove {SECTION_NAME}.option
for section in export_preset.get_sections():
if "options" in section:
continue
var name = export_preset.get_value(section, "name", "")
var platform = export_preset.get_value(section, "platform", null)
var export_path = export_preset.get_value(section, "export_path", null)
if not name:
printerr("Nameless export at %s" % section)
continue
if not platform:
printerr("Skip \"%s\", undefined platform" % name)
continue
if not (platform in include_platforms):
# print("Skip %s from %s" % [name, platform])
continue
if not export_path:
printerr("Skip \"%s\", undefined export_path" % name)
continue
sections.append(section)
print("%d export found:" % sections.size())
for section in sections:
var name = export_preset.get_value(section, "name", "")
var runnable = export_preset.get_value(section, "runnable", false)
print("- %s %s" % [name, "(Runnable)" if runnable else ""])
print("\n")
for section in sections:
var name = export_preset.get_value(section, "name", null)
var runnable = export_preset.get_value(section, "runnable", false)
var export_path = export_preset.get_value(section, "export_path", null)
var cmd
if runnable:
cmd = "godot --no-window --quiet --export-debug \"%s\"" % name
print("Exporting %s..." % name)
else:
cmd = "godot --no-window --quiet --export-pack \"%s\"" % name
print("Packing %s..." % name)
result = execute(cmd)
if result == OK:
if runnable:
print("Successfully export %s to: \n- %s" % [name, export_path])
else:
print("Successfully packed %s to: \n- %s" % [name, export_path])
else:
print("Failed to export %s (%d)" % [name, result])
print("\n")
print("Finished, elapsed %.3fs" % ((OS.get_system_time_msecs() - _start_time) / 1000.0))
quit()
func execute(command, blocking=true, output=[], read_stderr=false):
var cmd = "cd %s && %s" % [cwd, command]
# NOTE: OS.execute() seems to ignore read_stderr
var exit = FAILED
match OS.get_name():
"Windows":
cmd = cmd if read_stderr else "%s 2> nul" % cmd
# print("Execute \"%s\"" % cmd)
exit = OS.execute("cmd", ["/C", cmd], blocking, output, read_stderr)
"X11", "OSX", "Server":
cmd = cmd if read_stderr else "%s 2>/dev/null" % cmd
# print("Execute \"%s\"" % cmd)
exit = OS.execute("bash", ["-c", cmd], blocking, output, read_stderr)
var unhandled_os:
printerr("Unexpected OS: %s" % unhandled_os)
# print("Execution ended(code:%d): %s" % [exit, output])
return exit
func platform_alias_to_key(v):
match v:
PLATFORM_ANDROID, "android":
return PLATFORM_ANDROID
PLATFORM_HTML5, "html", "html5":
return PLATFORM_HTML5
PLATFORM_IOS, "ios":
return PLATFORM_IOS
PLATFORM_LINUX, "linux", "x11":
return PLATFORM_LINUX
PLATFORM_MAC, "mac":
return PLATFORM_MAC
PLATFORM_UWP, "uwp":
return PLATFORM_UWP
PLATFORM_WINDOWS, "window", "windows":
return PLATFORM_WINDOWS
@imjp94
Copy link
Author

imjp94 commented Jan 7, 2023

Build script to help exporting Godot project. Run godot --no-window -s build.gd to build.
Filter export by passing platform name as argument, for example, godot --no-window -s build.gd windows

Always "export" runnable("Export Project" in editor) and "pack" non-runnable("Export PCK/ZIP" in editor).
Export will be ignored if export_path is not set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment