Last active
May 14, 2020 16:28
-
-
Save amirrajan/5ef8cb3576f73f79dda20463b1299599 to your computer and use it in GitHub Desktop.
An auto runner script for Mac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :default do | |
puts 'hello world' | |
end | |
task :test do | |
puts 'running test' | |
end | |
task :ancillary do | |
puts 'ancillary' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Put this script next to your Rakefile. | |
# 2. Install the following gems: | |
# - gem install rerun | |
# - gem install readline | |
# - gem install open3 | |
# 3. Then run this script through ruby: ruby sidekick.rb | |
require 'readline' | |
require 'pty' | |
require 'open3' | |
require 'find' | |
require 'FileUtils' | |
def sidekick | |
# THIS IS THE PARENT PROCESS FOR SIDEKICK YOU CAN USE THIS TO DO | |
# DEV CENTRIC TASKS | |
while expr = Readline.readline('', true) | |
if expr == 'exit' | |
exit(0) | |
elsif expr == "custom" | |
spawn_process "rake ancillary" | |
else | |
puts "I don't know what you want me to do with:" | |
puts expr | |
end | |
sleep 0.2 | |
end | |
end | |
# Spawn a seperate process for rake. | |
def spawn_process command | |
PTY.spawn("#{command}") do |stdout, stdin, _| | |
@stdin = stdin | |
Thread.new do | |
loop do | |
stdout.each { |line| print line } | |
sleep 1 | |
end | |
end | |
end | |
end | |
# For this process trap SIGINT so it's not accidentally killed and orchestrates out of process interopability. | |
Signal.trap('INT') do | |
puts '' | |
puts '============================' | |
puts 'SIGINT has to be disable because sometimes repl/rerun sends it here. Yes I agree this is silly.' | |
puts "run `kill -9 #{Process.pid}` or press enter to exit." | |
puts '============================' | |
end | |
# Process interop thats use by the file watcher called rerun. | |
@first_time = true | |
# 4. Set your default rake command here. | |
@rake_command = "rake test" | |
Signal.trap('SIGUSR1') do | |
if @first_time | |
@first_time = false | |
else | |
spawn_process @rake_command | |
end | |
end | |
# And at the same time run the ruby file watcher | |
# File pattern to watch for to auto invoke rake. | |
@file_pattern = 'rb,c,h,mm,Rakefile' | |
PTY.spawn("rerun \"kill -30 #{Process.pid}\" --no-notify") do |stdout, _, _| | |
Thread.new do | |
loop do | |
stdout.each { |line| print line if line !~ /\[rerun\]/ } | |
sleep 1 | |
end | |
end | |
end | |
spawn_process @rake_command | |
sidekick |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment