Skip to content

Commit 32e15ed

Browse files
authored
Improve Gradle Task Runner (#819)
* Add streaming to gradle output * Add multiple gradle tasks
1 parent 41ea6ed commit 32e15ed

File tree

9 files changed

+283
-173
lines changed

9 files changed

+283
-173
lines changed

src/editor/build/build_manager.cpp

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

src/editor/build/build_manager.h

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
#ifdef TOOLS_ENABLED
3+
4+
#include "../godot_kotlin_jvm_editor.h"
5+
#include "editor/strings.h"
6+
#include "gradle_task_runner.h"
7+
#include "logging.h"
8+
9+
#include <core/config/project_settings.h>
10+
11+
Mutex build_mutex {};
12+
13+
String get_build_gradle_path() {
14+
String gradle_wrapper_path {ProjectSettings::get_singleton()->globalize_path(GLOBAL_GET(gradle_dir))};
15+
return gradle_wrapper_path.path_join("build.gradle.kts");
16+
}
17+
18+
String get_gradlew_path() {
19+
#if defined _WIN32 || defined _WIN64
20+
String gradle_wrapper {"gradlew.bat"};
21+
#else
22+
String gradle_wrapper {"gradlew"};
23+
#endif
24+
25+
String gradle_wrapper_path {ProjectSettings::get_singleton()->globalize_path(GLOBAL_GET(gradle_dir))};
26+
return gradle_wrapper_path.path_join(gradle_wrapper);
27+
}
28+
29+
Error GradleTaskRunner::run_task(int task_id, String& log, bool blocking) {
30+
JVM_ERR_FAIL_COND_V_MSG(
31+
!FileAccess::create(FileAccess::AccessType::ACCESS_RESOURCES)->file_exists(get_build_gradle_path()),
32+
Error::ERR_FILE_NOT_FOUND,
33+
missing_gradle_project
34+
);
35+
36+
List<String> args {};
37+
switch (task_id) {
38+
case Task::BUILD_DEBUG:
39+
args.push_back("build");
40+
break;
41+
case Task::BUILD_RELEASE:
42+
args.push_back("build");
43+
args.push_back("-Prelease=true");
44+
break;
45+
case Task::GENERATE_EMBEDDED_JVM:
46+
args.push_back("generateEmbeddedJre");
47+
break;
48+
default:
49+
return Error::ERR_INVALID_PARAMETER;
50+
}
51+
52+
String gradlew_path = get_gradlew_path();
53+
54+
if (blocking) {
55+
int exit_code;
56+
Error result = OS::get_singleton()->execute(gradlew_path, args, &log, &exit_code, true, &build_mutex, false);
57+
return result;
58+
} else {
59+
Dictionary info = OS::get_singleton()->execute_with_pipe(gradlew_path, args, /*blocking=*/false);
60+
61+
if (info.is_empty()) { JVM_ERR_FAIL_V_MSG(Error::ERR_CANT_CREATE, "Failed to start process"); }
62+
63+
log = vformat("Running gradle task: %s", args.get(0));
64+
stdio = info["stdio"];
65+
stderr_io = info["stderr"];
66+
pid = info["pid"];
67+
}
68+
69+
return Error::OK;
70+
}
71+
72+
GradleTaskRunner& GradleTaskRunner::get_instance() {
73+
static GradleTaskRunner instance;
74+
return instance;
75+
}
76+
77+
bool GradleTaskRunner::is_task_started() {
78+
return pid > -1;
79+
}
80+
81+
bool GradleTaskRunner::is_task_terminated() {
82+
bool ret = !OS::get_singleton()->is_process_running(pid);
83+
if (ret) { reset(); }
84+
return ret;
85+
}
86+
87+
void GradleTaskRunner::get_task_output(String& log, String& error) {
88+
if (stdio.is_valid()) {
89+
// keep reading until no full line is available
90+
Error err;
91+
do {
92+
String line = stdio->get_line();
93+
err = stdio->get_error();
94+
if (!line.is_empty()) { log += line + "\n"; }
95+
} while (err == OK);
96+
}
97+
98+
if (stderr_io.is_valid()) {
99+
// keep reading until no full line is available
100+
Error err;
101+
do {
102+
String line = stderr_io->get_line();
103+
err = stderr_io->get_error();
104+
if (!line.is_empty()) { error += line + "\n"; }
105+
} while (err == OK);
106+
}
107+
}
108+
109+
void GradleTaskRunner::reset() {
110+
stdio = {};
111+
stderr_io = {};
112+
pid = -1;
113+
}
114+
115+
void GradleTaskRunner::cleanup() {
116+
reset();
117+
}
118+
119+
#endif // TOOLS_ENABLED

src/editor/build/gradle_task_runner.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
#ifdef TOOLS_ENABLED
3+
4+
#ifndef GODOT_JVM_BUILD_MANAGER_H
5+
#define GODOT_JVM_BUILD_MANAGER_H
6+
7+
#include <core/object/worker_thread_pool.h>
8+
#include <core/os/os.h>
9+
#include <core/os/thread.h>
10+
11+
class GradleTaskRunner {
12+
public:
13+
enum Task {
14+
BUILD_DEBUG,
15+
BUILD_RELEASE,
16+
GENERATE_EMBEDDED_JVM
17+
};
18+
19+
private:
20+
Ref<FileAccess> stdio;
21+
Ref<FileAccess> stderr_io;
22+
int pid = -1;
23+
24+
GradleTaskRunner() = default;
25+
26+
void reset();
27+
28+
public:
29+
static GradleTaskRunner& get_instance();
30+
void cleanup();
31+
32+
GradleTaskRunner(const GradleTaskRunner&) = delete;
33+
GradleTaskRunner& operator=(const GradleTaskRunner&) = delete;
34+
35+
Error run_task(int task_id, String& log, bool blocking);
36+
bool is_task_started();
37+
bool is_task_terminated();
38+
void get_task_output(String& log, String& error);
39+
};
40+
41+
#endif // GODOT_JVM_BUILD_MANAGER_H
42+
43+
#endif // TOOLS_ENABLED

src/editor/dialog/build_dialog.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,61 @@
33

44
#include "build_dialog.h"
55

6-
#include "editor/build/build_manager.h"
6+
#include "editor/build/gradle_task_runner.h"
77
#include "logging.h"
88

99
#include <editor/themes/editor_scale.h>
1010

11-
BuildDialog::BuildDialog() : scroll_container(memnew(ScrollContainer)), log_label(memnew(Label)) {
12-
set_title("Godot Kotlin/JVM Gradle build");
11+
TaskDialog::TaskDialog() :
12+
scroll_container(memnew(ScrollContainer)),
13+
log_label(memnew(Label)),
14+
progress_bar(memnew(ProgressBar)),
15+
vertical_container(memnew(VBoxContainer)) {
16+
set_title("Gradle Task Runner");
1317
}
1418

15-
void BuildDialog::set_scrollbar_at_bottom() {
19+
void TaskDialog::set_scrollbar_at_bottom() {
1620
scroll_container->set_v_scroll(static_cast<int>(scroll_container->get_v_scroll_bar()->get_max()));
1721
}
1822

19-
void BuildDialog::update_state(String log) {
20-
log_label->set_text(log);
21-
JVM_LOG_INFO(log);
22-
23+
void TaskDialog::make_appear() {
24+
log_label->set_text("");
25+
set_transient(true);
26+
set_exclusive(true);
27+
progress_bar->show();
2328
popup_centered();
29+
}
30+
31+
void TaskDialog::update_state(String log) {
32+
if (log.is_empty()) { return; }
33+
log_label->set_text(log_label->get_text() + log);
2434

2535
StringName signal = SNAME("draw");
26-
Callable callback = callable_mp(this, &BuildDialog::set_scrollbar_at_bottom);
36+
Callable callback = callable_mp(this, &TaskDialog::set_scrollbar_at_bottom);
2737

2838
if (!scroll_container->is_connected(signal, callback)) {
2939
scroll_container->connect(signal, callback, CONNECT_ONE_SHOT);
3040
}
3141
}
3242

33-
void BuildDialog::_notification(int notification) {
43+
void TaskDialog::stop() {
44+
progress_bar->hide();
45+
set_exclusive(false);
46+
}
47+
48+
void TaskDialog::_notification(int notification) {
3449
if (notification != NOTIFICATION_ENTER_TREE) { return; }
3550
scroll_container->set_custom_minimum_size(Size2 {600, 400} * EDSCALE);
36-
add_child(scroll_container);
51+
vertical_container->add_child(scroll_container);
3752

3853
log_label->set_h_size_flags(Control::SizeFlags::SIZE_EXPAND_FILL);
3954
scroll_container->add_child(log_label);
55+
56+
progress_bar->set_editor_preview_indeterminate(true);
57+
progress_bar->set_indeterminate(true);
58+
vertical_container->add_child(progress_bar);
59+
60+
add_child(vertical_container);
4061
}
4162

42-
#endif// TOOLS_ENABLED
63+
#endif // TOOLS_ENABLED

src/editor/dialog/build_dialog.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
#define GODOT_JVM_BUILD_DIALOG_H
66

77
#include <scene/gui/dialogs.h>
8+
#include <scene/gui/progress_bar.h>
89
#include <scene/gui/scroll_container.h>
910

10-
class BuildDialog : public AcceptDialog {
11-
GDCLASS(BuildDialog, AcceptDialog)
11+
class TaskDialog : public AcceptDialog {
12+
GDCLASS(TaskDialog, AcceptDialog)
1213

1314
ScrollContainer* scroll_container;
1415
Label* log_label;
16+
ProgressBar* progress_bar;
17+
VBoxContainer* vertical_container;
1518

1619
public:
17-
BuildDialog();
20+
TaskDialog();
1821

1922
void set_scrollbar_at_bottom();
23+
void make_appear();
2024
void update_state(String log);
25+
void stop();
2126
void _notification(int notification);
2227
};
2328

24-
#endif// GODOT_JVM_BUILD_DIALOG_H
29+
#endif // GODOT_JVM_BUILD_DIALOG_H
2530

26-
#endif// TOOLS_ENABLED
31+
#endif // TOOLS_ENABLED

0 commit comments

Comments
 (0)