Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- Dir.chdir(File.dirname(__FILE__)+"/..")
- POLL_DIRECTORIES = %w(jaf-docs/content-blocks/)
- POLL_TIME = 0.9
- @options = {}
- ARGV.each do |arg|
- if arg =~ /^--([A-Za-z-]+)/
- @options[$1.to_sym] = true
- end
- end
- @states = {}
- def check_for_changes
- changes = []
- Dir[*POLL_DIRECTORIES.map{|i| "#{i}/**/*"}].each do |file|
- new_time = File.stat(file).mtime
- if @states[file] != new_time
- @states[file] = new_time
- changes << file
- end
- end
- changes
- end
- def emphasized(m)
- "\e[1;1m\e[41m \e[0m \e[1;1m\e[1m #{m} \e[0m"
- end
- def escape_quotes(s)
- s.gsub(/'/) { |c| "\\#{c}" }
- end
- def verbose?
- not @options[:verbose].nil?
- end
- loop do
- sleep POLL_TIME
- begin
- changes = check_for_changes
- unless changes.empty?
- changes.each do |change|
- name = File.basename(change.sub(File.extname(change), ''))
- id = name[/\d+/]
- content = File.read(change)
- $stderr.puts emphasized("Updating content_block #{id}")
- sql = "UPDATE content_blocks SET content = '#{escape_quotes(content)}' WHERE id=#{id}"
- puts "Running sql statement:\n#{sql}" if verbose?
- output = IO.popen("mysql -u root within3_development", 'w') do |io|
- io << sql
- end
- $stderr.puts emphasized("done")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment