mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:07:35 +00:00
HackStudio: Add a simple "Build" action
Pressing Ctrl+B now invokes "make" in the project directory and routes the output from the make command to the little terminal widget.
This commit is contained in:
parent
5a83e053b3
commit
654ffdef91
3 changed files with 56 additions and 12 deletions
|
@ -2,17 +2,53 @@
|
|||
#include <AK/String.h>
|
||||
#include <LibCore/CConfigFile.h>
|
||||
#include <LibGUI/GBoxLayout.h>
|
||||
#include <LibGUI/GMessageBox.h>
|
||||
#include <LibVT/TerminalWidget.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void run_command(int ptm_fd, const String& command)
|
||||
void TerminalWrapper::run_command(const String& command)
|
||||
{
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
if (m_pid != -1) {
|
||||
GMessageBox::show(
|
||||
"A command is already running in this TerminalWrapper",
|
||||
"Can't run command",
|
||||
GMessageBox::Type::Error,
|
||||
GMessageBox::InputType::OK,
|
||||
window());
|
||||
return;
|
||||
}
|
||||
|
||||
int ptm_fd = open("/dev/ptmx", O_RDWR);
|
||||
if (ptm_fd < 0) {
|
||||
perror("open(ptmx)");
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
m_terminal_widget->set_pty_master_fd(ptm_fd);
|
||||
m_terminal_widget->on_command_exit = [this] {
|
||||
int wstatus;
|
||||
int rc = waitpid(m_pid, &wstatus, 0);
|
||||
if (rc < 0) {
|
||||
perror("waitpid");
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
if (WIFEXITED(wstatus)) {
|
||||
m_terminal_widget->inject_string(String::format("\033[%d;1m(Command exited with code %d)\033[0m\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
|
||||
} else if (WIFSTOPPED(wstatus)) {
|
||||
m_terminal_widget->inject_string(String::format("\033[34;1m(Command stopped!)\033[0m\n"));
|
||||
} else if (WIFSIGNALED(wstatus)) {
|
||||
m_terminal_widget->inject_string(String::format("\033[34;1m(Command signaled with %s!)\033[0m\n", strsignal(WTERMSIG(wstatus))));
|
||||
}
|
||||
m_pid = -1;
|
||||
};
|
||||
|
||||
m_pid = fork();
|
||||
if (m_pid == 0) {
|
||||
const char* tty_name = ptsname(ptm_fd);
|
||||
if (!tty_name) {
|
||||
perror("ptsname");
|
||||
|
@ -77,16 +113,8 @@ TerminalWrapper::TerminalWrapper(GWidget* parent)
|
|||
{
|
||||
set_layout(make<GBoxLayout>(Orientation::Vertical));
|
||||
|
||||
int ptm_fd = open("/dev/ptmx", O_RDWR);
|
||||
if (ptm_fd < 0) {
|
||||
perror("open(ptmx)");
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
run_command(ptm_fd, "/bin/Shell");
|
||||
|
||||
RefPtr<CConfigFile> config = CConfigFile::get_for_app("Terminal");
|
||||
m_terminal_widget = TerminalWidget::construct(ptm_fd, false, config);
|
||||
m_terminal_widget = TerminalWidget::construct(-1, false, config);
|
||||
add_child(*m_terminal_widget);
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,11 @@ class TerminalWrapper final : public GWidget {
|
|||
public:
|
||||
virtual ~TerminalWrapper() override;
|
||||
|
||||
void run_command(const String&);
|
||||
|
||||
private:
|
||||
explicit TerminalWrapper(GWidget* parent);
|
||||
|
||||
RefPtr<TerminalWidget> m_terminal_widget;
|
||||
pid_t m_pid { -1 };
|
||||
};
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
String g_currently_open_file;
|
||||
|
||||
static void build(TerminalWrapper&);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
GApplication app(argc, argv);
|
||||
|
@ -97,6 +99,12 @@ int main(int argc, char** argv)
|
|||
}));
|
||||
menubar->add_menu(move(app_menu));
|
||||
|
||||
auto build_menu = make<GMenu>("Build");
|
||||
build_menu->add_action(GAction::create("Build", { Mod_Ctrl, Key_B }, [&](auto&) {
|
||||
build(terminal_wrapper);
|
||||
}));
|
||||
menubar->add_menu(move(build_menu));
|
||||
|
||||
auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
|
||||
|
||||
auto help_menu = make<GMenu>("Help");
|
||||
|
@ -112,3 +120,8 @@ int main(int argc, char** argv)
|
|||
window->show();
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
void build(TerminalWrapper& wrapper)
|
||||
{
|
||||
wrapper.run_command("make");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue