#!/usr/bin/env ruby require 'como' include Como require 'rb-inotify' Spec.command( 'up-to-date', 'Tero Isannainen', '2018', [ [ :switch, 'demo', '-d', "Demo mode (with slow compile)." ], [ :switch, 'verbose', '-v', "Verbose rubu." ], ] ) # Locks for sync. @lock = Mutex.new @comp = ConditionVariable.new @link = ConditionVariable.new # Number of pending compiles. @comps = 0 ths = [] NOC = "\e[0m" RED = "\e[31m" GREEN = "\e[32m" def start_comp puts RED + "PENDING..." + NOC @lock.synchronize do @comps += 1 @comp.signal end end # Check file changes. th = Thread.new do notify = INotify::Notifier.new notify.watch( "./src", :modify ) do |event| start_comp end notify.watch( "./bin", :modify ) do |event| start_comp end notify.run end ths.push th # Compile files. do_comp = false th = Thread.new do loop do @lock.synchronize do @comp.wait( @lock ) end loop do # Check for complation need. @lock.synchronize do if @comps >= 1 do_comp = true else do_comp = false end end # Compline and signal linking if no pending compiles left. if do_comp verbose = '' if Opt['verbose'].given verbose = ' -v' end cmd = "bin/rubu_example #{verbose} up-to-date" if Opt['demo'].given cmd += "; sleep 3" end system cmd @lock.synchronize do @comps -= 1 if @comps == 0 @link.signal end end else break end end end end ths.push th # Link files. th = Thread.new do loop do @lock.synchronize do @link.wait( @lock ) verbose = '' if Opt['verbose'].given verbose = ' -v' end system "bin/rubu_example #{verbose} link" puts GREEN + "READY!" + NOC end end end ths.push th ths.each{|i| i.join}