Merge pull request #63312 from bruvzg/one_click

[Export] Add one-click deploy over SSH for the desktop exports.
This commit is contained in:
Rémi Verschelde 2023-01-13 18:00:18 +01:00
commit 3dffe0b967
No known key found for this signature in database
GPG key ID: C3336907360768E1
43 changed files with 1391 additions and 186 deletions

View file

@ -34,6 +34,14 @@
#include "core/io/image_loader.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_scale.h"
#include "platform/windows/logo_svg.gen.h"
#include "platform/windows/run_icon_svg.gen.h"
#include "modules/modules_enabled.gen.h" // For svg.
#ifdef MODULE_SVG_ENABLED
#include "modules/svg/image_loader_svg.h"
#endif
Error EditorExportPlatformWindows::_process_icon(const Ref<EditorExportPreset> &p_preset, const String &p_src_path, const String &p_dst_path) {
static const uint8_t icon_size[] = { 16, 32, 48, 64, 128, 0 /*256*/ };
@ -168,9 +176,39 @@ Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset>
}
Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
String pck_path = p_path;
if (p_preset->get("binary_format/embed_pck")) {
pck_path = p_path.get_basename() + ".tmp";
bool export_as_zip = p_path.ends_with("zip");
bool embedded = p_preset->get("binary_format/embed_pck");
String pkg_name;
if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
} else {
pkg_name = "Unnamed";
}
pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
// Setup temp folder.
String path = p_path;
String tmp_dir_path = EditorPaths::get_singleton()->get_cache_dir().path_join(pkg_name);
Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_dir_path);
if (export_as_zip) {
if (tmp_app_dir.is_null()) {
return ERR_CANT_CREATE;
}
if (DirAccess::exists(tmp_dir_path)) {
if (tmp_app_dir->change_dir(tmp_dir_path) == OK) {
tmp_app_dir->erase_contents_recursive();
}
}
tmp_app_dir->make_dir_recursive(tmp_dir_path);
path = tmp_dir_path.path_join(p_path.get_file().get_basename() + ".exe");
}
// Export project.
String pck_path = path;
if (embedded) {
pck_path = pck_path.get_basename() + ".tmp";
}
Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags);
if (p_preset->get("codesign/enable") && err == OK) {
@ -181,7 +219,7 @@ Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset>
}
}
if (p_preset->get("binary_format/embed_pck") && err == OK) {
if (embedded && err == OK) {
Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
err = tmp_dir->rename(pck_path, p_path);
if (err != OK) {
@ -189,6 +227,27 @@ Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset>
}
}
// ZIP project.
if (export_as_zip) {
if (FileAccess::exists(p_path)) {
OS::get_singleton()->move_to_trash(p_path);
}
Ref<FileAccess> io_fa_dst;
zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst);
zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
zip_folder_recursive(zip, tmp_dir_path, "", pkg_name);
zipClose(zip, nullptr);
if (tmp_app_dir->change_dir(tmp_dir_path) == OK) {
tmp_app_dir->erase_contents_recursive();
tmp_app_dir->change_dir("..");
tmp_app_dir->remove(pkg_name);
}
}
return err;
}
@ -199,6 +258,7 @@ String EditorExportPlatformWindows::get_template_file_name(const String &p_targe
List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
List<String> list;
list.push_back("exe");
list.push_back("zip");
return list;
}
@ -212,6 +272,7 @@ bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExpor
void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
EditorExportPlatformPC::get_export_options(r_options);
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64"));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
@ -235,6 +296,29 @@ void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_optio
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
String run_script = "Expand-Archive -LiteralPath '{temp_dir}\\{archive_name}' -DestinationPath '{temp_dir}'\n"
"$action = New-ScheduledTaskAction -Execute '{temp_dir}\\{exe_name}' -Argument '{cmd_args}'\n"
"$trigger = New-ScheduledTaskTrigger -Once -At 00:00\n"
"$settings = New-ScheduledTaskSettingsSet\n"
"$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings\n"
"Register-ScheduledTask godot_remote_debug -InputObject $task -Force:$true\n"
"Start-ScheduledTask -TaskName godot_remote_debug\n"
"while (Get-ScheduledTask -TaskName godot_remote_debug | ? State -eq running) { Start-Sleep -Milliseconds 100 }\n"
"Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue";
String cleanup_script = "Stop-ScheduledTask -TaskName godot_remote_debug -ErrorAction:SilentlyContinue\n"
"Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue\n"
"Remove-Item -Recurse -Force '{temp_dir}'";
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "ssh_remote_deploy/enabled"), false));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/host"), "user@host_ip"));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/port"), "22"));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_ssh", PROPERTY_HINT_MULTILINE_TEXT), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_scp", PROPERTY_HINT_MULTILINE_TEXT), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/run_script", PROPERTY_HINT_MULTILINE_TEXT), run_script));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/cleanup_script", PROPERTY_HINT_MULTILINE_TEXT), cleanup_script));
}
Error EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path, bool p_console_icon) {
@ -659,3 +743,227 @@ Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int6
}
return OK;
}
Ref<Texture2D> EditorExportPlatformWindows::get_run_icon() const {
return run_icon;
}
bool EditorExportPlatformWindows::poll_export() {
Ref<EditorExportPreset> preset;
for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
if (ep->is_runnable() && ep->get_platform() == this) {
preset = ep;
break;
}
}
int prev = menu_options;
menu_options = (preset.is_valid() && preset->get("ssh_remote_deploy/enabled").operator bool());
if (ssh_pid != 0 || !cleanup_commands.is_empty()) {
if (menu_options == 0) {
cleanup();
} else {
menu_options += 1;
}
}
return menu_options != prev;
}
Ref<ImageTexture> EditorExportPlatformWindows::get_option_icon(int p_index) const {
return p_index == 1 ? stop_icon : EditorExportPlatform::get_option_icon(p_index);
}
int EditorExportPlatformWindows::get_options_count() const {
return menu_options;
}
String EditorExportPlatformWindows::get_option_label(int p_index) const {
return (p_index) ? TTR("Stop and uninstall") : TTR("Run on remote Windows system");
}
String EditorExportPlatformWindows::get_option_tooltip(int p_index) const {
return (p_index) ? TTR("Stop and uninstall running project from the remote system") : TTR("Run exported project on remote Windows system");
}
void EditorExportPlatformWindows::cleanup() {
if (ssh_pid != 0 && OS::get_singleton()->is_process_running(ssh_pid)) {
print_line("Terminating connection...");
OS::get_singleton()->kill(ssh_pid);
OS::get_singleton()->delay_usec(1000);
}
if (!cleanup_commands.is_empty()) {
print_line("Stopping and deleting previous version...");
for (const SSHCleanupCommand &cmd : cleanup_commands) {
if (cmd.wait) {
ssh_run_on_remote(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args);
} else {
ssh_run_on_remote_no_wait(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args);
}
}
}
ssh_pid = 0;
cleanup_commands.clear();
}
Error EditorExportPlatformWindows::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
cleanup();
if (p_device) { // Stop command, cleanup only.
return OK;
}
EditorProgress ep("run", TTR("Running..."), 5);
const String dest = EditorPaths::get_singleton()->get_cache_dir().path_join("windows");
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
if (!da->dir_exists(dest)) {
Error err = da->make_dir_recursive(dest);
if (err != OK) {
EditorNode::get_singleton()->show_warning(TTR("Could not create temp directory:") + "\n" + dest);
return err;
}
}
String host = p_preset->get("ssh_remote_deploy/host").operator String();
String port = p_preset->get("ssh_remote_deploy/port").operator String();
if (port.is_empty()) {
port = "22";
}
Vector<String> extra_args_ssh = p_preset->get("ssh_remote_deploy/extra_args_ssh").operator String().split(" ");
Vector<String> extra_args_scp = p_preset->get("ssh_remote_deploy/extra_args_scp").operator String().split(" ");
const String basepath = dest.path_join("tmp_windows_export");
#define CLEANUP_AND_RETURN(m_err) \
{ \
if (da->file_exists(basepath + ".zip")) { \
da->remove(basepath + ".zip"); \
} \
if (da->file_exists(basepath + "_start.ps1")) { \
da->remove(basepath + "_start.ps1"); \
} \
if (da->file_exists(basepath + "_clean.ps1")) { \
da->remove(basepath + "_clean.ps1"); \
} \
return m_err; \
} \
((void)0)
if (ep.step(TTR("Exporting project..."), 1)) {
return ERR_SKIP;
}
Error err = export_project(p_preset, true, basepath + ".zip", p_debug_flags);
if (err != OK) {
DirAccess::remove_file_or_error(basepath + ".zip");
return err;
}
String cmd_args;
{
Vector<String> cmd_args_list;
gen_debug_flags(cmd_args_list, p_debug_flags);
for (int i = 0; i < cmd_args_list.size(); i++) {
if (i != 0) {
cmd_args += " ";
}
cmd_args += cmd_args_list[i];
}
}
const bool use_remote = (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) || (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT);
int dbg_port = EditorSettings::get_singleton()->get("network/debug/remote_port");
print_line("Creating temporary directory...");
ep.step(TTR("Creating temporary directory..."), 2);
String temp_dir;
err = ssh_run_on_remote(host, port, extra_args_ssh, "powershell -command \\\"\\$tmp = Join-Path \\$Env:Temp \\$(New-Guid); New-Item -Type Directory -Path \\$tmp | Out-Null; Write-Output \\$tmp\\\"", &temp_dir);
if (err != OK || temp_dir.is_empty()) {
CLEANUP_AND_RETURN(err);
}
print_line("Uploading archive...");
ep.step(TTR("Uploading archive..."), 3);
err = ssh_push_to_remote(host, port, extra_args_scp, basepath + ".zip", temp_dir);
if (err != OK) {
CLEANUP_AND_RETURN(err);
}
{
String run_script = p_preset->get("ssh_remote_deploy/run_script");
run_script = run_script.replace("{temp_dir}", temp_dir);
run_script = run_script.replace("{archive_name}", basepath.get_file() + ".zip");
run_script = run_script.replace("{exe_name}", basepath.get_file() + ".exe");
run_script = run_script.replace("{cmd_args}", cmd_args);
Ref<FileAccess> f = FileAccess::open(basepath + "_start.ps1", FileAccess::WRITE);
if (f.is_null()) {
CLEANUP_AND_RETURN(err);
}
f->store_string(run_script);
}
{
String clean_script = p_preset->get("ssh_remote_deploy/cleanup_script");
clean_script = clean_script.replace("{temp_dir}", temp_dir);
clean_script = clean_script.replace("{archive_name}", basepath.get_file() + ".zip");
clean_script = clean_script.replace("{exe_name}", basepath.get_file() + ".exe");
clean_script = clean_script.replace("{cmd_args}", cmd_args);
Ref<FileAccess> f = FileAccess::open(basepath + "_clean.ps1", FileAccess::WRITE);
if (f.is_null()) {
CLEANUP_AND_RETURN(err);
}
f->store_string(clean_script);
}
print_line("Uploading scripts...");
ep.step(TTR("Uploading scripts..."), 4);
err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_start.ps1", temp_dir);
if (err != OK) {
CLEANUP_AND_RETURN(err);
}
err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_clean.ps1", temp_dir);
if (err != OK) {
CLEANUP_AND_RETURN(err);
}
print_line("Starting project...");
ep.step(TTR("Starting project..."), 5);
err = ssh_run_on_remote_no_wait(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_start.ps1"), &ssh_pid, (use_remote) ? dbg_port : -1);
if (err != OK) {
CLEANUP_AND_RETURN(err);
}
cleanup_commands.clear();
cleanup_commands.push_back(SSHCleanupCommand(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_clean.ps1")));
print_line("Project started.");
CLEANUP_AND_RETURN(OK);
#undef CLEANUP_AND_RETURN
}
EditorExportPlatformWindows::EditorExportPlatformWindows() {
#ifdef MODULE_SVG_ENABLED
Ref<Image> img = memnew(Image);
const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
ImageLoaderSVG img_loader;
img_loader.create_image_from_string(img, _windows_logo_svg, EDSCALE, upsample, false);
set_logo(ImageTexture::create_from_image(img));
img_loader.create_image_from_string(img, _windows_run_icon_svg, EDSCALE, upsample, false);
run_icon = ImageTexture::create_from_image(img);
#endif
Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
if (theme.is_valid()) {
stop_icon = theme->get_icon(SNAME("Stop"), SNAME("EditorIcons"));
} else {
stop_icon.instantiate();
}
}