-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoreload-latest.rb
executable file
·53 lines (47 loc) · 1.48 KB
/
autoreload-latest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
# @see https://github.com/samaaron/sonic-pi/blob/master/app/server/ruby/bin/sonic-pi-server.rb#L274
require 'osc-ruby'
require 'listen'
require 'fileutils'
require 'logger'
# @example in order to control remote host, expose the port
# while :; do socat -T1 UDP-LISTEN:4557,bind=192.168.2.4 UDP:localhost:4557; done
# @see "Listen port" in ~/.sonic-pi/log/server-output.log
logger = Logger.new($stdout)
server = URI.parse(ENV['SONIC_PI_URL'] || 'udp://localhost:51235')
logger.debug "Connecting to #{server.inspect}"
client = OSC::Client.new(server.host, server.port)
gui_id = '0'
buffer_id = 'workspace_nine'
workspace = (ENV['SONIC_PI_HOME'] || Dir.home) + '/.sonic-pi/store/default'
dest = "#{workspace}/#{buffer_id}.spi"
on_change = ->(*args) {
begin
src = args[0][0]
logger.info "Reloading #{src}"
client.send(OSC::Message.new('/stop-all-jobs'))
sleep 0.5
if server.host == 'localhost'
logger.debug "Reloading buffer #{gui_id}:#{buffer_id} from #{dest}"
# FileUtils.ln_s src, dest, force: true
FileUtils.rm_f dest
FileUtils.cp src, dest
client.send(OSC::Message.new('/load-buffer', gui_id, buffer_id))
end
client.send(OSC::Message.new('/run-code', gui_id, File.read(src)))
rescue => ex
logger.error ex.inspect
end
}
Listen.to(
File.dirname(Dir.pwd),
only: /\d+.*\.rb$/,
&on_change
).start
# on_change.call
begin
sleep
rescue Interrupt
client.send(OSC::Message.new('/stop-all-jobs'))
exit
end