Skip to content

Commit

Permalink
added: onyx run-watch to auto restart program on changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed Oct 10, 2024
1 parent 7947407 commit c0b797b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions compiler/include/astnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,7 @@ enum CompileAction {
ONYX_COMPILE_ACTION_RUN,
ONYX_COMPILE_ACTION_RUN_WASM,
ONYX_COMPILE_ACTION_WATCH,
ONYX_COMPILE_ACTION_WATCH_RUN,
ONYX_COMPILE_ACTION_DOCUMENT,
ONYX_COMPILE_ACTION_PRINT_HELP,
ONYX_COMPILE_ACTION_PRINT_VERSION,
Expand Down
15 changes: 14 additions & 1 deletion compiler/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ static const char* top_level_docstring = DOCSTRING_HEADER
#if defined(_BH_LINUX) || defined(_BH_DARWIN)
C_LBLUE " watch " C_NORM "Continuously rebuilds a program on file changes\n"
#endif
#if (defined(_BH_LINUX) || defined(_BH_DARWIN)) && defined(ONYX_RUNTIME_LIBRARY)
C_LBLUE " run-watch " C_NORM "Continuously rebuilds and runs a program on file changes " C_GREY "(onyx rw)" C_NORM "\n"
#endif
#ifdef ONYX_RUNTIME_LIBRARY
"\n"
C_LBLUE " package " C_GREY "cmd " C_NORM "Package manager " C_GREY "(onyx pkg cmd)" C_NORM "\n"
Expand Down Expand Up @@ -211,6 +214,14 @@ static void cli_determine_action(CompileOptions *options, int *first_sub_arg, in
}
#endif

#if (defined(_BH_LINUX) || defined(_BH_DARWIN)) && defined(ONYX_RUNTIME_LIBRARY)
if (!strcmp(argv[1], "run-watch") || !strcmp(argv[1], "rw")) {
options->action = ONYX_COMPILE_ACTION_WATCH_RUN;
*first_sub_arg = 2;
return;
}
#endif

#if defined(_BH_LINUX) || defined(_BH_DARWIN)
if (!strcmp(argv[1], "self-upgrade")) {
options->action = ONYX_COMPILE_ACTION_SELF_UPGRADE;
Expand Down Expand Up @@ -518,6 +529,7 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
case ONYX_COMPILE_ACTION_CHECK:
case ONYX_COMPILE_ACTION_RUN:
case ONYX_COMPILE_ACTION_WATCH:
case ONYX_COMPILE_ACTION_WATCH_RUN:
case ONYX_COMPILE_ACTION_COMPILE:
cli_parse_compilation_options(&options, arg_parse_start, argc, argv);
break;
Expand Down Expand Up @@ -575,7 +587,8 @@ static void print_subcommand_help(const char *subcommand) {
return;
}

if (!strcmp(subcommand, "run") || !strcmp(subcommand, "r")) {
if (!strcmp(subcommand, "run") || !strcmp(subcommand, "r")
|| !strcmp(subcommand, "run-watch") || !strcmp(subcommand, "rw")) {
bh_printf(build_docstring, subcommand, "[-- program args]");
bh_printf(
C_LBLUE " --debug-socket " C_GREY "addr " C_NORM "Specifies the address or port used for the debug server.\n"
Expand Down
50 changes: 46 additions & 4 deletions compiler/src/onyx.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,27 +794,48 @@ void cleanup_compilation() {
}

#if defined(_BH_LINUX) || defined(_BH_DARWIN)

#include <signal.h>
#include <sys/wait.h>

static bh_file_watch watches;
static i32 watch_run_pid = -1;

static void onyx_watch_stop(int sig) {
bh_file_watch_stop(&watches);
}

static void onyx_watch_run_executable(const char *target) {
watch_run_pid = fork();
switch (watch_run_pid) {
case -1: bh_printf("error: fork() failed\n"); break;
case 0:
setpgid(0, getpid());
close(STDIN_FILENO);
open("/dev/null", O_RDONLY);
execlp("onyx", "onyx", "run", target, NULL);
exit(1);
break;
default:
break;
}
}

static void onyx_watch(CompileOptions *compile_opts) {
signal(SIGINT, onyx_watch_stop);

b32 watch_run = compile_opts->action == ONYX_COMPILE_ACTION_WATCH_RUN;

b32 running_watch = 1;

do {
bh_printf("\e[2J\e[?25l\n");
bh_printf("\e[3;1H");

if (do_compilation(compile_opts) == ONYX_COMPILER_PROGRESS_SUCCESS) {
b32 successful_compilation = do_compilation(compile_opts) == ONYX_COMPILER_PROGRESS_SUCCESS;

if (successful_compilation) {
onyx_flush_module();
bh_printf("\e[92mNo errors.\n");
bh_printf("\e[92mNo errors!\n");
}

char time_buf[128] = {0};
Expand All @@ -829,6 +850,11 @@ static void onyx_watch(CompileOptions *compile_opts) {
bh_printf("\e[30;101m Error%s %d \e[0m", bh_num_plural(errors), errors);
}

if (watch_run && successful_compilation) {
bh_printf("\n\n\nRunning your program...\n");
onyx_watch_run_executable(compile_opts->target_file);
}

watches = bh_file_watch_new();

bh_arr_each(bh_file_contents, file, context.loaded_files) {
Expand All @@ -837,11 +863,23 @@ static void onyx_watch(CompileOptions *compile_opts) {

cleanup_compilation();

if (!bh_file_watch_wait(&watches)) {
b32 wait_successful = bh_file_watch_wait(&watches);

if (watch_run) {
if (watch_run_pid > 0) {
int status;
killpg(watch_run_pid, SIGTERM);
waitpid(watch_run_pid, &status, 0);
watch_run_pid = 0;
}
}

if (!wait_successful) {
running_watch = 0;
}

bh_file_watch_free(&watches);

} while(running_watch);


Expand Down Expand Up @@ -945,6 +983,10 @@ int main(int argc, char *argv[]) {
compiler_progress = ONYX_COMPILER_PROGRESS_ERROR;
}
break;

case ONYX_COMPILE_ACTION_WATCH_RUN:
onyx_watch(&compile_opts);
break;
#endif

#if defined(_BH_LINUX) || defined(_BH_DARWIN)
Expand Down

0 comments on commit c0b797b

Please sign in to comment.