Created
November 22, 2021 23:09
-
-
Save bcc32/3377cd1c15578a6c0c711eca62c0b9df to your computer and use it in GitHub Desktop.
Keep directories synced
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
require 'pathname' | |
src = ARGV[0] | |
dst = ARGV[1] | |
src = "#{src}/" unless src.end_with?("/") | |
dst = "#{dst}/" unless dst.end_with?("/") | |
src = Pathname.new(src) | |
dst = Pathname.new(dst) | |
RSYNC_OPTS = %w[ | |
-ai | |
--del | |
--stats | |
] | |
def rsync src, dst | |
system 'rsync', *RSYNC_OPTS, src.to_s, dst.to_s | |
end | |
mutex = Mutex.new | |
cond = ConditionVariable.new | |
has_pending_changes = true | |
io = IO.popen(['fswatch', '-or', src.to_s]) | |
th1 = Thread.new do | |
while io.gets | |
mutex.synchronize do | |
has_pending_changes = true | |
cond.signal | |
end | |
end | |
end | |
th2 = Thread.new do | |
loop do | |
mutex.synchronize do | |
until has_pending_changes do | |
cond.wait(mutex) | |
end | |
puts "Detected changes, rsyncing '#{src}' to '#{dst}'" | |
has_pending_changes = false | |
if File.directory?(src) | |
rsync src, dst | |
else | |
puts "'#{src}' not found; skipping" | |
end | |
end | |
end | |
end | |
th1.join | |
th2.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment