From b950b8d858f2316377c380d20b63d09bb0f4c4bc Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Fri, 6 Dec 2024 11:50:12 +0800 Subject: [PATCH 1/4] Initial windows support: https://github.com/samueleaton/sentry/pull/68 1. Use process.terminate instead of signal(:kill) 2. Terminate app process before rebuilding on Windows --- src/sentry/process_runner.cr | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/sentry/process_runner.cr b/src/sentry/process_runner.cr index 608cfff..b9faad7 100644 --- a/src/sentry/process_runner.cr +++ b/src/sentry/process_runner.cr @@ -139,6 +139,14 @@ module Sentry private def build_app_process : Process::Status stdout "🤖 compiling #{@display_name}..." + {% if flag?(:win32) %} + if (app_process = @app_process).is_a? Process + stdout "🤖 killing #{@display_name}..." + app_process.terminate + # app_process.wait + end + {% end %} + Process.run( @build_command, @build_args_list, @@ -151,7 +159,7 @@ module Sentry if (app_process = @app_process).is_a? Process unless app_process.terminated? stdout "🤖 killing #{@display_name}..." - app_process.signal(:kill) + app_process.terminate app_process.wait end end From 02d233edb56e60f765d47e75de33bf3d1e36ffe0 Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Fri, 6 Dec 2024 12:37:47 +0800 Subject: [PATCH 2/4] Refactor on audio player for skip in windows --- src/sentry/process_runner.cr | 31 ++++++++++++++----------------- src/sentry/sound_file_storage.cr | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/sentry/process_runner.cr b/src/sentry/process_runner.cr index b9faad7..46220ce 100644 --- a/src/sentry/process_runner.cr +++ b/src/sentry/process_runner.cr @@ -2,9 +2,10 @@ module Sentry class ProcessRunner FILE_TIMESTAMPS = {} of String => String # {file => timestamp} - @sound_player : String? - @success_wav : BakedFileSystem::BakedFile = SoundFileStorage.get("success.wav") - @error_wav : BakedFileSystem::BakedFile = SoundFileStorage.get("error.wav") + {% if flag?(:linux) %} + @audio_player : AudioPlayer? + {% end %} + @app_process : Process? def initialize( @@ -27,7 +28,7 @@ module Sentry end {% if flag?(:linux) %} - @sound_player = Process.find_executable("aplay") if @should_play_audio + @audio_player = AudioPlayer.new if @should_play_audio {% end %} end @@ -104,33 +105,29 @@ module Sentry private def start_app : Process? return create_app_process unless @should_build - sound_player = @sound_player + audio_player = nil + + {% if flag?(:linux) %} + audio_player = @audio_player + {% end %} + build_result = build_app_process if build_result && build_result.success? @app_built = true process = create_app_process - unless sound_player.nil? - Process.new(command: sound_player, input: @success_wav) - @success_wav.rewind - end + audio_player.success unless audio_player.nil? process elsif !@app_built # if build fails on first time compiling, then exit stdout "🤖 Compile time errors detected. SentryBot shutting down..." - unless sound_player.nil? - Process.new(command: sound_player, input: @error_wav) - @error_wav.rewind - end + audio_player.error unless audio_player.nil? exit 1 else - unless sound_player.nil? - Process.new(command: sound_player, input: @error_wav) - @error_wav.rewind - end + audio_player.error unless audio_player.nil? nil end diff --git a/src/sentry/sound_file_storage.cr b/src/sentry/sound_file_storage.cr index a320126..d4c6955 100644 --- a/src/sentry/sound_file_storage.cr +++ b/src/sentry/sound_file_storage.cr @@ -1,3 +1,5 @@ +{% skip_file if flag?(:win32) %} + require "baked_file_system" class SoundFileStorage @@ -5,3 +7,27 @@ class SoundFileStorage bake_folder "./sounds" end + +class AudioPlayer + @success_wav : BakedFileSystem::BakedFile = SoundFileStorage.get("success.wav") + @error_wav : BakedFileSystem::BakedFile = SoundFileStorage.get("error.wav") + @player : String? + + def initialize + @player = Process.find_executable("aplay") + end + + def success + if (player = @player) + Process.new(command: player, input: @success_wav) + @success_wav.rewind + end + end + + def error + if (player = @player) + Process.new(command: player, input: @error_wav) + @error_wav.rewind + end + end +end From 5649916857036370de3da1e9c833b821a6be91ea Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Fri, 6 Dec 2024 12:56:50 +0800 Subject: [PATCH 3/4] Use portable terminate api for catch Ctrl+C --- src/sentry/process_runner.cr | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/sentry/process_runner.cr b/src/sentry/process_runner.cr index 46220ce..a9ca7b0 100644 --- a/src/sentry/process_runner.cr +++ b/src/sentry/process_runner.cr @@ -23,8 +23,11 @@ module Sentry @should_kill = false @app_built = false - Signal::INT.trap do - @should_kill = true + Process.on_terminate do |reason| + case reason + when .interrupted? + @should_kill = true + end end {% if flag?(:linux) %} From be411697bdec4e726436e854a0537bca8df18caa Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Fri, 6 Dec 2024 13:17:21 +0800 Subject: [PATCH 4/4] Add .exe extension name when run command in windows --- src/sentry/process_runner.cr | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/sentry/process_runner.cr b/src/sentry/process_runner.cr index a9ca7b0..38109aa 100644 --- a/src/sentry/process_runner.cr +++ b/src/sentry/process_runner.cr @@ -35,12 +35,20 @@ module Sentry {% end %} end + def run_command : String + {% if flag?(:win32) %} + "#{@run_command}.exe" + {% else %} + @run_command + {% end %} + end + def run : Nil stdout "🤖 Your SentryBot is vigilant. beep-boop..." run_shards_install if @run_shards_install - File.delete?(@run_command) if @should_build + File.delete?(run_command) if @should_build loop do if @should_kill @@ -166,15 +174,15 @@ module Sentry stdout "🤖 starting #{@display_name}..." - if File.file?(@run_command) + if File.file?(run_command) @app_process = Process.new( - @run_command, + run_command, @run_args_list, output: :inherit, error: :inherit ) else - puts "🤖 Sentry error: the inferred run command file(#{@run_command}) \ + puts "🤖 Sentry error: the inferred run command file(#{run_command}) \ does not exist. either set correct run command use `-r COMMAND' or fix the \ `BUILD ARGS' to output correct run command. SentryBot shutting down..." exit 1