Skip to content

Instantly share code, notes, and snippets.

@bcc32
Created November 22, 2021 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcc32/3377cd1c15578a6c0c711eca62c0b9df to your computer and use it in GitHub Desktop.
Save bcc32/3377cd1c15578a6c0c711eca62c0b9df to your computer and use it in GitHub Desktop.
Keep directories synced
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